Commit b5b74664 authored by Ross Batacan's avatar Ross Batacan

Update views.py: successfully stores DateTime when an announcement is...

Update views.py: successfully stores DateTime when an announcement is published, and successfuly displays on announcement-details.html as well
parent cbf7cd07
...@@ -3,11 +3,12 @@ from dashboard.models import WidgetUser ...@@ -3,11 +3,12 @@ from dashboard.models import WidgetUser
from django.urls import reverse from django.urls import reverse
from django.utils import timezone from django.utils import timezone
# Create your models here. # Create your models here.
# please insert foreign key from dashboard application on announcement_author # please insert foreign key from dashboard application on announcement_author
class Announcement(models.Model): class Announcement(models.Model):
announcement_title = models.CharField(max_length=50, unique=True, null=True, verbose_name="Title" ) announcement_title = models.CharField(max_length=50, unique=True, null=True, verbose_name="Title")
announcement_body = models.TextField(max_length=250, unique=True, null=True, verbose_name="Body") announcement_body = models.TextField(max_length=250, unique=True, null=True, verbose_name="Body")
announcement_author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, null=True, verbose_name="Author") announcement_author = models.ForeignKey(WidgetUser, on_delete=models.CASCADE, null=True, verbose_name="Author")
announcement_pub_datetime = models.DateTimeField(null=True, ) announcement_pub_datetime = models.DateTimeField(null=True, )
...@@ -15,12 +16,14 @@ class Announcement(models.Model): ...@@ -15,12 +16,14 @@ class Announcement(models.Model):
def __str__(self): def __str__(self):
return self.announcement_title return self.announcement_title
def save(self, *args, **kwargs):
if not self.pk:
self.announcement_pub_datetime = timezone.now()
return super().save(*args, **kwargs)
def get_absolute_url(self): def get_absolute_url(self):
return reverse('announcements:announcement-details', kwargs={'pk': self.pk}) return reverse('announcements:announcement-details', kwargs={'pk': self.pk})
def save(self):
self.announcement_pub_datetime = timezone.now()
reaction_choices = ( reaction_choices = (
('Like', 'Like'), ('Like', 'Like'),
...@@ -30,10 +33,10 @@ reaction_choices = ( ...@@ -30,10 +33,10 @@ reaction_choices = (
class Reaction(models.Model): class Reaction(models.Model):
reaction_name = models.CharField(max_length=50, blank=True, choices=reaction_choices, null=True,) reaction_name = models.CharField(max_length=50, blank=True, choices=reaction_choices, null=True, )
reaction_tally = models.IntegerField(null=True, default=0, ) reaction_tally = models.IntegerField(null=True, default=0, )
reaction_announcement = models.ForeignKey(Announcement, on_delete=models.CASCADE, null=True, ) reaction_announcement = models.ForeignKey(Announcement, on_delete=models.CASCADE, null=True, )
def __str__(self): def __str__(self):
return self.reaction_name return self.reaction_name
...@@ -7,9 +7,8 @@ from .models import Announcement, Reaction ...@@ -7,9 +7,8 @@ from .models import Announcement, Reaction
# Create your views here. # Create your views here.
def Announcements(request): def Announcements(request):
announcements = Announcement.objects.order_by("announcement_pub_datetime") announcements = Announcement.objects.order_by("-announcement_pub_datetime")
context = { context = {
"announcements": announcements, "announcements": announcements,
} }
......
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