Commit e09a862a authored by Ross Batacan's avatar Ross Batacan

Modified admin.py and views.py to allow edits in /admin. Please add ForeignKey...

Modified admin.py and views.py to allow edits in /admin. Please add ForeignKey on announcements/models.py line 10"
parent f6de2fcd
from django.contrib import admin
from .models import Announcement, Reaction
# Register your models here.
# class ReactionInLine(admin.TabularInline):
# model = Reaction
class AnnouncementAdmin(admin.ModelAdmin):
model = Announcement
list_display = ('announcement_title', 'announcement_body', 'announcement_pub_datetime')
search_fields = ('announcement_title',)
list_filter = ('announcement_title',)
# inlines = [ReactionInLine]
class ReactionAdmin(admin.ModelAdmin):
model = Reaction
list_display = ('reaction_name', 'reaction_tally', 'reaction_announcement',)
search_fields = ('reaction_name',)
list_filter = ('reaction_name',)
admin.site.register(Announcement, AnnouncementAdmin)
admin.site.register(Reaction, ReactionAdmin)
# Generated by Django 3.2 on 2023-03-05 07:16
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Announcement',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('announcement_title', models.CharField(max_length=50, null=True, unique=True)),
('announcement_body', models.CharField(max_length=250, null=True, unique=True)),
('announcement_pub_datetime', models.DateTimeField(null=True)),
],
),
migrations.CreateModel(
name='Reaction',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('reaction_name', models.CharField(max_length=50, null=True)),
('reaction_tally', models.IntegerField(null=True)),
('reaction_announcement', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='announcements.announcement')),
],
),
]
# Generated by Django 3.2 on 2023-03-05 09:07
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcements', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='reaction',
name='reaction_name',
field=models.CharField(blank=True, choices=[('Like', 'Like'), ('Love', 'Love'), ('Angry', 'Angry')], max_length=50, null=True),
),
migrations.AlterField(
model_name='reaction',
name='reaction_tally',
field=models.IntegerField(default=0, null=True),
),
]
from django.db import models
# Create your models here.
# please insert foreign key from dashboard application on announcement_author
class Announcement(models.Model):
announcement_title = models.CharField(max_length=50, unique=True, null=True,)
announcement_body = models.CharField(max_length=250, unique=True, null=True,)
announcement_title = models.CharField(max_length=50, unique=True, null=True, )
announcement_body = models.CharField(max_length=250, unique=True, null=True, )
# announcement_author = models.ForeignKey(on_delete=models.CASCADE, null=True)
announcement_pub_datetime = models.DateTimeField(null=True)
announcement_pub_datetime = models.DateTimeField(null=True, )
def __str__(self):
return self.announcement_title
# def __str__(self):
# return self.announcement_title
reaction_choices = (
('Like', 'Like'),
('Love', 'Love'),
('Angry', 'Angry'),
)
class Reaction(models.Model):
reaction_name = models.CharField(max_length=50, null=True,)
reaction_tally = models.IntegerField(null=True)
reaction_announcement = models.ForeignKey(Announcement, on_delete=models.CASCADE, null=True)
reaction_name = models.CharField(max_length=50, blank=True, choices=reaction_choices, null=True,)
reaction_tally = models.IntegerField(null=True, default=0, )
reaction_announcement = models.ForeignKey(Announcement, on_delete=models.CASCADE, null=True, )
def __str__(self):
return self.reaction_name
\ No newline at end of file
return self.reaction_name
from django.http import HttpResponse
# from django.http import HttpResponse
# from .models import Announcement, Reaction
#
#
# # Create your views here.
#
#
# def announcements(request):
# announcement = Announcement.objects.all()
# react = Reaction.objects.all()
#
# head = "Widget's Announcement Board <br><br>Announcements:"
# response = ''
#
# for every in announcement:
# like_on_post = 0
# love_on_post = 0
# angry_on_post = 0
#
# reaction = Reaction.objects.filter(reaction_announcement=every)
#
# datetime = every.announcement_pub_datetime.strftime("%a, %b %d, %Y %I:%M %p")
# response += every.announcement_title + " by " + "--insert foreign name-- " + "published <br>" +\
# datetime + "<br>"
#
# for r in reaction:
# response += r.reaction_name + ": " + str(r.reaction_tally) + "<br>"
# response += "<br>"
#
# final = head + "<br><br>" + response
#
# return HttpResponse(final)
# Create your views here.
from django.http import HttpResponse
from .models import Announcement, Reaction
def announcements(request):
return HttpResponse("testing")
\ No newline at end of file
announcement = Announcement.objects.all()
response = "<h2>Widget's Announcement Board</h2>"
for every in announcement:
datetime = every.announcement_pub_datetime.strftime("%a, %b %d, %Y %I:%M %p")
response += "<h3>{}</h3>".format(every.announcement_title) +\
"<p>By --insert foreign name-- published {}</p>".format(datetime)
like_on_post = 0
love_on_post = 0
angry_on_post = 0
reactions = Reaction.objects.filter(reaction_announcement=every)
for every in reactions:
if every.reaction_name == "Like":
like_on_post = every.reaction_tally
elif every.reaction_name == "Love":
love_on_post = every.reaction_tally
elif every.reaction_name == "Angry":
angry_on_post = every.reaction_tally
response += "<p>like: {}".format(like_on_post) +\
"<br>love: {}".format(love_on_post) +\
"<br>angry: {}</p>".format(angry_on_post)
return HttpResponse(response)
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