def obligee_set_details_live(self, obligee_pks): obligee_pks = obligee_pks.split(u',') if obligee_pks else [] obligees = [ try_except(lambda: Obligee.objects.get(pk=pk), None) for pk in obligee_pks ] return admin_obj_format_join(u'\n', obligees, u'{tag} {obj.name}')
def has_delete_permission(self, request, obj=None): if obj: # Only messages that are not a part of an action may be deleted. action = try_except(lambda: obj.action, None) if action is not None: return False return super(MessageAdminMixin, self).has_delete_permission(request, obj)
def clean(self): cleaned_data = super(ActionAdminChangeForm, self).clean() if u'email' in cleaned_data: if cleaned_data[u'email']: action = try_except(lambda: cleaned_data[u'email'].action, None, Action.DoesNotExist) if action is not None and action.pk != self.instance.pk: self.add_error(u'email', u'This e-mail is already used with another action.') return cleaned_data
def render_change_form(self, request, context, **kwargs): inforequestemail = kwargs.get(u'obj', None) # Decide button if inforequestemail and inforequestemail.type == InforequestEmail.TYPES.UNDECIDED: message = inforequestemail.email action = try_except(lambda: message.action, None) if message.type == Message.TYPES.INBOUND and message.processed and action is None: context[u'decide_url'] = reverse(u'admin:inforequests_inforequestemail_decide', args=[inforequestemail.pk]) return super(InforequestEmailAdmin, self).render_change_form(request, context, **kwargs)
def clean(self): cleaned_data = super(ActionAdminChangeForm, self).clean() if u'email' in cleaned_data: if cleaned_data[u'email']: action = try_except(lambda: cleaned_data[u'email'].action, None, Action.DoesNotExist) if action is not None and action.pk != self.instance.pk: self.add_error( u'email', u'This e-mail is already used with another action.') return cleaned_data
def render_change_form(self, request, context, **kwargs): inforequestemail = kwargs.get(u'obj', None) # Decide button if inforequestemail and inforequestemail.type == InforequestEmail.TYPES.UNDECIDED: message = inforequestemail.email action = try_except(lambda: message.action, None) if message.type == Message.TYPES.INBOUND and message.processed and action is None: context[u'decide_url'] = reverse( u'admin:inforequests_inforequestemail_decide', args=[inforequestemail.pk]) return super(InforequestEmailAdmin, self).render_change_form(request, context, **kwargs)
def decide_view(self, request, inforequestemail_pk): inforequestemail = self.get_object(request, inforequestemail_pk) message = inforequestemail.email if inforequestemail else None action = try_except(lambda: message.action, None) if (inforequestemail is None or inforequestemail.type != InforequestEmail.TYPES.UNDECIDED or message.type != Message.TYPES.INBOUND or not message.processed or action is not None): return HttpResponseNotFound() session = Session.objects.get(session_key=request.session.session_key) if request.method == u'POST': form = InforequestEmailAdminDecideForm(request.POST, instance=inforequestemail, attached_to=session) if form.is_valid(): new_action = form.save(commit=False) new_action.save() inforequestemail.type = InforequestEmail.TYPES.OBLIGEE_ACTION inforequestemail.save(update_fields=[u'type']) info = new_action._meta.app_label, new_action._meta.model_name return HttpResponseRedirect( reverse(u'admin:%s_%s_change' % info, args=[new_action.pk])) else: form = InforequestEmailAdminDecideForm(instance=inforequestemail, attached_to=session) opts = self.model._meta template = u'admin/%s/%s/decide_form.html' % (opts.app_label, opts.model_name) adminForm = admin.helpers.AdminForm( form, fieldsets=self.fieldsets_decide, prepopulated_fields={}, readonly_fields=self.readonly_fields, model_admin=self, ) return render( request, template, { u'object': inforequestemail, u'title': 'Decide %s' % force_text(opts.verbose_name), u'opts': opts, u'adminform': adminForm, u'media': self.media + adminForm.media, })
def decide_view(self, request, inforequestemail_pk): inforequestemail = self.get_object(request, inforequestemail_pk) message = inforequestemail.email if inforequestemail else None action = try_except(lambda: message.action, None) if (inforequestemail is None or inforequestemail.type != InforequestEmail.TYPES.UNDECIDED or message.type != Message.TYPES.INBOUND or not message.processed or action is not None): return HttpResponseNotFound() session = Session.objects.get(session_key=request.session.session_key) if request.method == u'POST': form = InforequestEmailAdminDecideForm(request.POST, instance=inforequestemail, attached_to=session) if form.is_valid(): new_action = form.save(commit=False) new_action.save() inforequestemail.type = InforequestEmail.TYPES.OBLIGEE_ACTION inforequestemail.save(update_fields=[u'type']) info = new_action._meta.app_label, new_action._meta.model_name return HttpResponseRedirect(reverse(u'admin:%s_%s_change' % info, args=[new_action.pk])) else: form = InforequestEmailAdminDecideForm(instance=inforequestemail, attached_to=session) opts = self.model._meta template = u'admin/%s/%s/decide_form.html' % (opts.app_label, opts.model_name) adminForm = admin.helpers.AdminForm(form, fieldsets=self.fieldsets_decide, prepopulated_fields={}, readonly_fields=self.readonly_fields, model_admin=self, ) return render(request, template, { u'object': inforequestemail, u'title': 'Decide %s' % force_text(opts.verbose_name), u'opts': opts, u'adminform': adminForm, u'media': self.media + adminForm.media, })
def test_uncatched_exception(self): a = dict(moo=4) with self.assertRaises(KeyError): try_except(lambda: a[u'foo'], 7, ValueError)
def branch_details_live(self, branch_pk): branch = try_except(lambda: Branch.objects.get(pk=branch_pk), None) return admin_obj_format(branch)
def branch_inforequest_live(self, branch_pk): branch = try_except(lambda: Branch.objects.get(pk=branch_pk), None) inforequest = branch.inforequest if branch else None return admin_obj_format(inforequest)
def test_with_callable_failture(self): a = dict(moo=4) b = try_except(lambda: a[u'foo'], lambda: a[u'moo']) self.assertEqual(b, 4)
def test_with_multiple_exception_classes(self): a = dict(moo=4) b = try_except(lambda: a[u'foo'], 7, ValueError, KeyError, IndexError) self.assertEqual(b, 7)
def has_delete_permission(self, request, obj=None): if not obj: return True # Only messages that are not a part of an action may be unassigned. action = try_except(lambda: obj.email.action, None) return action is None
def email_action_field(self, inforequestemail): email = inforequestemail.email if inforequestemail else None action = try_except(lambda: email.action, None) return admin_obj_format(action)
def test_catched_exception(self): a = dict(moo=4) b = try_except(lambda: a[u'foo'], 7, KeyError) self.assertEqual(b, 7)
def obligee_details_live(self, obligee_pk): obligee = try_except(lambda: Obligee.objects.get(pk=obligee_pk), None) return admin_obj_format(obligee, u'{tag}\n{obj.name}')
def branch_obligee_live(self, branch_pk): branch = try_except(lambda: Branch.objects.get(pk=branch_pk), None) obligee = branch.obligee if branch else None return admin_obj_format(obligee, u'{tag}\n{obj.name}')
def test_without_exception(self): a = dict(moo=4) b = try_except(lambda: a[u'moo'], 7, KeyError) self.assertEqual(b, 4)
def test_with_no_exception_classes(self): a = dict(moo=4) b = try_except(lambda: a[u'foo'], 7) self.assertEqual(b, 7)
def action_column(self, inforequestemail): action = try_except(lambda: inforequestemail.email.action, None) return admin_obj_format(action)
def generic_object_live(self, generic_type, generic_id): generic = try_except( lambda: generic_type.get_object_for_this_type(pk=generic_id), None) return admin_obj_format(generic)
def action_field(self, message): action = try_except(lambda: message.action, None) return admin_obj_format(action)
def email_action_live(self, email): action = try_except(lambda: email.action, None) return admin_obj_format(action)
def generic_object_live(self, generic_type, generic_id): generic = try_except(lambda: generic_type.get_object_for_this_type(pk=generic_id), None) return admin_obj_format(generic)
def obligee_set_details_live(self, obligee_pks): obligee_pks = obligee_pks.split(u',') if obligee_pks else [] obligees = [try_except(lambda: Obligee.objects.get(pk=pk), None) for pk in obligee_pks] return admin_obj_format_join(u'\n', obligees, u'{tag} {obj.name}')