Commit 9ffe5f16 by Őry Máté

dashboard: use HumanReadableObject for Notifications

parent 82af31c5
...@@ -30,16 +30,17 @@ from django.db.models import ( ...@@ -30,16 +30,17 @@ from django.db.models import (
DateTimeField, permalink, BooleanField DateTimeField, permalink, BooleanField
) )
from django.db.models.signals import post_save, pre_delete from django.db.models.signals import post_save, pre_delete
from django.template.loader import render_to_string
from django.templatetags.static import static from django.templatetags.static import static
from django.utils.translation import ugettext_lazy as _, override, ugettext from django.utils.translation import ugettext_lazy as _
from django_sshkey.models import UserKey from django_sshkey.models import UserKey
from jsonfield import JSONField
from model_utils.models import TimeStampedModel from model_utils.models import TimeStampedModel
from model_utils.fields import StatusField from model_utils.fields import StatusField
from model_utils import Choices from model_utils import Choices
from acl.models import AclBase from acl.models import AclBase
from common.models import HumanReadableObject, create_readable, Encoder
from vm.tasks.agent_tasks import add_keys, del_keys from vm.tasks.agent_tasks import add_keys, del_keys
...@@ -58,26 +59,39 @@ class Notification(TimeStampedModel): ...@@ -58,26 +59,39 @@ class Notification(TimeStampedModel):
status = StatusField() status = StatusField()
to = ForeignKey(User) to = ForeignKey(User)
subject = CharField(max_length=128) subject_data = JSONField(null=True, dump_kwargs={"cls": Encoder})
message = TextField() message_data = JSONField(null=True, dump_kwargs={"cls": Encoder})
valid_until = DateTimeField(null=True, default=None) valid_until = DateTimeField(null=True, default=None)
class Meta: class Meta:
ordering = ['-created'] ordering = ['-created']
@classmethod @classmethod
def send(cls, user, subject, template, context={}, valid_until=None): def send(cls, user, subject, template, context,
try: valid_until=None, subject_context=None):
language = user.profile.preferred_language hro = create_readable(template, user=user, **context)
except: subject = create_readable(subject, subject_context or context)
language = None return cls.objects.create(to=user,
with override(language): subject_data=subject.to_dict(),
context['user'] = user message_data=hro.to_dict(),
rendered = render_to_string(template, context)
subject = ugettext(unicode(subject))
return cls.objects.create(to=user, subject=subject, message=rendered,
valid_until=valid_until) valid_until=valid_until)
@property
def subject(self):
return HumanReadableObject.from_dict(self.subject_data)
@subject.setter
def subject(self, value):
self.subject_data = None if value is None else value.to_dict()
@property
def message(self):
return HumanReadableObject.from_dict(self.message_data)
@message.setter
def message(self, value):
self.message_data = None if value is None else value.to_dict()
class Profile(Model): class Profile(Model):
user = OneToOneField(User) user = OneToOneField(User)
...@@ -96,8 +110,11 @@ class Profile(Model): ...@@ -96,8 +110,11 @@ class Profile(Model):
verbose_name=_("Email notifications"), default=True, verbose_name=_("Email notifications"), default=True,
help_text=_('Whether user wants to get digested email notifications.')) help_text=_('Whether user wants to get digested email notifications.'))
def notify(self, subject, template, context={}, valid_until=None): def notify(self, subject, template, context=None, valid_until=None,
return Notification.send(self.user, subject, template, context, **kwargs):
if context is not None:
kwargs.update(context)
return Notification.send(self.user, subject, template, kwargs,
valid_until) valid_until)
def get_absolute_url(self): def get_absolute_url(self):
......
{%load i18n%}
{%blocktrans with instance=instance.name user=user.name%}
Your ownership offer of {{instance}} has been accepted by {{user}}.
{%endblocktrans%}
{%load i18n%}
{%blocktrans with instance=instance.name user=user.name%}
{{user}} offered you to take the ownership of his/her virtual machine
called {{instance}}.{%endblocktrans%}
<a href="{{token}}" class="btn btn-success btn-small">{%trans "Accept"%}</a>
{%load i18n%}
{%blocktrans with instance=instance.name url=instance.get_absolute_url %}
Your instance <a href="{{url}}">{{instance}}</a> has been destroyed due to expiration.
{%endblocktrans%}
{%load i18n%}
{%blocktrans with instance=instance.name url=instance.get_absolute_url suspend=instance.time_of_suspend delete=instance.time_of_delete %}
Your instance <a href="{{url}}">{{instance}}</a> is going to expire.
It will be suspended at {{suspend}} and destroyed at {{delete}}.
{%endblocktrans%}
{%blocktrans with token=token url=instance.get_absolute_url %}
Please, either <a href="{{token}}">renew</a> or <a href="{{url}}">destroy</a>
it now.
{%endblocktrans%}
{%load i18n%}
{%blocktrans with instance=instance.name url=instance.get_absolute_url %}
Your instance <a href="{{url}}">{{instance}}</a> has been suspended due to expiration.
{%endblocktrans%}
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 sign in to comment