def notify(request, recipient, event, actor=None, object=None, target=None, details=''): NotificationManager.notify(request, recipient, event, actor, object, target, details)
def get_description(self, pass_variables): language = get_language() cache_key = self.__class__.__name__.lower() cache_version = '{}_{}_{}'.format(self.pk, language, pass_variables) saved_description = cache.get(cache_key, version=cache_version) if saved_description is not None: return saved_description try: description = NotificationManager.get_description( self.event, self.actor, self.object, self.target, pass_variables) except KeyError: # referenced object does not exist anymore if self.pk: self.delete() return ugettext('Related object does not exist anymore') # save into cache cache.set(cache_key, description, version=cache_version, timeout=whistle_settings.TIMEOUT) return description
def init_form_helper(self): fields = [] channel_fields = [] for channel in whistle_settings.CHANNELS: if NotificationManager.is_channel_available(self.user, channel): channel_fields.append( Div(Field(channel, css_class='switch'), css_class='channel fw-bold col-md mb-3') ) fields.append(Div( Div(HTML('<p>{}</p>'.format(_(''))), css_class='col-md-6'), *channel_fields, css_class='row' ) ) for event, label in self.labels.items(): field_names = self.field_names(event) event_fields = [] for channel in whistle_settings.CHANNELS: if NotificationManager.is_notification_available(self.user, channel, event): event_fields.append( Div(Field(field_names[channel], css_class='switch'), css_class='col-md') ) if len(event_fields) > 0: fields.append(Div( Div(HTML('<p>{}</p>'.format(label)), css_class='col-md-6'), *event_fields, css_class='row' ) ) fields.append( FormActions( Submit('submit', _('Save'), css_class='btn-lg') ) ) self.helper = FormHelper() self.helper.form_class = 'notification-settings' self.helper.layout = Layout(*fields)
def init_fields(self): for channel in whistle_settings.CHANNELS: if NotificationManager.is_channel_available(self.user, channel): self.fields.update({ channel: forms.BooleanField( label=self.channel_labels(channel), required=False, initial=self.get_initial_value(channel)), }) for event, label in self.labels.items(): field_names = self.field_names(event) for channel in whistle_settings.CHANNELS: if NotificationManager.is_notification_available(self.user, channel, event): self.fields.update({ field_names[channel]: forms.BooleanField( label=self.channel_labels(channel), required=False, initial=self.get_initial_value(channel, event)), })
def clean(self): settings = {'channels': {}, 'events': {}} for channel in whistle_settings.CHANNELS: if NotificationManager.is_channel_available(self.user, channel): settings['channels'][channel] = self.cleaned_data.get(channel) # events for event in self.labels.keys(): field_names = self.field_names(event) event_identifier = event.lower() for channel in whistle_settings.CHANNELS: if NotificationManager.is_channel_available(self.user, channel) and \ NotificationManager.is_notification_available(self.user, channel, event): if channel not in settings['events']: settings['events'][channel] = {} settings['events'][channel][event_identifier] = self.cleaned_data.get(field_names[channel]) return settings
def get_initial_value(self, channel, event=None): return NotificationManager.is_notification_enabled(self.user, channel, event, bypass_channel=True)