Commit e0c34617 authored by Ryan Angelo G. Lim's avatar Ryan Angelo G. Lim

Added Reply model and updated views

parent dcd7f9c9
...@@ -15,4 +15,4 @@ class ReactionAdmin(admin.ModelAdmin): ...@@ -15,4 +15,4 @@ class ReactionAdmin(admin.ModelAdmin):
model = Reaction model = Reaction
admin.site.register(Announcement, AnnouncementAdmin) admin.site.register(Announcement, AnnouncementAdmin)
admin.site.register(Reaction, ReactionAdmin) admin.site.register(Reaction, ReactionAdmin)
\ No newline at end of file
...@@ -7,10 +7,10 @@ class Announcement(models.Model): ...@@ -7,10 +7,10 @@ class Announcement(models.Model):
announcement_body = models.CharField(max_length=500) announcement_body = models.CharField(max_length=500)
pub_date = models.DateTimeField("date published", null=True) pub_date = models.DateTimeField("date published", null=True)
author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, null=True, related_name='author') author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, null=True, related_name='author')
def __str__(self): def __str__(self):
return self.announcement_title return self.announcement_title
class Reaction(models.Model): class Reaction(models.Model):
Announcement = models.ForeignKey(Announcement, on_delete=models.CASCADE, null=True, related_name='announcements') Announcement = models.ForeignKey(Announcement, on_delete=models.CASCADE, null=True, related_name='announcements')
reaction_name = models.CharField(max_length=50) reaction_name = models.CharField(max_length=50)
......
# Generated by Django 4.0.3 on 2022-04-05 09:36
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('assignments', '0017_assignment_passing_score'),
]
operations = [
migrations.AlterField(
model_name='assignment',
name='passing_score',
field=models.IntegerField(default=0, editable=False),
),
]
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 ReplyAdminInline(admin.TabularInline):
model = Reply
class PostAdmin(admin.ModelAdmin):
model = Post
inlines = [ReplyAdminInline]
class ReplyAdmin(admin.ModelAdmin):
model = Reply
admin.site.register(Post, PostAdmin)
admin.site.register(Reply, ReplyAdmin)
# Generated by Django 4.0.3 on 2022-04-05 09:36
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('homepage', '0002_widgetuser_email_widgetuser_id_num_department'),
('forum', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='post',
name='author',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='post_author', 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)),
('reply_pub_date', models.DateTimeField(verbose_name='date published')),
('post', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='posts', to='forum.post')),
('reply_author', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='reply_author', to='homepage.widgetuser')),
],
),
]
# Generated by Django 4.0.3 on 2022-04-05 10:13
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('forum', '0002_post_author_reply'),
]
operations = [
migrations.RenameField(
model_name='post',
old_name='author',
new_name='post_author',
),
]
# Generated by Django 4.0.3 on 2022-04-05 10:41
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('forum', '0003_rename_author_post_post_author'),
]
operations = [
migrations.AlterField(
model_name='post',
name='pub_date',
field=models.DateField(verbose_name='date published'),
),
migrations.AlterField(
model_name='reply',
name='reply_pub_date',
field=models.DateField(verbose_name='date published'),
),
]
# Generated by Django 4.0.3 on 2022-04-05 10:43
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('forum', '0004_alter_post_pub_date_alter_reply_reply_pub_date'),
]
operations = [
migrations.AlterField(
model_name='post',
name='pub_date',
field=models.DateTimeField(verbose_name='date published'),
),
migrations.AlterField(
model_name='reply',
name='reply_pub_date',
field=models.DateTimeField(verbose_name='date published'),
),
]
# Generated by Django 4.0.3 on 2022-04-05 10:45
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('forum', '0005_alter_post_pub_date_alter_reply_reply_pub_date'),
]
operations = [
migrations.AlterField(
model_name='post',
name='pub_date',
field=models.DateField(verbose_name='date published'),
),
migrations.AlterField(
model_name='reply',
name='reply_pub_date',
field=models.DateField(verbose_name='date published'),
),
]
# Generated by Django 4.0.3 on 2022-04-05 10:53
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('forum', '0006_alter_post_pub_date_alter_reply_reply_pub_date'),
]
operations = [
migrations.AlterField(
model_name='post',
name='pub_date',
field=models.DateField(auto_now_add=True, verbose_name='date published'),
),
migrations.AlterField(
model_name='reply',
name='reply_pub_date',
field=models.DateField(auto_now_add=True, verbose_name='date published'),
),
]
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") pub_date = models.DateField("date published", auto_now_add=True)
post_author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, null=True, related_name='post_author')
def __str__(self): def __str__(self):
return self.post_title return self.post_title
class Reply(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE, null=True, related_name='posts')
reply_body = models.CharField(max_length=500)
reply_pub_date = models.DateField("date published", auto_now_add=True)
reply_author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, null=True, related_name='reply_author')
from django.http import HttpResponse from django.http import HttpResponse
from forum.models import Post, Reply, WidgetUser
# Create your views here. # Create your views here.
def index(request): def index(request):
return HttpResponse("Welcome to Widget's Forum!") entries = Post.objects.all().count()
n = 1
forum = "FORUM POSTS:<br/>"
while(n<=entries):
forum += getPost(n)
forum += getReply(n)
n += 1
return HttpResponse(forum)
def getPost(pk):
postFirstLine = ""
postSecondLine = ""
post = ""
postTitle = Post.objects.get(pk=pk).post_title
authorFirstName = Post.objects.get(pk=pk).post_author.first_name
authorLastName = Post.objects.get(pk=pk).post_author.last_name
publicationDate = Post.objects.get(pk=pk).pub_date
postBody = Post.objects.get(pk=pk).post_body
postFirstLine = "{} by {} {} dated {}:<br/>" .format(postTitle, authorFirstName, authorLastName, publicationDate)
postSecondLine = "{}<br/>" .format(postBody)
post += postFirstLine + postSecondLine
return post
def getReply(pk):
replies = ""
for reply in Post.objects.get(pk=pk).posts.all():
replies += "Reply by {} {} {}:<br/>{}" .format(reply.reply_author.first_name, reply.reply_author.last_name, reply.reply_pub_date, reply.reply_body)
replies += "<br/>"
replies += "<br/>"
return replies
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