Commit 24f3c55b authored by Jiuvi Anne Hu's avatar Jiuvi Anne Hu

Updated announcements.models and announcements.views

parent d3a3009f
*.env *.env
widget_robo_mommy/announcements/__pycache__
\ No newline at end of file
from django.contrib import admin from django.contrib import admin
from .models import Announcement, Reaction
class AnnouncementAdmin(admin.ModelAdmin):
model = Announcement
class ReactionAdmin(admin.ModelAdmin):
model = Reaction
# Register your models here. # Register your models here.
admin.site.register(Announcement, AnnouncementAdmin)
admin.site.register(Reaction, ReactionAdmin)
\ No newline at end of file
# Generated by Django 4.1.7 on 2023-03-05 14:21
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
('Dashboard', '0002_widgetuser'),
]
operations = [
migrations.CreateModel(
name='Announcement',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.TextField(blank=True, null=True)),
('body', models.TextField(blank=True, null=True)),
('pub_datetime', models.DateTimeField(null=True)),
('author', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='Dashboard.widgetuser')),
],
),
migrations.CreateModel(
name='Reaction',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(blank=True, default='Like', max_length=5, null=True)),
('tally', models.IntegerField(blank=True, default=0, null=True)),
('announcement', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='announcements.announcement')),
],
),
]
from django.db import models from django.db import models
from Dashboard.models import WidgetUser
class Announcement(models.Model): class Announcement(models.Model):
title = models.TextField(null=True, blank=True) title = models.TextField(null=True, blank=True)
body = models.TextField(null=True, blank=True) body = models.TextField(null=True, blank=True)
author = models.CharField(max_length=255, null=True, blank=True) author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, null=True)
pub_datetime = models.DateTimeField(null=True, blank=True) pub_datetime = models.DateTimeField(null=True)
class Reaction(models.Model): class Reaction(models.Model):
LIKE = 'Like' LIKE = 'Like'
...@@ -18,6 +19,6 @@ class Reaction(models.Model): ...@@ -18,6 +19,6 @@ class Reaction(models.Model):
name = models.CharField(max_length=5, default=LIKE, null=True, blank=True) name = models.CharField(max_length=5, default=LIKE, null=True, blank=True)
tally = models.IntegerField(default=0 ,null=True, blank=True) tally = models.IntegerField(default=0 ,null=True, blank=True)
announcement = models.ForeignKey(Announcement, on_delete=models.CASCADE, null=True, blank=True) announcement = models.ForeignKey(Announcement, on_delete=models.CASCADE, null=True)
# Create your models here. # Create your models here.
from django.shortcuts import render from django.shortcuts import render
from django.http import HttpResponse from django.http import HttpResponse
from .models import Announcement, Reaction
import pytz
from django.utils import timezone
def convert_to_localtime(utctime):
format = '%d/%m/%Y %H:%M'
utc = utctime.replace(tzinfo=pytz.UTC)
localtz = utc.astimezone(timezone.get_current_timezone())
return localtz.strftime(format)
def index(request): def index(request):
return HttpResponse('temp') html_string_1 = '<html lang="en"><head><meta charset="UTF-8"></head>\
<b><h1>"Widget\'s Announcement Board"</h1></b><br/>\
<h2>"Announcements:"</h2><br/>'
html_string_2 = ""
for announced in Announcement.objects.all():
html_string_2 += "{} by {} {} published {}<br />:\
{}".format(announced.title, announced.author.first_name,
announced.author.last_name,
convert_to_localtime(announced.pub_datetime), announced.body)
for reacts in announced.reaction.all():
html_string_2 += "{}: {}".format(reacts.name, reacts.tally)
html_string_final = html_string_1 + html_string_2 + "</html>"
return HttpResponse(html_string_final)
# Create your views here. # Create your views here.
...@@ -17,6 +17,7 @@ import os ...@@ -17,6 +17,7 @@ import os
DIRNAME = os.path.abspath(os.path.dirname(__file__)) DIRNAME = os.path.abspath(os.path.dirname(__file__))
load_dotenv() load_dotenv()
# Build paths inside the project like this: BASE_DIR / 'subdir'. # Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent BASE_DIR = Path(__file__).resolve().parent.parent
...@@ -46,7 +47,6 @@ INSTALLED_APPS = [ ...@@ -46,7 +47,6 @@ INSTALLED_APPS = [
'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'tz_detect',
] ]
MIDDLEWARE = [ MIDDLEWARE = [
......
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