Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
midterm_casanatics
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
John Aidan Vincent M. Ng
midterm_casanatics
Commits
1e4cf00e
Commit
1e4cf00e
authored
Mar 04, 2023
by
justin
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'announcements_wip'
parents
a14fc9da
7f83c099
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
114 additions
and
3 deletions
+114
-3
admin.py
widget_casanatics/announcement_board/admin.py
+49
-0
models.py
widget_casanatics/announcement_board/models.py
+23
-1
urls.py
widget_casanatics/announcement_board/urls.py
+6
-0
views.py
widget_casanatics/announcement_board/views.py
+36
-2
No files found.
widget_casanatics/announcement_board/admin.py
View file @
1e4cf00e
from
django.contrib
import
admin
from
django.contrib
import
admin
from
.models
import
Announcement
,
Reaction
# Register your models here.
# Register your models here.
# show reactions in page for announcements
class
ReactionInline
(
admin
.
TabularInline
):
model
=
Reaction
# announcement admin panel
class
AnnouncementAdmin
(
admin
.
ModelAdmin
):
model
=
Announcement
# display title, body, datetime, author
list_display
=
(
"title"
,
"get_author"
,
"pub_datetime"
,
"body"
)
# search by title or body
search_fields
=
(
"title"
,
"body"
)
# filter by author
list_filter
=
(
"get_author"
)
def
get_author
(
self
,
obj
):
return
obj
.
author
.
displayName
()
get_author
.
short_description
=
"Author Name"
get_author
.
admin_order_field
=
"author"
inlines
=
[
ReactionInline
]
class
ReactionAdmin
(
admin
.
ModelAdmin
):
# display reaction name, tally, announcement
list_display
=
(
"reaction_name"
,
"tally"
,
"get_name"
)
# search by announcement name
search_fields
=
(
"get_name"
)
# filter by announcement
list_filter
=
(
"get_name"
)
def
get_name
(
self
,
obj
):
return
obj
.
announcement
.
title
# title of column for get_name
get_name
.
short_description
=
"Announcement Title"
# sorting by announcement
get_name
.
admin_order_field
=
"announcement"
admin
.
site
.
register
(
Announcement
,
AnnouncementAdmin
)
admin
.
site
.
register
(
Reaction
,
ReactionAdmin
)
\ No newline at end of file
widget_casanatics/announcement_board/models.py
View file @
1e4cf00e
from
django.db
import
models
from
django.db
import
models
from
dashboard.models
import
WidgetUser
# Create your models here.
class
Announcement
(
models
.
Model
):
title
=
models
.
CharField
(
max_length
=
50
)
body
=
models
.
CharField
(
max_length
=
500
)
author
=
models
.
ForeignKey
(
WidgetUser
,
on_delete
=
models
.
CASCADE
)
pub_datetime
=
models
.
DateTimeField
(
"Date and Time Published"
,
auto_now_add
=
True
,
)
def
__str__
(
self
):
return
self
.
title
class
Reaction
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
50
)
tally
=
models
.
IntegerField
(
default
=
0
)
announcement
=
models
.
ForeignKey
(
Announcement
,
on_delete
=
models
.
CASCADE
)
def
__str__
(
self
):
return
self
.
name
widget_casanatics/announcement_board/urls.py
0 → 100644
View file @
1e4cf00e
from
django.urls
import
path
from
.
import
views
urlpatterns
=
[
path
(
''
,
views
.
announcements
,
name
=
"announcements"
),
]
\ No newline at end of file
widget_casanatics/announcement_board/views.py
View file @
1e4cf00e
from
django.shortcuts
import
render
from
django.http
import
HttpResponse
from
.models
import
Announcement
,
Reaction
from
datetime
import
datetime
# Create your views here.
def
announcements
(
request
):
# retrieve all announcement entries in a list
announcementList
=
Announcement
.
objects
.
all
()
# build response string
response
=
"Widget's Announcement Board <br><br>"
+
"Announcements:"
for
announcement
in
announcementList
:
# string formatting for date-time
datetime
=
announcement
.
pub_datetime
.
strftime
(
"
%
m/
%
d/
%
Y,
%
I:
%
M
%
p"
)
# list of reactions for announcement
reactions
=
announcement
.
reaction_set
.
all
()
# response proper
response
=
(
response
+
"<br>"
+
announcement
.
title
+
" by "
+
announcement
.
author
.
displayName
()
+
" published "
+
datetime
+
": <br>"
)
response
=
response
+
announcement
.
body
+
"<br>"
# for each reaction, add line with reaction and tally
for
reaction
in
reactions
:
response
=
response
+
reaction
.
name
+
": "
+
str
(
reaction
.
tally
)
+
"<br>"
return
HttpResponse
(
response
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment