Commit ad685ca1 authored by Thom Wiggers's avatar Thom Wiggers 📐
Browse files

Merge branch '538-add-filtering-to-push-notifications' into 'master'

Resolve "Add filtering to push notifications"

Closes #538

See merge request !665
parents 9e6a8dfe 2c203cca
from django.db.models import QuerySet
from rest_framework import permissions
class IsOwner(permissions.BasePermission):
def has_object_permission(self, request, view, obj):
if isinstance(obj, QuerySet):
return True
# must be the owner to view the object
return obj.user == request.user
......@@ -2,14 +2,28 @@ from __future__ import absolute_import
from rest_framework.serializers import ModelSerializer
from pushnotifications.models import Device
from pushnotifications.models import Device, Category
class DeviceSerializer(ModelSerializer):
class Meta:
model = Device
fields = ('pk', 'registration_id', 'active', 'date_created', 'type')
fields = (
'pk',
'registration_id',
'active',
'date_created',
'type',
'receive_category'
)
read_only_fields = ('date_created',)
extra_kwargs = {'active': {'default': True}}
class CategorySerializer(ModelSerializer):
class Meta:
model = Category
fields = ('key', 'name')
from rest_framework import permissions
from rest_framework.decorators import list_route
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from pushnotifications.api.permissions import IsOwner
from pushnotifications.api.serializers import DeviceSerializer
from pushnotifications.models import Device
from pushnotifications.api.serializers import DeviceSerializer, \
CategorySerializer
from pushnotifications.models import Device, Category
class DeviceViewSet(ModelViewSet):
......@@ -27,3 +30,9 @@ class DeviceViewSet(ModelViewSet):
def perform_update(self, serializer):
serializer.save(user=self.request.user)
@list_route()
def categories(self, request):
categories = Category.objects.all()
serializer = CategorySerializer(categories, many=True)
return Response(serializer.data)
# Generated by Django 2.0.2 on 2018-02-21 18:24
from django.db import migrations, models
import django.db.models.deletion
import pushnotifications.models
class Migration(migrations.Migration):
dependencies = [
('pushnotifications', '0003_message_category'),
]
operations = [
migrations.CreateModel(
name='Category',
fields=[
('key', models.CharField(max_length=16, primary_key=True, serialize=False)),
('name_en', models.CharField(max_length=32, verbose_name='name (EN)')),
('name_nl', models.CharField(max_length=32, verbose_name='name (NL)')),
],
),
migrations.AlterField(
model_name='message',
name='category',
field=models.ForeignKey(default='general', on_delete=django.db.models.deletion.CASCADE, to='pushnotifications.Category', verbose_name='category'),
),
migrations.AddField(
model_name='device',
name='receive_category',
field=models.ManyToManyField(default=pushnotifications.models.Device.default_receive_category, to='pushnotifications.Category'),
),
]
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations
def forwards_func(apps, schema_editor):
Category = apps.get_model("pushnotifications", "Category")
db_alias = schema_editor.connection.alias
Category.objects.using(db_alias).bulk_create([
Category(key="general", name_en="general", name_nl="algemeen"),
Category(key="pizza", name_en="pizza", name_nl="pizza"),
Category(key="event", name_en="event", name_nl="evenement"),
Category(key="newsletter", name_en="newsletter", name_nl="nieuwsbrief"),
Category(key="sponsor", name_en="sponsor", name_nl="sponsor"),
Category(key="photo", name_en="photo", name_nl="foto"),
Category(key="board", name_en="board", name_nl="bestuur"),
])
def reverse_func(apps, schema_editor):
Category = apps.get_model("pushnotifications", "Category")
db_alias = schema_editor.connection.alias
Category.objects.using(db_alias).all().delete()
class Migration(migrations.Migration):
dependencies = [
('pushnotifications', '0004_auto_20180221_1924'),
]
operations = [
migrations.RunPython(forwards_func, reverse_func),
]
......@@ -6,6 +6,20 @@ from django.utils.translation import ugettext_lazy as _
from pyfcm import FCMNotification
from thaliawebsite import settings
from utils.translation import MultilingualField, ModelTranslateMeta
class Category(models.Model, metaclass=ModelTranslateMeta):
key = models.CharField(max_length=16, primary_key=True)
name = MultilingualField(
models.CharField,
_("name"),
max_length=32,
)
def __str__(self):
return self.name_en
class Device(models.Model):
......@@ -33,29 +47,19 @@ class Device(models.Model):
choices=settings.LANGUAGES,
)
def default_receive_category(self):
return Category.objects.filter(key="general")
receive_category = models.ManyToManyField(
Category,
default=default_receive_category
)
class Meta:
unique_together = ('registration_id', 'user',)
class Message(models.Model):
GENERAL = 'general'
PIZZA = 'pizza'
EVENT = 'event'
NEWSLETTER = 'newsletter'
SPONSOR = 'sponsor'
PHOTO = 'photo'
BOARD = 'board'
CATEGORIES = (
(GENERAL, _("General")),
(PIZZA, _("Pizza")),
(EVENT, _("Events")),
(NEWSLETTER, _("Newsletter")),
(SPONSOR, _("Sponsored messages")),
(PHOTO, _("Photos")),
(BOARD, _("Board")),
)
users = models.ManyToManyField(django_settings.AUTH_USER_MODEL)
title = models.CharField(
max_length=150,
......@@ -64,13 +68,12 @@ class Message(models.Model):
body = models.TextField(
verbose_name=_('body')
)
category = models.CharField(
choices=CATEGORIES,
max_length=10,
category = models.ForeignKey(
Category,
on_delete=models.CASCADE,
verbose_name=_('category'),
default="general"
)
sent = models.BooleanField(
verbose_name=_('sent'),
default=False
......@@ -92,8 +95,11 @@ class Message(models.Model):
def send(self, **kwargs):
if self:
reg_ids = list(
Device.objects.filter(user__in=self.users.all(), active=True)
.values_list('registration_id', flat=True))
Device.objects.filter(
user__in=self.users.all(),
receive_category__id=self.category_id,
active=True
).values_list('registration_id', flat=True))
if len(reg_ids) == 0:
return None
......
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