diff --git a/boards/migrations/0003_board_slug.py b/boards/migrations/0003_topic_views.py similarity index 61% rename from boards/migrations/0003_board_slug.py rename to boards/migrations/0003_topic_views.py index 529394a..7f97714 100644 --- a/boards/migrations/0003_board_slug.py +++ b/boards/migrations/0003_topic_views.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Generated by Django 1.11.4 on 2017-09-25 08:56 +# Generated by Django 1.11.4 on 2017-10-01 17:07 from __future__ import unicode_literals from django.db import migrations, models @@ -13,8 +13,8 @@ class Migration(migrations.Migration): operations = [ migrations.AddField( - model_name='board', - name='slug', - field=models.SlugField(max_length=30, null=True, unique=True), + model_name='topic', + name='views', + field=models.PositiveIntegerField(default=0), ), ] diff --git a/boards/migrations/0004_auto_20170925_0857.py b/boards/migrations/0004_auto_20170925_0857.py deleted file mode 100644 index 6d95589..0000000 --- a/boards/migrations/0004_auto_20170925_0857.py +++ /dev/null @@ -1,41 +0,0 @@ -# -*- coding: utf-8 -*- -# Generated by Django 1.11.4 on 2017-09-25 08:57 -from __future__ import unicode_literals - -from django.db import migrations, IntegrityError -from django.utils.text import slugify -from django.utils.crypto import get_random_string - - -def slugify_names(apps, schema_editor): - ''' - We can't import the Board model directly as it may be a newer - version than this migration expects. We use the historical version. - ''' - Board = apps.get_model('boards', 'Board') - for board in Board.objects.all(): - try: - board.slug = slugify(board.name) - board.save() - except IntegrityError: - ''' - Even though the Board.name is marked as unique, `Django` and `django` - would produce the same slug. Create a fall back with a random slug - This is unlike to happen, but just in case. - ''' - slug = slugify(get_random_string(10)) - while Board.objects.filter(slug=slug).exists(): - slug = slugify(get_random_string(10)) - board.slug = slug - board.save() - - -class Migration(migrations.Migration): - - dependencies = [ - ('boards', '0003_board_slug'), - ] - - operations = [ - migrations.RunPython(slugify_names), - ] diff --git a/boards/migrations/0005_auto_20170925_0921.py b/boards/migrations/0005_auto_20170925_0921.py deleted file mode 100644 index 15b82c7..0000000 --- a/boards/migrations/0005_auto_20170925_0921.py +++ /dev/null @@ -1,20 +0,0 @@ -# -*- coding: utf-8 -*- -# Generated by Django 1.11.4 on 2017-09-25 09:21 -from __future__ import unicode_literals - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('boards', '0004_auto_20170925_0857'), - ] - - operations = [ - migrations.AlterField( - model_name='board', - name='slug', - field=models.SlugField(max_length=30, unique=True), - ), - ] diff --git a/boards/models.py b/boards/models.py index f773490..9e77378 100644 --- a/boards/models.py +++ b/boards/models.py @@ -1,21 +1,31 @@ from django.contrib.auth.models import User from django.db import models +from django.utils.text import Truncator class Board(models.Model): name = models.CharField(max_length=30, unique=True) - slug = models.SlugField(max_length=30, unique=True) description = models.CharField(max_length=100) def __str__(self): return self.name + def get_posts_count(self): + return Post.objects.filter(topic__board=self).count() + + def get_last_post(self): + return Post.objects.filter(topic__board=self).order_by('-created_at').first() + class Topic(models.Model): subject = models.CharField(max_length=255) last_updated = models.DateTimeField(auto_now_add=True) board = models.ForeignKey(Board, related_name='topics') starter = models.ForeignKey(User, related_name='topics') + views = models.PositiveIntegerField(default=0) + + def __str__(self): + return self.subject class Post(models.Model): @@ -25,3 +35,7 @@ class Post(models.Model): updated_at = models.DateTimeField(null=True) created_by = models.ForeignKey(User, related_name='posts') updated_by = models.ForeignKey(User, null=True, related_name='+') + + def __str__(self): + truncated_message = Truncator(self.message) + return truncated_message.chars(30) diff --git a/boards/views.py b/boards/views.py index d23a74a..fa99445 100644 --- a/boards/views.py +++ b/boards/views.py @@ -1,3 +1,4 @@ +from django.db.models import Count from django.contrib.auth.decorators import login_required from django.shortcuts import get_object_or_404, redirect, render @@ -12,7 +13,8 @@ def home(request): def board_topics(request, pk): board = get_object_or_404(Board, pk=pk) - return render(request, 'topics.html', {'board': board}) + topics = board.topics.order_by('-last_updated').annotate(replies=Count('posts') - 1) + return render(request, 'topics.html', {'board': board, 'topics': topics}) @login_required @@ -38,6 +40,8 @@ def new_topic(request, pk): def topic_posts(request, pk, topic_pk): topic = get_object_or_404(Topic, board__pk=pk, pk=topic_pk) + topic.views += 1 + topic.save() return render(request, 'topic_posts.html', {'topic': topic}) diff --git a/templates/home.html b/templates/home.html index 8c1b24f..dccc680 100644 --- a/templates/home.html +++ b/templates/home.html @@ -21,9 +21,27 @@ {{ board.name }} {{ board.description }} -