class CreateBookmarkView(BookmarkCreateUpdateMixin, CreateView): success_message = _("The bookmark was created successfully.") @method_decorator(permission_required("menus.change_menu", _get_bookmarks)) @method_decorator(permission_required("menus.add_link")) def dispatch(self, request, *args, **kwargs): return super(CreateBookmarkView, self).dispatch(request, *args, **kwargs)
class UpdateBookmarkView(BookmarkCreateUpdateMixin, UpdateView): success_url = reverse_lazy("bookmark_list") success_message = _("The bookmark was updated successfully.") @method_decorator(permission_required("menus.change_menu", _get_bookmarks)) @method_decorator(permission_required("menus.change_link", _get_bookmark)) def dispatch(self, request, *args, **kwargs): return super(UpdateBookmarkView, self).dispatch(request, *args, **kwargs)
class DeleteBookmarkView(SuccessMessageMixin, SetCancelUrlMixin, BookmarkMixin, DeleteView): success_url = reverse_lazy("bookmark_list") success_message = _("The bookmark was deleted successfully.") @method_decorator(permission_required("menus.change_menu", _get_bookmarks)) @method_decorator(permission_required("menus.delete_link", _get_bookmark)) def dispatch(self, request, *args, **kwargs): return super(DeleteBookmarkView, self).dispatch(request, *args, **kwargs)
class ListNotificationView(NotificationMixin, ModelListView): """Displays the list of all filtered notifications. """ field_list = ["title", "created", "read"] paginate_by=10 @method_decorator(permission_required(_get_object_view_perm, _get_object)) @method_decorator(permission_required('notifications.view_notification')) def dispatch(self, request, *args, **kwargs): return super(ListNotificationView, self).dispatch(request, *args, **kwargs)
class ListBookmarkView(BookmarkMixin, ModelListView): field_list = ["title", "url", "description", "new_window"] delete_template_name = "menus/bookmark_model_list_confirm_delete.html" paginate_by = 10 @method_decorator(permission_required("menus.view_menu", _get_bookmarks)) def dispatch(self, request, *args, **kwargs): return super(ListBookmarkView, self).dispatch(request, *args, **kwargs)
class DeletePluggetView(SuccessMessageMixin, SetCancelUrlMixin, DeleteView): model = Plugget success_message = _("The plugget was deleted successfully.") @method_decorator( permission_required("pluggets.change_region", _get_region)) @method_decorator( permission_required("pluggets.delete_plugget", _get_plugget)) def dispatch(self, request, *args, **kwargs): return super(DeletePluggetView, self).dispatch(request, *args, **kwargs) def get_object(self, queryset=None): self.object = super(DeletePluggetView, self).get_object(queryset) self.cancel_url = self.object.region.get_absolute_url() self.success_url = self.object.region.get_absolute_url() return self.object
class DetailNotificationView(NotificationMixin, DetailView): """Displays the list of all filtered notifications. """ @method_decorator(permission_required(_get_object_view_perm, _get_object)) def dispatch(self, request, *args, **kwargs): return super(DetailNotificationView, self).dispatch(request, *args, **kwargs) def get_object(self, *args, **kwargs): obj = super(DetailNotificationView, self).get_object(*args, **kwargs) obj.read = now() obj.save() return obj
class DeleteNotificationView(SuccessMessageMixin, SetCancelUrlMixin, NotificationMixin, DeleteView): """Deletes the given notification. """ success_message = _("The notification was deleted successfully.") @method_decorator(permission_required("notifications.delete_notification", _get_notification)) def dispatch(self, request, *args, **kwargs): return super(DeleteNotificationView, self).dispatch(request, *args, **kwargs) def get_object(self, queryset=None): self.object = super(DeleteNotificationView, self).get_object(queryset) self.success_url = reverse('notification_list', args=[self.object.target._meta.verbose_name_plural, self.object.target_id]) return self.object
class PluggetWizard(SetCancelUrlMixin, SessionWizardView): DEFAULT_FORMS = [SelectPluggetSourceForm, CustomizePluggetSettingsForm] template_name = "pluggets/plugget_wizard_form.html" model = Plugget instance = None source = None region = None @method_decorator( permission_required("pluggets.change_region", _get_region)) @method_decorator( permission_required(_get_plugget_add_or_edit_perm, _get_plugget)) @method_decorator(is_plugget_editable(_get_plugget)) def dispatch(self, request, *args, **kwargs): return super(PluggetWizard, self).dispatch(request, *args, **kwargs) def get_form_kwargs(self, step): if "pk" in self.kwargs: self.instance = get_object_or_404(Plugget, pk=self.kwargs['pk']) self.region = self.instance.region elif "slug" in self.kwargs: self.region = get_object_or_404(Region, slug=self.kwargs['slug']) if step == "1": return {"region": self.region} return {} def get_form(self, step=None, data=None, files=None): form = super(PluggetWizard, self).get_form(step, data, files) # determine the step if not given if step is None: step = self.steps.current if step == '1': custom_form_class = self.source['form'] if custom_form_class and issubclass(custom_form_class, DjangoForm): custom_form = custom_form_class(self.get_form_initial(step)) form.fields.update(custom_form.fields) return form def get_form_instance(self, step): if step == '1': return self.instance return self.instance_dict.get(step, None) def get_form_initial(self, step): initial = super(PluggetWizard, self).get_form_initial(step) source_uid = None title = None data0 = self.storage.get_step_data("0") data1 = self.storage.get_step_data("1") sources = registry.get_plugget_sources() if self.instance: source_uid = self.instance.source title = self.instance.title if data0: source_uid = data0.get('0-source_uid', self.instance.source) self.source = sources.get(source_uid, {}) self.source['source_uid'] = source_uid if data1: title = data1.get('1-title', self.instance.title) else: if data0: source_uid = data0.get('0-source_uid', None) self.source = sources.get(source_uid, {}) self.source['source_uid'] = source_uid if data1: title = data1.get('1-title', source_uid) if step == "0": initial['region_slug'] = self.region.slug initial['source_uid'] = source_uid elif step == "1": initial['title'] = title or source_uid if self.instance: initial.update(json.loads(self.instance.context or "{}")) self.cancel_url = self.region.get_absolute_url() return initial def get_context_data(self, form, **kwargs): context = super(PluggetWizard, self).get_context_data(form=form, **kwargs) context.update({'object': self.instance, 'region': self.region}) if self.steps.current == '1': context.update({ "plugget_description": self.source['description'], }) return context def done(self, form_list, **kwargs): from django.http import HttpResponseRedirect # First step: f0 = list(form_list)[0] source_uid = f0.cleaned_data['source_uid'] source = registry.get_plugget_source(source_uid) # Second step: f1 = form_list[1] title = f1.cleaned_data['title'] context = {} for k, v in list(f1.cleaned_data.items()): if k != "title": context[k] = v if isinstance(v, DjangoModel): context[k] = v.pk context = json.dumps(context) # Merging: if self.instance: self.instance.source = source_uid self.instance.title = title self.instance.context = context self.instance.template = source['default_template'] self.instance.description = source['description'] self.instance.save() messages.success(self.request, _("The plugget was updated successfully.")) else: region = get_object_or_404(Region, slug=kwargs.get('slug', None)) self.instance = Plugget.objects.create( source=source_uid, region=region, title=title, context=context, description=source['description'], template=source['default_template'], sort_order=region.pluggets.count()) messages.success(self.request, _("The plugget was created successfully.")) return HttpResponseRedirect(self.instance.get_absolute_url())