Commit 8b41b926 authored by Michael Lopez's avatar Michael Lopez

added announcement edit and create view on views.py

parent 8a8d3f8f
from django.urls import path from django.urls import path
from .views import AnnouncementDetailView from .views import *
from .views import pageview
urlpatterns = [ urlpatterns = [
path('', pageview, name='pageview'), path('', pageview, name='pageview'),
path('<int:pk>/details/', AnnouncementDetailView.as_view(), name="announcement_details"), path('<int:pk>/details/', AnnouncementDetailView.as_view(), name="announcement_details"),
path("add/", )
] ]
app_name = "announcements" app_name = "announcements"
\ No newline at end of file
import pytz # need to do "pip install pytz" for this to be activated import pytz # need to do "pip install pytz" for this to be activated
from .forms import *
from .models import Announcement, Reaction from .models import Announcement, Reaction
from django.utils import timezone from django.utils import timezone
from django.views.generic import ListView, DetailView from django.views.generic import ListView, DetailView, CreateView, UpdateView
from django.db.models import Sum from django.db.models import Sum
from django.shortcuts import render from django.shortcuts import render
from django.urls import reverse
def pageview(request): def pageview(request):
model_object = Announcement.objects.order_by("-pub_datetime") model_object = Announcement.objects.order_by("-pub_datetime")
return render(request, "announcements/announcements.html", context={"announcement_list":model_object}) return render(request, "announcements/announcements.html", context={"announcement_list":model_object})
def UTC_to_PHTZ(utctime): def UTC_to_PHTZ(utctime):
ph_tz = "%m/%d/%Y, %I:%M %p" ph_tz = "%m/%d/%Y, %I:%M %p"
utc = utctime.replace(tzinfo=pytz.UTC) utc = utctime.replace(tzinfo=pytz.UTC)
local_tz = utc.astimezone(timezone.get_current_timezone) local_tz = utc.astimezone(timezone.get_current_timezone)
return local_tz.strftime(ph_tz) return local_tz.strftime(ph_tz)
class AnnouncementListView(ListView): class AnnouncementListView(ListView):
model = Announcement model = Announcement
template_name = "announcements/announcements.html" template_name = "announcements/announcements.html"
...@@ -35,4 +37,25 @@ class AnnouncementDetailView(DetailView): ...@@ -35,4 +37,25 @@ class AnnouncementDetailView(DetailView):
context['pubpost_datetime'] = UTC_to_PHTZ(announcement.pub_datetime) context['pubpost_datetime'] = UTC_to_PHTZ(announcement.pub_datetime)
return context return context
class AnnouncementCreateView(CreateView):
model = Announcement
form_class = AddAnnouncement
template_name = "announcements/announcement-add.html"
def form_valid(self, form):
response = super().form_valid(form)
form.save()
return response
def get_success_url(self):
return reverse("announcement_board:announcement_details", args=(self.object.pk,))
class AnnouncementEditView(UpdateView):
model = Announcement
form_class = AddAnnouncement
template_name = "announcements/announcement-edit.html"
def form_valid(self, form):
response = super().form_valid(form)
form.save()
return response
def get_success_url(self):
return reverse("announcement_board:announcement_details", args=(self.object.pk,))
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