Commit ddfbc71c authored by Emmanuel Linus T. Evangelista's avatar Emmanuel Linus T. Evangelista

Merge branch 'evangelista/forum' into 'master'

Create Reply model, update and migrate Forum models, update Forum views

See merge request !8
parents 31836b43 94a82378
from django.contrib import admin from django.contrib import admin
# Register your models here. # Register your models here.
from .models import Post from .models import Post, Reply
admin.site.register(Post) class PostAdmin(admin.ModelAdmin):
\ No newline at end of file model = Post
search_fields = ['post_title', 'post_body', 'author']
list_display = ['post_title', 'post_body', 'pub_date', 'author']
list_filter = ['author']
class ReplyAdmin(admin.ModelAdmin):
model = Reply
search_fields = ['reply_body', 'author', 'post']
list_display = ['post', 'reply_body', 'pub_date', 'author']
list_filter = ['author']
admin.site.register(Post, PostAdmin)
admin.site.register(Reply, ReplyAdmin)
\ No newline at end of file
# Generated by Django 3.2.12 on 2022-03-24 17:35
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('Homepage', '0001_initial'),
('Forum', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='post',
name='author',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='Homepage.widgetuser'),
),
migrations.CreateModel(
name='Reply',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('reply_body', models.CharField(max_length=500)),
('pub_date', models.DateTimeField(auto_now_add=True, verbose_name='date published')),
('author', models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='Homepage.widgetuser')),
('post', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='Forum.post')),
],
),
]
from django.db import models from django.db import models
from Homepage.models import WidgetUser
# Create your models here. # Create your models here.
class Post(models.Model): class Post(models.Model):
post_title = models.CharField(max_length=50) post_title = models.CharField(max_length=50)
post_body = models.CharField(max_length=500) post_body = models.CharField(max_length=500)
pub_date = models.DateTimeField("date published", auto_now_add=True) pub_date = models.DateTimeField("date published", auto_now_add=True)
\ No newline at end of file
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, default=1)
def __str__(self):
return self.post_title
class Reply(models.Model):
reply_body = models.CharField(max_length=500)
pub_date = models.DateTimeField("date published", auto_now_add=True)
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, default=1)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
def __str__(self):
if len(self.reply_body) > 30:
return self.reply_body[0:30] + '...'
else:
return self.reply_body
\ No newline at end of file
from django.http import HttpResponse from django.http import HttpResponse
from .models import Post, Reply
def get_readable_time(timestamp):
return timestamp.strftime("%m/%d/%Y")
def index(request): def index(request):
return HttpResponse('Welcome to Widget\'s Forum!') out = "FORUM POSTS: <br />"
\ No newline at end of file
posts = Post.objects.all()
for post in posts:
out += f'{post.post_title} by {post.author.first_name} {post.author.last_name} dated {get_readable_time(post.pub_date)}: <br />'
out += f'{post.post_body} <br />'
post_replies = post.reply_set.all()
for reply in post_replies:
out += f'Reply by {reply.author.first_name} {reply.author.last_name} dated {get_readable_time(reply.pub_date)}: <br />'
out += f'{reply.reply_body} <br />'
out += '<br />'
return HttpResponse(out)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment