Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
thalia
concrexit
Commits
696950b5
Commit
696950b5
authored
Feb 28, 2018
by
Luko van der Maas
Browse files
Added multilingual title and body and added support to the send and admin
parent
14d8dfcc
Changes
4
Hide whitespace changes
Inline
Side-by-side
website/pushnotifications/admin.py
View file @
696950b5
...
...
@@ -3,6 +3,7 @@ from django.utils.translation import ugettext_lazy as _
from
pushnotifications
import
models
from
pushnotifications.models
import
Message
from
utils.translation
import
TranslatedModelAdmin
@
admin
.
register
(
models
.
Device
)
...
...
@@ -29,19 +30,22 @@ class DeviceAdmin(admin.ModelAdmin):
@
admin
.
register
(
models
.
Message
)
class
MessageAdmin
(
admin
.
ModelAdmin
):
class
MessageAdmin
(
Translated
ModelAdmin
):
list_display
=
(
'title'
,
'body'
,
'category'
,
'sent'
,
'success'
,
'failure'
)
filter_horizontal
=
(
'users'
,)
list_filter
=
(
'sent'
,
'category'
)
def
get_fields
(
self
,
request
,
obj
=
None
):
if
obj
and
obj
.
sent
:
return
'users'
,
'title'
,
'body'
,
'category'
,
'success'
,
'failure'
return
'users'
,
'title'
,
'body'
,
'category'
return
(
'users'
,
'title_nl'
,
'title_en'
,
'body_nl'
,
'body_en'
,
'category'
,
'success'
,
'failure'
)
return
(
'users'
,
'title_nl'
,
'title_en'
,
'body_nl'
,
'body_en'
,
'category'
)
def
get_readonly_fields
(
self
,
request
,
obj
=
None
):
if
obj
and
obj
.
sent
:
return
'users'
,
'title'
,
'body'
,
'category'
,
'success'
,
'failure'
return
(
'users'
,
'title_nl'
,
'title_en'
,
'body_nl'
,
'body_en'
,
'category'
,
'success'
,
'failure'
)
return
super
().
get_readonly_fields
(
request
,
obj
)
def
change_view
(
self
,
request
,
object_id
,
form_url
=
''
,
**
kwargs
):
...
...
website/pushnotifications/migrations/0004_auto_20180221_1924.py
View file @
696950b5
...
...
@@ -28,6 +28,6 @@ class Migration(migrations.Migration):
migrations
.
AddField
(
model_name
=
'device'
,
name
=
'receive_category'
,
field
=
models
.
ManyToManyField
(
default
=
pushnotifications
.
models
.
Device
.
default_receive_category
,
to
=
'pushnotifications.Category'
),
field
=
models
.
ManyToManyField
(
to
=
'pushnotifications.Category'
),
),
]
website/pushnotifications/migrations/0006_auto_20180228_1942.py
0 → 100644
View file @
696950b5
# Generated by Django 2.0.2 on 2018-02-28 18:42
from
django.db
import
migrations
,
models
import
pushnotifications.models
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'pushnotifications'
,
'0005_populate_category'
),
]
operations
=
[
migrations
.
RenameField
(
model_name
=
'message'
,
old_name
=
'body'
,
new_name
=
'body_nl'
,
),
migrations
.
AlterField
(
model_name
=
'message'
,
name
=
'body_nl'
,
field
=
models
.
TextField
(
default
=
''
,
verbose_name
=
'body (NL)'
),
preserve_default
=
False
,
),
migrations
.
RenameField
(
model_name
=
'message'
,
old_name
=
'title'
,
new_name
=
'title_nl'
,
),
migrations
.
AlterField
(
model_name
=
'message'
,
name
=
'title_nl'
,
field
=
models
.
CharField
(
default
=
''
,
max_length
=
150
,
verbose_name
=
'title (NL)'
),
preserve_default
=
False
,
),
migrations
.
AddField
(
model_name
=
'message'
,
name
=
'body_en'
,
field
=
models
.
TextField
(
default
=
''
,
verbose_name
=
'body (EN)'
),
preserve_default
=
False
,
),
migrations
.
AddField
(
model_name
=
'message'
,
name
=
'title_en'
,
field
=
models
.
CharField
(
default
=
''
,
max_length
=
150
,
verbose_name
=
'title (EN)'
),
preserve_default
=
False
,
),
migrations
.
AlterField
(
model_name
=
'device'
,
name
=
'receive_category'
,
field
=
models
.
ManyToManyField
(
default
=
pushnotifications
.
models
.
default_receive_category
,
to
=
'pushnotifications.Category'
),
),
]
website/pushnotifications/models.py
View file @
696950b5
...
...
@@ -3,6 +3,7 @@ from __future__ import unicode_literals
from
django.conf
import
settings
as
django_settings
from
django.db
import
models
from
django.utils.translation
import
ugettext_lazy
as
_
from
django.utils.translation
import
override
from
pyfcm
import
FCMNotification
from
thaliawebsite
import
settings
...
...
@@ -22,6 +23,10 @@ class Category(models.Model, metaclass=ModelTranslateMeta):
return
self
.
name_en
def
default_receive_category
():
return
Category
.
objects
.
filter
(
key
=
"general"
)
class
Device
(
models
.
Model
):
DEVICE_TYPES
=
(
(
'ios'
,
'iOS'
),
...
...
@@ -47,9 +52,6 @@ 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
...
...
@@ -59,13 +61,33 @@ class Device(models.Model):
unique_together
=
(
'registration_id'
,
'user'
,)
class
Message
(
models
.
Model
):
class
Message
(
models
.
Model
,
metaclass
=
ModelTranslateMeta
):
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
(
title
=
MultilingualField
(
models
.
CharField
,
max_length
=
150
,
verbose_name
=
_
(
'title'
)
)
body
=
models
.
TextField
(
body
=
MultilingualField
(
models
.
TextField
,
verbose_name
=
_
(
'body'
)
)
category
=
models
.
ForeignKey
(
...
...
@@ -94,42 +116,60 @@ class Message(models.Model):
def
send
(
self
,
**
kwargs
):
if
self
:
reg_ids
=
list
(
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
result
=
FCMNotification
(
api_key
=
settings
.
PUSH_NOTIFICATIONS_API_KEY
).
notify_multiple_devices
(
registration_ids
=
reg_ids
,
message_title
=
self
.
title
,
message_body
=
self
.
body
,
color
=
'#E62272'
,
sound
=
'default'
,
**
kwargs
)
results
=
result
[
'results'
]
for
(
index
,
item
)
in
enumerate
(
results
):
if
'error'
in
item
:
reg_id
=
reg_ids
[
index
]
if
(
item
[
'error'
]
==
'NotRegistered'
or
item
[
'error'
]
==
'InvalidRegistration'
):
Device
.
objects
.
filter
(
registration_id
=
reg_id
).
delete
()
else
:
any_reg_ids
=
False
success_total
=
0
failure_total
=
0
result_list
=
[]
for
lang
in
settings
.
LANGUAGES
:
with
override
(
lang
[
0
]):
reg_ids
=
list
(
Device
.
objects
.
filter
(
registration_id
=
reg_id
).
update
(
active
=
False
)
self
.
sent
=
True
self
.
success
=
result
[
'success'
]
self
.
failure
=
result
[
'failure'
]
self
.
save
()
return
result
user__in
=
self
.
users
.
all
(),
receive_category__key
=
self
.
category_id
,
active
=
True
,
language
=
lang
[
0
]
).
values_list
(
'registration_id'
,
flat
=
True
))
if
len
(
reg_ids
)
==
0
:
continue
any_reg_ids
=
True
result
=
FCMNotification
(
api_key
=
settings
.
PUSH_NOTIFICATIONS_API_KEY
).
notify_multiple_devices
(
registration_ids
=
reg_ids
,
message_title
=
self
.
title
,
message_body
=
str
(
self
.
body
),
color
=
'#E62272'
,
sound
=
'default'
,
**
kwargs
)
results
=
result
[
'results'
]
for
(
index
,
item
)
in
enumerate
(
results
):
if
'error'
in
item
:
reg_id
=
reg_ids
[
index
]
if
(
item
[
'error'
]
==
'NotRegistered'
or
item
[
'error'
]
==
'InvalidRegistration'
):
Device
.
objects
.
filter
(
registration_id
=
reg_id
).
delete
()
else
:
Device
.
objects
.
filter
(
registration_id
=
reg_id
).
update
(
active
=
False
)
success_total
+=
result
[
'success'
]
failure_total
+=
result
[
'failure'
]
result_list
.
append
(
result
)
if
any_reg_ids
:
self
.
sent
=
True
self
.
success
=
success_total
self
.
failure
=
failure_total
self
.
save
()
return
result_list
return
None
Write
Preview
Supports
Markdown
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