Commit 67a18768 authored by John Raymon C Yu's avatar John Raymon C Yu

Yu edited assignments

parents 3f6e2545 eb72774a
from django.forms import ModelForm
from .models import Announcement
class AnnouncementForm(ModelForm):
class Meta:
model = Announcement
fields = ["announcement_title", "announcement_body", "author", "pub_date"]
\ No newline at end of file
# Generated by Django 3.2.12 on 2022-05-26 11:44
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('announcements', '0017_announcement_announce_image'),
]
operations = [
migrations.AlterField(
model_name='announcement',
name='announce_image',
field=models.ImageField(blank=True, default='faceless.png', null=True, upload_to=''),
),
]
...@@ -8,7 +8,7 @@ class Announcement(models.Model): ...@@ -8,7 +8,7 @@ class Announcement(models.Model):
announcement_body = models.CharField(max_length=500) announcement_body = models.CharField(max_length=500)
pub_date = models.DateTimeField("date published") pub_date = models.DateTimeField("date published")
reaction_list = models.ForeignKey('Reaction', on_delete=models.CASCADE, related_name='+', default=1) reaction_list = models.ForeignKey('Reaction', on_delete=models.CASCADE, related_name='+', default=1)
announce_image = models.ImageField(default="faceless_9ltCukV.png", null=True, blank=True) announce_image = models.ImageField(default="faceless.png", null=True, blank=True)
def __str__(self): def __str__(self):
return self.announcement_title return self.announcement_title
......
...@@ -11,4 +11,10 @@ ...@@ -11,4 +11,10 @@
{% else %} {% else %}
<p> No announcements here! </p> <p> No announcements here! </p>
{% endif %} {% endif %}
<form action="add">
<input type="Submit" value="Make an announcement!">
</form>
{% endblock %} {% endblock %}
\ No newline at end of file
{% extends "announcement/base.html" %}
{% block content %}
<h1> NEW ANNOUNCEMENT </h1>
<form action="{% url 'add' %}" method="POST">
{% csrf_token %}
<h3> Title: </h3> <input type="text" name="Title">
<h3> Body: </h3> <textarea rows="5" cols="60" name="Body" placeholder="Type your announcement here..."> </textarea>
<br>
<br>
<select class="custom-select mb-4" name="Author">
<option selected> Choose an author </option>
{% for author in Author %}
<option value="{{ author.pk }}"> {{ author.first_name }} {{ author.last_name }} </option>
{% endfor %}
</select>
<br>
<br>
<button class="button" type="submit"> Save Announcement </button>
</form>
<br>
<br>
<a href="{% url 'showAnnouncements' %}"> Go back </a>
{% endblock %}
\ No newline at end of file
{% extends "announcement/base.html" %}
{% block content %}
<form method="POST" action = "{% url 'announcements:newannounce' %}">
{% csrf_token %}
{{ announce_form.as_p }}
<button class="button" type="submit"> Save </button>
</form>
{% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from . import views from . import views
from django.conf import settings
from .views import showAnnouncements
urlpatterns = [ urlpatterns = [
path('', views.index, name='index'), path('', showAnnouncements.as_view(), name='showAnnouncements'),
path("<int:announcement_id>", views.detail, name="detail"), path("<int:announcement_id>/", views.detail, name="detail"),
path("add/", views.add, name="add"),
#path("add/newAnnounce", views.newAnnounce, name="newAnnounce")
] ]
\ No newline at end of file
from django.http import Http404
from django.shortcuts import render
from .models import Announcement, Reaction
from django.http import Http404, HttpResponse
from django.shortcuts import render, redirect
from .models import Announcement, Reaction
from homepage.models import WidgetUser
import datetime
from .forms import AnnouncementForm
from django.views import View
# Create your views here. # Create your views here.
#def show_announcements(request):
#announcement = Announcement.objects.all()
#return render(request, 'announcement/announcement_board.html', {'announcements': announcement})
def index(request): def index(request):
announce_list = Announcement.objects.order_by("pub_date") announce_list = Announcement.objects.order_by("pub_date")
...@@ -16,6 +16,30 @@ def index(request): ...@@ -16,6 +16,30 @@ def index(request):
} }
return render(request, "announcement/announcement_page.html", context) return render(request, "announcement/announcement_page.html", context)
class showAnnouncements(View):
def get(self, request):
announce_list = Announcement.objects.order_by("pub_date")
context = {
"announce_list": announce_list,
}
return render(request, "announcement/announcement_page.html", context)
def add(request):
author = WidgetUser.objects.all()
context = {
"Author": author,
}
if request.method == "POST":
title = request.POST.get("Title")
body = request.POST.get("Body")
author = request.POST.get("Author")
pub_date = datetime.datetime.now()
announcement = Announcement.objects.create(author_id = author, announcement_title = title, announcement_body = body, pub_date = pub_date)
return redirect('add')
return render(request, "announcement/create_announcement.html", context)
def detail(request, announcement_id): def detail(request, announcement_id):
try: try:
announce = Announcement.objects.get(pk=announcement_id) announce = Announcement.objects.get(pk=announcement_id)
...@@ -24,3 +48,4 @@ def detail(request, announcement_id): ...@@ -24,3 +48,4 @@ def detail(request, announcement_id):
reacts = Reaction.objects.filter(announcement_id=announce).order_by("reaction_name") reacts = Reaction.objects.filter(announcement_id=announce).order_by("reaction_name")
return render(request, "announcement/announcement_board.html", {"announce": announce, "reacts": reacts}) return render(request, "announcement/announcement_board.html", {"announce": announce, "reacts": reacts})
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