Commit 9ffe5f16 by Őry Máté

dashboard: use HumanReadableObject for Notifications

parent 82af31c5
......@@ -30,16 +30,17 @@ from django.db.models import (
DateTimeField, permalink, BooleanField
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.utils.translation import ugettext_lazy as _, override, ugettext
from django.utils.translation import ugettext_lazy as _
from django_sshkey.models import UserKey
from jsonfield import JSONField
from model_utils.models import TimeStampedModel
from model_utils.fields import StatusField
from model_utils import Choices
from acl.models import AclBase
from common.models import HumanReadableObject, create_readable, Encoder
from vm.tasks.agent_tasks import add_keys, del_keys
......@@ -58,26 +59,39 @@ class Notification(TimeStampedModel):
status = StatusField()
to = ForeignKey(User)
subject = CharField(max_length=128)
message = TextField()
subject_data = JSONField(null=True, dump_kwargs={"cls": Encoder})
message_data = JSONField(null=True, dump_kwargs={"cls": Encoder})
valid_until = DateTimeField(null=True, default=None)
class Meta:
ordering = ['-created']
def send(cls, user, subject, template, context={}, valid_until=None):
language = user.profile.preferred_language
language = None
with override(language):
context['user'] = user
rendered = render_to_string(template, context)
subject = ugettext(unicode(subject))
return cls.objects.create(to=user, subject=subject, message=rendered,
def send(cls, user, subject, template, context,
valid_until=None, subject_context=None):
hro = create_readable(template, user=user, **context)
subject = create_readable(subject, subject_context or context)
return cls.objects.create(to=user,
def subject(self):
return HumanReadableObject.from_dict(self.subject_data)
def subject(self, value):
self.subject_data = None if value is None else value.to_dict()
def message(self):
return HumanReadableObject.from_dict(self.message_data)
def message(self, value):
self.message_data = None if value is None else value.to_dict()
class Profile(Model):
user = OneToOneField(User)
......@@ -96,8 +110,11 @@ class Profile(Model):
verbose_name=_("Email notifications"), default=True,
help_text=_('Whether user wants to get digested email notifications.'))
def notify(self, subject, template, context={}, valid_until=None):
return Notification.send(self.user, subject, template, context,
def notify(self, subject, template, context=None, valid_until=None,
if context is not None:
return Notification.send(self.user, subject, template, kwargs,
def get_absolute_url(self):
{%load i18n%}
{%blocktrans with}
Your ownership offer of {{instance}} has been accepted by {{user}}.
{%load i18n%}
{%blocktrans with}
{{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 url=instance.get_absolute_url %}
Your instance <a href="{{url}}">{{instance}}</a> has been destroyed due to expiration.
{%load i18n%}
{%blocktrans with 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}}.
{%blocktrans with token=token url=instance.get_absolute_url %}
Please, either <a href="{{token}}">renew</a> or <a href="{{url}}">destroy</a>
it now.
{%load i18n%}
{%blocktrans with url=instance.get_absolute_url %}
Your instance <a href="{{url}}">{{instance}}</a> has been suspended due to expiration.
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