Skip to content

Commit

Permalink
Migrate topic model
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorfs committed Oct 1, 2017
1 parent d50931c commit dc7aa18
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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),
),
]
41 changes: 0 additions & 41 deletions boards/migrations/0004_auto_20170925_0857.py

This file was deleted.

20 changes: 0 additions & 20 deletions boards/migrations/0005_auto_20170925_0921.py

This file was deleted.

16 changes: 15 additions & 1 deletion boards/models.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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)
6 changes: 5 additions & 1 deletion boards/views.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand All @@ -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})


Expand Down
24 changes: 21 additions & 3 deletions templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,27 @@
<a href="{% url 'board_topics' board.pk %}">{{ board.name }}</a>
<small class="text-muted d-block">{{ board.description }}</small>
</td>
<td class="align-middle">0</td>
<td class="align-middle">0</td>
<td></td>
<td class="align-middle">
{{ board.get_posts_count }}
</td>
<td class="align-middle">
{{ board.topics.count }}
</td>
<td class="align-middle">
{% with post=board.get_last_post %}
{% if post %}
<small>
<a href="{% url 'topic_posts' board.pk post.topic.pk %}">
By {{ post.created_by.username }} at {{ post.created_at }}
</a>
</small>
{% else %}
<small class="text-muted">
<em>No posts yet.</em>
</small>
{% endif %}
{% endwith %}
</td>
</tr>
{% endfor %}
</tbody>
Expand Down
2 changes: 1 addition & 1 deletion templates/reply_topic.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</form>

{% for post in topic.posts.all %}
<div class="card">
<div class="card mb-2">
<div class="card-body p-3">
<div class="row mb-3">
<div class="col-6">
Expand Down
6 changes: 3 additions & 3 deletions templates/topics.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
</tr>
</thead>
<tbody>
{% for topic in board.topics.all %}
{% for topic in topics %}
<tr>
<td><a href="{% url 'topic_posts' board.pk topic.pk %}">{{ topic.subject }}</a></td>
<td>{{ topic.starter.username }}</td>
<td>0</td>
<td>0</td>
<td>{{ topic.replies }}</td>
<td>{{ topic.views }}</td>
<td>{{ topic.last_updated }}</td>
</tr>
{% endfor %}
Expand Down

0 comments on commit dc7aa18

Please sign in to comment.