Commit a939c3d1 authored by Raul Jarod Conanan's avatar Raul Jarod Conanan

Merge branch '3-Announcementsv2' into 'dev'

3 announcementsv2

See merge request !21
parents 7811df17 3fc18139
# Generated by Django 3.2 on 2023-05-13 14:43
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('announcements', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='reaction',
name='announcement',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='react', to='announcements.announcement'),
),
]
......@@ -19,6 +19,6 @@ class Reaction(models.Model):
name = models.CharField(max_length=5, default=LIKE, null=True, blank=True)
tally = models.IntegerField(default=0 ,null=True, blank=True)
announcement = models.ForeignKey(Announcement, on_delete=models.CASCADE, null=True)
announcement = models.ForeignKey(Announcement, related_name="react", on_delete=models.CASCADE, null=True)
# Create your models here.
{% extends 'base.html' %}
{% load static %}
{% block content %}
<title>Add Announcement</title>
<h1>Add a new announcement:</h1>
<form action="" method=POST>
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Add Announcement">
</form>
{% endblock content %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block content %}
<title>{{ announce.title }}</title>
<h1>{{ announce.title }}</h1>
<h2>by {{ announce.author.first_name }} {{ announce.author.last_name }}</h2>
<p>{{ announce.pub_datetime|date:'m/d/Y, h:i A' }}</p>
<p>{{ announce.body }}</p>
<p>
{% for react in announce.react.all %}
{{ react.name }}: {{ react.tally }}<br>
{% endfor %}
</p>
<a href = "/announcements/{{ announce.pk }}/edit/"><button value="">Edit Announcement</button></a>
{% endblock content %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block content %}
<title>Edit Announcement</title>
<h1>Edit announcement:</h1>
<form action="" method=POST>
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save Changes to Announcement">
</form>
{% endblock content %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block content %}
<title>Widget's Announcement Board</title>
<h1>Welcome to Widget's Announcement Board!</h1>
<h3>Announcements:</h3>
{% for announce in announcement %}
<a href = "{{ announce.pk }}/details/">{{ announce.title }} by {{ announce.author.first_name }} {{ announce.author.last_name }}</a><br>
{% endfor %} <br>
<a href="add/"><button value="click here">Add Announcement</button></a><br><br>
<a href = "/Dashboard/">Dashboard</a><br>
<a href = "/forum/">Forum</a><br>
<a href = "/assignments/">Assignments</a><br>
<a href = "/widget_Calendar/">Calendar</a>
{% endblock content %}
\ No newline at end of file
from django.urls import path
from .views import index
from .views import index, AnnouncementDetailView, AnnouncementAddView, AnnouncementEditView
urlpatterns = [
path('', index, name='index'),
path('announcements/', index, name='index'),
path('announcements/<int:pk>/details/', AnnouncementDetailView.as_view(), name='announcementdetailview'),
path('announcements/add/', AnnouncementAddView.as_view(), name='announcement-add'),
path('announcements/<int:pk>/edit/', AnnouncementEditView.as_view(), name='announcement-edit'),
]
app_name = "announcements"
\ No newline at end of file
from django.shortcuts import render
from django.http import HttpResponse
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
from django.urls import reverse
from .models import Announcement, Reaction
import pytz
from django.utils import timezone
def convert_to_localtime(utctime):
format = '%d/%m/%Y %I:%M %p'
format = '%m/%d/%Y %I:%M %p'
utc = utctime.replace(tzinfo=pytz.UTC)
localtz = utc.astimezone(timezone.get_current_timezone())
return localtz.strftime(format)
def index(request):
html_string_1 = '<html lang="en"><head><meta charset="UTF-8"></head>\
<b><h1>Widget\'s Announcement Board</h1></b>\
<h2>Announcements:</h2><br/>'
announcement = Announcement.objects.all().order_by('-pub_datetime')
context = {
'announcement': announcement
}
html_string_2 = ""
for announced in Announcement.objects.all():
html_string_2 += "{} by {} {} published {}:<br />\
{}<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_set.all():
html_string_2 += "{}: {}<br/>".format(reacts.name, reacts.tally)
html_string_2 += '<br/>'
html_string_final = html_string_1 + html_string_2 + "</html>"
return render(request, 'announcements/announcements.html', context)
class AnnouncementDetailView(DetailView):
model = Announcement
template_name = 'announcements/announcement-details.html'
queryset = Announcement.objects.all()
context_object_name = 'announce'
class AnnouncementAddView(CreateView):
model = Announcement
fields = ['title', 'body', 'author']
template_name = 'announcements/announcement-add.html'
return HttpResponse(html_string_final)
def get_success_url(self):
return reverse('announcement:announcementdetailview', kwargs={'pk': self.object.id},
current_app=self.request.resolver_match.namespace)
class AnnouncementEditView(UpdateView):
model = Announcement
template_name = 'announcements/announcement-edit.html'
fields = ['title', 'body', 'author']
def get_success_url(self):
return reverse('announcement:announcementdetailview', kwargs={'pk': self.object.id},
current_app=self.request.resolver_match.namespace)
# Create your views here.
SECRET_KEY = 'django-insecure-t*s#_++5=ze%3#*ns6vcmt8a5bw6249en-!ek7*#3=p-dkhl_f'
\ No newline at end of file
......@@ -18,7 +18,7 @@ from django.urls import include, path
urlpatterns = [
path('announcements/', include('announcements.urls', namespace="announcements")),
path('', include('announcements.urls', namespace="announcements")),
path('widget_Calendar/', include('widget_Calendar.urls', namespace="widget_Calendar")),
path('', include('Dashboard.urls', namespace="Dashboard")),
path('admin/', admin.site.urls),
......
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