Commit 910c0e41 authored by Neal Luigi D. Rodriguez's avatar Neal Luigi D. Rodriguez

Merge branch 'announcementv2'

parents 9f35145c 0aa3ee67
from django import forms
from .models import Announcement, Reaction
class AnnouncementForm(forms.ModelForm):
class meta:
model = Announcement
fields = ['title','body','author','pub_datetime']
class ReactionForm(forms.ModelForm):
class meta:
model = Reaction
fields = ['name','tally','announcement']
from django.db import models from django.db import models
from Dashboard.models import WidgetUser from Dashboard.models import WidgetUser
from django.urls import reverse
class Announcement(models.Model): class Announcement(models.Model):
title = models.CharField(max_length=100) title = models.CharField(max_length=100)
...@@ -9,7 +10,10 @@ class Announcement(models.Model): ...@@ -9,7 +10,10 @@ class Announcement(models.Model):
def __str__(self): def __str__(self):
return self.title return self.title
def get_absolute_url(self):
return reverse('Announcement_Board:announcements-details', kwargs={'pk': self.pk})
class Reaction(models.Model): class Reaction(models.Model):
name_choices = ( name_choices = (
('like','like'), ('like','like'),
...@@ -18,9 +22,11 @@ class Reaction(models.Model): ...@@ -18,9 +22,11 @@ class Reaction(models.Model):
) )
name = models.CharField(max_length=100, choices=name_choices) name = models.CharField(max_length=100, choices=name_choices)
tally = models.IntegerField() tally = models.IntegerField()
announcement = models.ForeignKey(Announcement, on_delete = models.PROTECT) announcement = models.ForeignKey(
Announcement,
on_delete=models.PROTECT,
related_name='reactions')
def __str__(self): def __str__(self):
return self.name return self.name
{% extends 'base.html' %}
{% load static %}
{% block title %} Add Announcement {% endblock %}
{% block content %}
<h1>Add a new announcement</h1>
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Add Announcement">
</form>
{% endblock %}
{% extends 'base.html' %}
{% load static %}
{% block title %}{{ object.title }}{% endblock %}
{% block content %}
<h1>{{ object.title }}</h1>
<h2>by {{ object.author }}</h2>
<h3>{{ object.pub_datetime }}</h3>
<h4>{{ object.body }}</h4>
<ul>
{% for reaction in object.reactions.all %}
{{reaction.name}}: {{reaction.tally}}<br>
{% endfor %}
</ul>
<button onclick="window.location.href='../../../announcements/{{object.id}}/edit/';">
Edit Announcement
</button>
{% endblock %}
{% extends 'base.html' %}
{% load static %}
{% block title %} Edit Announcement {% endblock %}
{% block content %}
<h1>Edit announcement</h1>
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Save Changes to Announcement">
</form>
{% endblock %}
{% extends 'base.html' %}
{% load static %}
{% block title %} Widget's Announcement Board {% endblock %}
{% block content %}
<h1>Welcome to Widget's Announcement Board!</h1>
<h2>Announcements:</h2>
<ul>
{% for object in announcements %}
<li>
<a href="{{ object.get_absolute_url }}">{{ object.title }} by {{ object.author }}</a>
</li>
{% endfor %}
</ul>
<button onclick="window.location.href='../announcements/add/';">
New Announcement
</button>
<p>
<a href="http://127.0.0.1:8000/Dashboard/">Dashboard</a><br>
<a href="http://127.0.0.1:8000/Forum/">Forum</a><br>
<a href="http://127.0.0.1:8000/Assignments/">Assignments</a><br>
<a href="http://127.0.0.1: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 .views import (index,AnnouncementView,AnnouncementsDetailView,
AnnouncementsCreateView,
AnnouncementsUpdateView)
urlpatterns = [ urlpatterns = [
path('', index, name = 'index'), path('', index, name='index'),
path('announcements/', AnnouncementView, name="Announcements"),
path('announcements/<pk>/details/', AnnouncementsDetailView.as_view(), name='announcements-details'),
path('announcements/add/', AnnouncementsCreateView.as_view(), name='announcements-add'),
path('announcements/<pk>/edit/', AnnouncementsUpdateView.as_view(), name='announcements-edit')
] ]
app_name = "AnnouncementBoard" app_name = "Announcement_Board"
\ No newline at end of file
from django.shortcuts import render from django.shortcuts import render
from django.shortcuts import HttpResponse from django.shortcuts import HttpResponse
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, UpdateView
from .models import Announcement, Reaction from .models import Announcement, Reaction
...@@ -25,8 +27,27 @@ def index(request): ...@@ -25,8 +27,27 @@ def index(request):
like, love, angry, like, love, angry,
) )
return_string += '</ul>' return_string += '</ul>'
html_string = '<html><head>Widget’s Announcement Board<br><br>Announcements:</head><body>{}</body><html>'.format(return_string) html_string = '<html><head>Widget’s Announcement Board<br><br>Announcements:</head><body>{}</body><html>'.format(return_string)
return HttpResponse(html_string) return HttpResponse(html_string)
def AnnouncementView(request):
announcements = Announcement.objects.all()
context = {'announcements': announcements}
return render(request, 'announcements/announcements.html', context)
class AnnouncementsDetailView(DetailView):
model = Announcement
template_name = 'announcements/announcements-details.html'
class AnnouncementsCreateView(CreateView):
model = Announcement
fields = '__all__'
template_name = 'announcements/announcements-add.html'
class AnnouncementsUpdateView(UpdateView):
model = Announcement
fields = '__all__'
template_name = 'announcements/announcements-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