Last active
October 19, 2017 04:26
-
-
Save guyjacks/9bd743ef265dd4306e659867de39c732 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#### users/models.py | |
class Message(models.Model): | |
user = ForeignKeyField() | |
# we could add a priorty or level field if we want, but lets not do that until we know we need it | |
app_name = CharField(default='users') # i.e. 'impact' | |
template = CharField(default='message.html') # i.e. 'badge_message.html' | |
heading = CharField() | |
content = TextField() | |
action = HStoreField() | |
image = HStoreField() | |
link = HStoreField() | |
# Alternative Message Model | |
# Probably gives us better flexibility | |
class Message(models.Model): | |
user = ForeignKeyField() | |
app_name = CharField(default='users') # i.e. 'impact' | |
template = CharField(default='message.html') # i.e. 'badge_message.html' | |
heading = CharField() | |
content = TextField() | |
# replace the HStoreFields with a single context JSONField | |
# https://docs.djangoproject.com/en/1.11/ref/contrib/postgres/fields/#jsonfield | |
context = JSONField() | |
""" | |
context = { | |
'action': { | |
'button-call': 'btn-paddle', 'button-data-id': 123 | |
}, | |
'image': {...} | |
} | |
Message.objects.create(..., context=context) | |
""" | |
#### users/messages.py | |
def notify(user, type, heading, content="", action=None, image=None, link=None): | |
# create a record in the database and return the new model instance | |
return Message.objects.create(...) | |
def get_unread_messages_by_user(user): | |
return user.messages.filter(read=False) | |
#### impact/game.py | |
# import the messages module | |
from ..users import messages | |
# ... | |
def complete_action(): | |
... | |
# PLEASE WAIT for me to start to work on this function. | |
# We'll pair code this piece since it will be great exercise in automated testing. | |
# You can see tests here impact/tests/test_game.py | |
""" | |
If the user is being awarded points | |
- Check if there is already an unread message with type = "points" | |
- If there is a record then update with a new message alerting the user of all unread points. | |
- MAYBE we won't try to solve this problem right now. | |
""" | |
messages.notify(...) | |
#### impact/views.py | |
from ..users import messages | |
class ImpactDetail(DetailView): | |
def get_context_data(): | |
# ... | |
context['users'] = messages.get_unread_messages_for_user(self.request.user) | |
# ... | |
""" | |
The following two message templates are meant to illustrate the point | |
that we can defer to individual apps to implement custom message templates. | |
For insance, the impact app can have its own message templates unique to | |
game notifications. | |
The messages system expects a subfolder named "messages" to exist in the | |
app's template folder so there would be something like: | |
"templates/impact/messages/*" | |
""" | |
#### templates/impact/messages/badge_message.html | |
<h2>{{ message.heading }}<h2> | |
<img src="{% static 'images/badges/' + message.image.filename %}" alt="{{ message.image.alt }}" /> | |
<p>{{ message.content }}</p> | |
#### templates/users/messages/tour_message.html | |
<h2>{{ message.heading }}<h2> | |
<p>{{ message.content }}</p> | |
{% if message.action %} | |
<button id="begin-tour" data_id="{{ message.action.id }}" class="btn {{ message.action.button_class">{{ message.action.label }}</button> | |
{% endif %} | |
#### templates/users/messages/messages.html | |
{% for message in messages %} | |
{% include message.app + '/messages/' + message.template with message=message %} | |
{% endfor %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment