Commit f677ebe2 authored by Anthony Bicomong's avatar Anthony Bicomong

announcementsv2

parent 56e03e5d
from django.forms import ModelForm
from .models import Announcement
class AnnouncementForm(ModelForm):
class Meta:
model = Announcement
fields = '__all__'
# Generated by Django 4.2.1 on 2023-05-04 04:57
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcements', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='pub_datetime',
field=models.DateTimeField(auto_now_add=True),
),
]
from django.db import models from django.db import models
from django.urls import reverse
class Announcement(models.Model): class Announcement(models.Model):
title = models.CharField(max_length = 50) title = models.CharField(max_length = 50)
body = models.TextField() body = models.TextField()
author = models.ForeignKey("dashboard.WidgetUser", on_delete=models.CASCADE) author = models.ForeignKey("dashboard.WidgetUser", on_delete=models.CASCADE)
pub_datetime = models.DateTimeField() pub_datetime = models.DateTimeField(auto_now_add=True)
def __str__(self): def __str__(self):
return self.title return self.title
def datetime(self):
return self.pub_datetime.strftime("%m/%d/%Y, %I:%M %p")
def get_absolute_url(self):
return reverse("announcement-details", kwargs={'pk': self.pk})
class Reaction(models.Model): class Reaction(models.Model):
Like = 'Like' Like = 'Like'
Love = 'Love' Love = 'Love'
......
{% extends 'base.html' %}
{% load static %}
{% block title %}Add Announcement{% endblock %}
{% block content %}
<p>Add a new announcement:</p>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="Submit" value="Add Announcement">
</form>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}{{ object.title }}{% endblock %}
{% block heading %}{{ object.title }}{% endblock %}
{% block content %}
<p>by {{ object.author }}</p>
<p>{{ object.datetime }}</p>
<p>{{ object.body }}<br></p>
{% for reac in object.reaction_set.all %}
<p>{{ reac.name }}: {{ reac.tally }}</p>
{% endfor %}
<input type="button" value="Edit Announcement" onclick="location.href='{% url 'announcement-edit' object.pk %}'"/>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}Edit Announcement{% endblock %}
{% block content %}
<p>Edit Announcement:</p>
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="Submit" value="Save Changes to Announcement">
</form>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% load static %}
{% block title %}Widget's Announcement Board{% endblock %}
{% block heading %}Welcome to Widget's Announcement Board!{% endblock %}
{% block content %}
<p>Announcements:</p>
{% for ann in announcements %}
<p><a href="{% url 'announcement-details' ann.pk %}">{{ ann.title }} by {{ ann.author }}</a></p>
{% endfor %}
<input type="button" value="New Announcement" onclick="location.href='{% url 'announcement-add' %}'"/>
{% endblock %}
{% block links %}
<p><a href="http://localhost:8000/dashboard/">Dashboard</a></p>
<p><a href="http://localhost:8000/forum/">Forum</a></p>
<p><a href="http://localhost:8000/assignments/">Assignment</a></p>
<p><a href="http://localhost:8000/calendar/">Calendar</a></p>
{% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import index from . import views
from .views import (AnnouncementDetailView, AnnouncementCreateView, AnnouncementUpdateView)
urlpatterns = [ urlpatterns = [
path('', index, name='index'), path('', views.index, name='index'),
] path('announcements/<int:pk>/details/', AnnouncementDetailView.as_view(), name="announcement-details"),
path('announcements/add/', AnnouncementCreateView.as_view(), name="announcement-add"),
app_name = "announcements" path('announcements/<int:pk>/edit/', AnnouncementUpdateView.as_view(), name="announcement-edit"),
]
\ No newline at end of file
from django.http import HttpResponse from django.shortcuts import render
from .models import Announcement, Reaction from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
from .models import Announcement
def index(request): def index(request):
announcements = Announcement.objects.all() return render(request, 'announcements/announcements.html', {'announcements': Announcement.objects.order_by('-pub_datetime')})
reactions = Reaction.objects.all()
response = "Widget's Announcement Board<br><br>Announcements:<br>" class AnnouncementDetailView(DetailView):
for announcement in announcements: model = Announcement
response += "{} by {} published {}:<br>{}<br>".format( template_name = 'announcements/announcement-details.html'
announcement.title,
announcement.author,
announcement.pub_datetime.strftime("%m/%d/%Y, %I:%M %p"),
announcement.body
)
for reaction in reactions: class AnnouncementCreateView(CreateView):
if reaction.announcement == announcement: model = Announcement
response += "{}: {}<br>".format( fields = '__all__'
reaction.name, template_name = 'announcements/announcement-add.html'
str(reaction.tally)
)
response += "<br>"
return HttpResponse(response)
class AnnouncementUpdateView(UpdateView):
model = Announcement
fields = '__all__'
template_name = 'announcements/announcement-edit.html'
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