-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use CharField & Remove "Banner" (#166)
* fix(announcement): use char instead of integer for announcement_type * lint: lol I didn't even have flake8 installed * update Announcement serializer * unpin black version + lint * isort --------- Co-authored-by: Rohan Moniz <rohan.moniz@gmail.com>
- Loading branch information
Showing
11 changed files
with
579 additions
and
544 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,6 @@ | ||
from django.conf import settings | ||
from announcements.models import Announcement, Audience | ||
from django.contrib import admin | ||
from django.contrib.auth.models import Permission | ||
from django.shortcuts import redirect | ||
from django.urls import reverse | ||
|
||
from announcements.models import ( | ||
Audience, | ||
Announcement | ||
) | ||
|
||
admin.site.register(Audience) | ||
admin.site.register(Announcement) |
15 changes: 10 additions & 5 deletions
15
backend/announcements/management/commands/populate_audiences.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
from django.core.management import BaseCommand | ||
|
||
from announcements.models import Audience | ||
from django.core.management import BaseCommand | ||
|
||
|
||
class Command(BaseCommand): | ||
|
||
def handle(self, *args, **options): | ||
for x in ["MOBILE", "OHQ", "CLUBS", "COURSE_PLAN", "COURSE_REVIEW", "COURSE_ALERT"]: | ||
Audience.objects.get_or_create(name=x) | ||
for x in [ | ||
"MOBILE", | ||
"OHQ", | ||
"CLUBS", | ||
"COURSE_PLAN", | ||
"COURSE_REVIEW", | ||
"COURSE_ALERT", | ||
]: | ||
Audience.objects.get_or_create(name=x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
backend/announcements/migrations/0002_alter_announcement_announcement_type.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Generated by Django 3.2.21 on 2023-10-26 18:28 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("announcements", "0001_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="announcement", | ||
name="announcement_type", | ||
field=models.CharField( | ||
choices=[("NOTICE", "Notice"), ("ISSUE", "Issue")], | ||
default="NOTICE", | ||
max_length=20, | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,57 @@ | ||
from rest_framework import serializers | ||
from announcements.models import Announcement, Audience | ||
from rest_framework import serializers | ||
|
||
class AudienceSerializer(serializers.ModelSerializer): | ||
|
||
class AudienceSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Audience | ||
fields = "__all__" | ||
fields = ("name",) | ||
|
||
|
||
class AnnouncementSerializer(serializers.ModelSerializer): | ||
audiences = serializers.SlugRelatedField( | ||
many=True, slug_field="name", queryset=Audience.objects.all() | ||
) | ||
|
||
class Meta: | ||
model = Announcement | ||
fields = "__all__" | ||
fields = ( | ||
"id", | ||
"title", | ||
"message", | ||
"announcement_type", | ||
"release_time", | ||
"end_time", | ||
"audiences", | ||
) | ||
|
||
def to_representation(self, instance): | ||
representation = super().to_representation(instance) | ||
representation["audiences"] = [ | ||
audience.name for audience in instance.audiences.all() | ||
] | ||
return representation | ||
|
||
def to_internal_value(self, data): | ||
audiences = data.get("audiences") | ||
if isinstance(audiences, list): | ||
audience_objs = [] | ||
for audience_name in audiences: | ||
audience = Audience.objects.filter(name=audience_name).first() | ||
if audience: | ||
audience_objs.append(audience) | ||
data["audiences"] = audience_objs | ||
return super().to_internal_value(data) | ||
|
||
def create(self, validated_data): | ||
audiences = validated_data.pop("audiences") | ||
instance = Announcement.objects.create(**validated_data) | ||
instance.audiences.set(audiences) | ||
return instance | ||
|
||
def update(self, instance, validated_data): | ||
audiences = validated_data.pop("audiences", None) | ||
super().update(instance, validated_data) | ||
if audiences: | ||
instance.audiences.set(audiences) | ||
return instance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters