Commit db7d0ff8 authored by Jonathan Talbot's avatar Jonathan Talbot

lab3

parent 8529e972
from django.contrib import admin from django.contrib import admin
from .models import Announcement, Reaction from .models import Announcement, Reaction
class ReactionInline(admin.TabularInline):
model = Reaction
class AnnouncementAdmin(admin.ModelAdmin): class AnnouncementAdmin(admin.ModelAdmin):
model = Announcement model = Announcement
...@@ -15,28 +17,25 @@ class AnnouncementAdmin(admin.ModelAdmin): ...@@ -15,28 +17,25 @@ class AnnouncementAdmin(admin.ModelAdmin):
'announcement_title', 'announcement_title',
'announcement_body', 'announcement_body',
'authors_First_Name', 'authors_First_Name',
'authors_Last_Name', 'authors_Last_Name'
] ]
}) })
] ]
inlines = [ReactionInline,]
class ReactionAdmin(admin.ModelAdmin): class ReactionAdmin(admin.ModelAdmin):
model = Reaction model = Reaction
search_fields = ('reaction_name1', 'tally1', 'reaction_name2', 'tally2', 'reaction_name3', 'tally3') search_fields = ('reaction_name', 'tally')
list_display = ('reaction_name1', 'tally1', 'reaction_name2', 'tally2', 'reaction_name3', 'tally3') list_display = ('reaction_name', 'tally')
list_filter = ('reaction_name1', 'tally1', 'reaction_name2', 'tally2', 'reaction_name3', 'tally3') list_filter = ('reaction_name', 'tally')
fieldsets = [ fieldsets = [
('Department Data', { ('Reaction Data', {
'fields': [ 'fields': [
'reaction_name1', 'reaction_name',
'tally1', 'tally',
'reaction_name2',
'tally2',
'reaction_name3',
'tally3',
] ]
}), }),
] ]
......
# Generated by Django 4.0.3 on 2022-05-22 10:59 # Generated by Django 4.0.3 on 2022-05-23 00:24
import django.core.validators
from django.db import migrations, models from django.db import migrations, models
import django.db.models.deletion import django.db.models.deletion
...@@ -16,12 +17,8 @@ class Migration(migrations.Migration): ...@@ -16,12 +17,8 @@ class Migration(migrations.Migration):
name='Reaction', name='Reaction',
fields=[ fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('reaction_name1', models.CharField(max_length=15)), ('reaction_name', models.CharField(max_length=15)),
('tally1', models.IntegerField(max_length=3)), ('tally', models.CharField(max_length=3, validators=[django.core.validators.RegexValidator('^\\d{1,10}$')])),
('reaction_name2', models.CharField(max_length=15)),
('tally2', models.IntegerField(max_length=3)),
('reaction_name3', models.CharField(max_length=15)),
('tally3', models.IntegerField(max_length=3)),
], ],
), ),
migrations.CreateModel( migrations.CreateModel(
......
# Generated by Django 4.0.3 on 2022-05-22 11:01
import django.core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcements', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='reaction',
name='tally1',
field=models.CharField(max_length=3, validators=[django.core.validators.RegexValidator('^\\d{1,10}$')]),
),
migrations.AlterField(
model_name='reaction',
name='tally2',
field=models.CharField(max_length=3, validators=[django.core.validators.RegexValidator('^\\d{1,10}$')]),
),
migrations.AlterField(
model_name='reaction',
name='tally3',
field=models.CharField(max_length=3, validators=[django.core.validators.RegexValidator('^\\d{1,10}$')]),
),
]
# Generated by Django 4.0.3 on 2022-05-23 00:36
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('announcements', '0001_initial'),
]
operations = [
migrations.RemoveField(
model_name='announcement',
name='reaction',
),
migrations.AddField(
model_name='reaction',
name='announcement',
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, to='announcements.announcement'),
),
]
...@@ -2,14 +2,6 @@ from django.db import models ...@@ -2,14 +2,6 @@ from django.db import models
from django.urls import reverse from django.urls import reverse
from django.core.validators import RegexValidator from django.core.validators import RegexValidator
class Reaction(models.Model):
reaction_name1 = models.CharField(max_length=15)
tally1 = models.CharField(max_length=3, validators=[RegexValidator(r'^\d{1,10}$')])
reaction_name2 = models.CharField(max_length=15)
tally2 = models.CharField(max_length=3, validators=[RegexValidator(r'^\d{1,10}$')])
reaction_name3 = models.CharField(max_length=15)
tally3 = models.CharField(max_length=3, validators=[RegexValidator(r'^\d{1,10}$')])
class Announcement(models.Model): class Announcement(models.Model):
announcement_title= models.CharField(max_length=50) announcement_title= models.CharField(max_length=50)
...@@ -17,13 +9,17 @@ class Announcement(models.Model): ...@@ -17,13 +9,17 @@ class Announcement(models.Model):
authors_First_Name = models.CharField(max_length=20) authors_First_Name = models.CharField(max_length=20)
authors_Last_Name = models.CharField(max_length=20) authors_Last_Name = models.CharField(max_length=20)
pub_date = models.DateTimeField(auto_now_add=True, editable=False) pub_date = models.DateTimeField(auto_now_add=True, editable=False)
reaction = models.ForeignKey(Reaction, on_delete=models.CASCADE, default=1)
def get_absolute_url(self): def get_absolute_url(self):
return reverse('Announcement', args=[(self.full_announcement)]) return reverse('announcement-detail', args=[(self.pk)])
@property @property
def full_announcement(self): def full_announcement(self):
return '{} {} {}'.format(self.announcement_title, self.announcement_body, self.pub_date) return '{} {} {}'.format(self.announcement_title, self.announcement_body, self.pub_date)
class Reaction(models.Model):
reaction_name = models.CharField(max_length=15)
tally = models.CharField(max_length=3, validators=[RegexValidator(r'^\d{1,10}$')])
announcement = models.ForeignKey(Announcement, on_delete=models.CASCADE, default=1)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Announcement Detail</title>
</head>
<body>
<h2>{{ announcement.announcement_title }}</h2>
<h3>by {{ announcement.First_Name }} {{ announcement.Last_Name }}, {{ announcement.pub_date|date:"d/m/yy" }}</h3>
<p>{{ announcement.announcement_body }}</p>
<hr>
{% for reaction in reactions %}
{%if reaction.announcement == announcement %}
<p>{{ reaction.reaction_name }} : {{ reaction.tally }}</p>
{%endif%}
{% endfor %}
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Announcements List</title>
</head>
<body>
<h1>Important Announcements:</h1>
{% for announcement in announcements %}
<a href="{{ announcement.get_absolute_url }}">{{ announcement.announcement_title }} by {{ announcement.authors_First_Name }} {{ announcement.authors_Last_Name }} dated {{ announcement.pub_date }}</a>
<br>
{% endfor %}
</body>
</html>
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import announcements from .views import AnnouncementListView, AnnouncementDetailView
urlpatterns = [ urlpatterns = [
path('', announcements, name='Announcement Board') path('', AnnouncementListView.as_view(), name='announcement-list'),
path('<int:pk>/details', AnnouncementDetailView, name='announcement-detail'),
] ]
\ No newline at end of file
from django.http import HttpResponse from django.http import HttpResponse
from django.shortcuts import render, get_object_or_404
from django.views import View
from django.views.generic.detail import DetailView
from django.views.generic.list import ListView
from .models import Announcement, Reaction from .models import Announcement, Reaction
def announcements(request): # def announcements(request):
announcements = Announcement.objects.all() # announcements = Announcement.objects.all()
reactions = Reaction.objects.all() # reactions = Reaction.objects.all()
output = "ANNOUNCEMENTS:\n" + "\n".join( # output = "ANNOUNCEMENTS:\n" + "\n".join(
['{} by {} {} dated by {}: \n {} \n'.format( # ['{} by {} {} dated by {}: \n {} \n'.format(
str(announcement.announcement_title), # str(announcement.announcement_title),
str(announcement.authors_First_Name), # str(announcement.authors_First_Name),
str(announcement.authors_Last_Name), # str(announcement.authors_Last_Name),
str(announcement.pub_date), # str(announcement.pub_date),
str(announcement.announcement_body), # str(announcement.announcement_body),
) # )
# for announcement in announcements]
# ) + "REACTIONS:\n" + "\n".join(
# ['{}: {} \n {}: {} \n {}: {}'.format(
# str(reaction.reaction_name1),
# str(reaction.tally1),
# str(reaction.reaction_name2),
# str(reaction.tally2),
# str(reaction.reaction_name3),
# str(reaction.tally3),
# )
for announcement in announcements] # for reaction in reactions]
) + "REACTIONS:\n" + "\n".join( # )
['{}: {} \n {}: {} \n {}: {}'.format( # return HttpResponse(output, content_type="text/plain")
str(reaction.reaction_name1),
str(reaction.tally1),
str(reaction.reaction_name2),
str(reaction.tally2),
str(reaction.reaction_name3),
str(reaction.tally3),
)
for reaction in reactions] class AnnouncementListView(View):
) def get(self, request):
return HttpResponse(output, content_type="text/plain") announcements = Announcement.objects.order_by('pub_date').all()
context = {
'announcements': announcements,
}
return render(request, 'announcements/announcements_list.html', context)
def AnnouncementDetailView(request, pk):
announcement = get_object_or_404(Announcement, pk=pk)
reactions = Reaction.objects.all()
return render(request, 'announcements/announcements_detail.html', {
'announcement': announcement,
'reactions': reactions
})
No preview for this file type
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