-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from Savage-Aim/notifications
Notifications
- Loading branch information
Showing
56 changed files
with
1,471 additions
and
285 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from django.contrib.auth.models import User | ||
from django.core.management.base import BaseCommand | ||
from api import models | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'Set / Update the initial values of the notification details for every user.' | ||
|
||
def handle(self, *args, **options): | ||
# Add the tiers | ||
for user in User.objects.all(): | ||
try: | ||
for key in models.Settings.NOTIFICATIONS: | ||
if key not in user.settings.notifications: | ||
user.settings.notifications[key] = True | ||
user.settings.save() | ||
except models.Settings.DoesNotExist: | ||
models.Settings.objects.create( | ||
user=user, | ||
theme='beta', | ||
notifications={key: True for key in models.Settings.NOTIFICATIONS}, | ||
) |
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,23 @@ | ||
# Generated by Django 3.2.11 on 2022-01-27 13:57 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('api', '0014_user_settings'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='settings', | ||
name='notifications', | ||
field=models.JSONField(default=dict), | ||
), | ||
migrations.AlterField( | ||
model_name='settings', | ||
name='theme', | ||
field=models.CharField(max_length=24), | ||
), | ||
] |
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,31 @@ | ||
# Generated by Django 3.2.11 on 2022-01-28 10:26 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('api', '0015_settings_updates'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Notification', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('link', models.TextField()), | ||
('read', models.BooleanField(default=False)), | ||
('text', models.TextField()), | ||
('timestamp', models.DateTimeField(auto_now_add=True)), | ||
('type', models.TextField()), | ||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), | ||
], | ||
options={ | ||
'ordering': ['-timestamp'], | ||
}, | ||
), | ||
] |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from django.contrib.auth.models import User | ||
from django.db import models | ||
|
||
|
||
class Notification(models.Model): | ||
link = models.TextField() | ||
read = models.BooleanField(default=False) | ||
text = models.TextField() | ||
timestamp = models.DateTimeField(auto_now_add=True) | ||
type = models.TextField() # Type field maintains the internal notification type, used to send updates via ws | ||
user = models.ForeignKey(User, on_delete=models.CASCADE) | ||
|
||
def __str__(self): | ||
return f'Notification #{self.id} for {self.user.username}' | ||
|
||
class Meta: | ||
ordering = ['-timestamp'] |
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
""" | ||
Notifier contains a series of functions to create notifications for every different | ||
type used in the system, without having to add messy code elsewhere | ||
Also will handle sending info to websockets when we get there | ||
""" | ||
from django.contrib.auth.models import User | ||
from . import models | ||
|
||
|
||
def _create_notif(user: User, text: str, link: str, type: str): | ||
""" | ||
Actually does the work of creating a Notification (and sending it down the websockets later) | ||
Also is where the notification settings are checked, we won't save notifications that the User doesn't want | ||
""" | ||
# First we ensure that the User is set up to receive the notification type | ||
try: | ||
send = user.settings.notifications[type] | ||
except (AttributeError, models.Settings.DoesNotExist, KeyError): | ||
send = True | ||
|
||
if not send: | ||
return | ||
|
||
# If we make it to this point, create the object and then push updates down the web socket | ||
models.Notification.objects.create(user=user, text=text, link=link, type=type) | ||
# TODO - Websocket stuff | ||
|
||
|
||
def loot_tracker_update(bis: models.BISList, team: models.Team): | ||
char = bis.owner | ||
text = f'"{char}"\'s {bis.job.id} BIS List was updated via "{team.name}"\'s Loot Tracker!' | ||
link = f'/characters/{char.id}/bis_list/{bis.id}/' | ||
user = char.user | ||
_create_notif(user, text, link, 'loot_tracker_update') | ||
|
||
|
||
def team_join(char: models.Character, team: models.Team): | ||
text = f'{char} has joined {team.name}!' | ||
link = f'/team/{team.id}/' | ||
user = team.members.get(lead=True).character.user | ||
_create_notif(user, text, link, 'team_join') | ||
|
||
|
||
def team_lead(char: models.Character, team: models.Team): | ||
text = f'{char} has been made the Team Leader of {team.name}!' | ||
link = f'/team/{team.id}/' | ||
user = char.user | ||
_create_notif(user, text, link, 'team_lead') | ||
|
||
|
||
def verify_fail(char: models.Character, error: str): | ||
text = f'The verification of {char} has failed! Reason: {error}' | ||
link = f'/characters/{char.id}/' | ||
user = char.user | ||
_create_notif(user, text, link, 'verify_fail') | ||
|
||
|
||
def verify_success(char: models.Character): | ||
text = f'The verification of {char} has succeeded!' | ||
link = f'/characters/{char.id}/' | ||
user = char.user | ||
_create_notif(user, text, link, 'verify_success') |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
""" | ||
Serializer for Notification entries | ||
""" | ||
# lib | ||
from rest_framework import serializers | ||
# local | ||
from api.models import Notification | ||
|
||
__all__ = [ | ||
'NotificationSerializer', | ||
] | ||
|
||
|
||
class NotificationSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
exclude = ['user'] | ||
model = Notification |
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
Oops, something went wrong.