Commit d8a574ab authored by Bianca Aguilar's avatar Bianca Aguilar

Created Post + Reply Models; created view for displaying them

parent 72d001e7
No preview for this file type
from django.contrib import admin
# Register your models here.
from .models import Post, Reply, WidgetUser
from .models import Post, Reply
class PostAdmin(admin.ModelAdmin):
model = Post
......
# Generated by Django 4.0.3 on 2022-04-04 17:31
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('homepage', '0003_widgetuser_department'),
('forum', '0005_alter_post_author_alter_reply_author'),
]
operations = [
migrations.AlterField(
model_name='post',
name='author',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='homepage.widgetuser'),
preserve_default=False,
),
migrations.AlterField(
model_name='reply',
name='author',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='homepage.widgetuser'),
preserve_default=False,
),
]
# Generated by Django 4.0.3 on 2022-04-05 04:49
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('forum', '0006_alter_post_author_alter_reply_author'),
]
operations = [
migrations.AlterField(
model_name='post',
name='pub_date',
field=models.DateTimeField(auto_now=True),
),
migrations.AlterField(
model_name='reply',
name='pub_date',
field=models.DateTimeField(auto_now=True),
),
]
# Generated by Django 4.0.3 on 2022-04-05 05:44
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('forum', '0007_alter_post_pub_date_alter_reply_pub_date'),
]
operations = [
migrations.AddField(
model_name='reply',
name='post',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='forum.post'),
preserve_default=False,
),
migrations.AlterField(
model_name='post',
name='pub_date',
field=models.DateField(auto_now=True),
),
migrations.AlterField(
model_name='reply',
name='pub_date',
field=models.DateField(auto_now=True),
),
]
# Generated by Django 4.0.3 on 2022-04-05 05:47
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('forum', '0008_reply_post_alter_post_pub_date_alter_reply_pub_date'),
]
operations = [
migrations.AlterField(
model_name='post',
name='pub_date',
field=models.DateTimeField(auto_now=True),
),
migrations.AlterField(
model_name='reply',
name='pub_date',
field=models.DateTimeField(auto_now=True),
),
]
# Generated by Django 4.0.3 on 2022-04-05 05:49
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('forum', '0009_alter_post_pub_date_alter_reply_pub_date'),
]
operations = [
migrations.AlterField(
model_name='post',
name='pub_date',
field=models.DateField(auto_now=True),
),
migrations.AlterField(
model_name='reply',
name='pub_date',
field=models.DateField(auto_now=True),
),
]
from tkinter import Widget
from django.db import models
from django.urls import reverse
......@@ -9,11 +8,10 @@ from homepage.models import WidgetUser
class Post(models.Model):
post_title = models.CharField(max_length=50)
post_body = models.CharField(max_length=500)
pub_date = models.DateTimeField("Date Published", auto_now=True)
pub_date = models.DateField(auto_now=True)
author = models.ForeignKey(
WidgetUser,
on_delete=models.CASCADE,
null=True,
'homepage.WidgetUser',
on_delete=models.CASCADE
)
def __str__(self):
......@@ -21,22 +19,23 @@ class Post(models.Model):
@property
def post_detail(self):
post = '{} by {} {} dated {}:'.format(self.post_title, self.author.first_name, self.author.last_name, self.pub_date)
post = '<br>{} by {} dated {}:'.format(self.post_title, self.author.forum_name, self.pub_date)
post += '<br>{}'.format(self.post_body)
return post
def get_absolute_url(self):
return reverse('post_detail', args=[str(self.pub_date)])
class Reply(models.Model):
reply_body = models.CharField(max_length=500)
pub_date = models.DateTimeField("Date Published", auto_now=True)
pub_date = models.DateField(auto_now=True)
author = models.ForeignKey(
WidgetUser,
'homepage.WidgetUser',
on_delete=models.CASCADE
)
post = models.ForeignKey(
Post,
on_delete=models.CASCADE,
null=True,
related_name="comments"
)
def __str__(self):
......@@ -44,11 +43,12 @@ class Reply(models.Model):
@property
def reply_detail(self):
reply = 'Reply by {} {} dated {}:'.format(self.author.first_name, self.author.last_name, self.pub_date)
reply = '<br>Reply by {} {} dated {}:'.format(self.author.first_name, self.author.last_name, self.pub_date)
reply += '<br>{}'.format(self.reply_body)
return reply
def get_absolute_url(self):
return reverse('reply_detail', args=[str(self.pub_date)])
@property
def associated_post(self):
return '{}'.format(self.post.__str__)
from django.shortcuts import render
from django.http import HttpResponse
from .models import Post
from .models import Post, Reply
# Create your views here.
def index(request):
......@@ -7,6 +8,9 @@ def index(request):
final_list = ''
for p in range(len(Post.objects.all())):
final_list += '{}'.format(Post.objects.get(pk=p+1).post_detail)
reply_list = Post.objects.get(pk=p+1).comments.all()
for r in range(len(reply_list)):
final_list += '{}'.format(Reply.objects.get(post__exact=Post.objects.get(pk=p+1)).reply_detail)
return final_list
......@@ -14,7 +18,9 @@ def index(request):
<html>
<body>
<h1>FORUM POSTS:</h1>
<main>
<p>{post_list()}</p>
</main>
</body>
</html>
'''
......
......@@ -23,6 +23,11 @@ class WidgetUser(models.Model):
def full_name(self):
return '{}, {} {}'.format(self.last_name, self.first_name, self.middle_name)
@property
def forum_name(self):
return '{} {}'.format(self.first_name, self.last_name)
@property
def user_info(self):
return '<br>{}: {}, {}, {}'.format(self.full_name, self.id_num, self.email, self.department.dept_info)
\ 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