def get_context_data(self, **kwargs): widget_filter = self.get_filter() widgets_configs = WidgetConfig.objects.filter( **widget_filter).order_by('sort_field') deactivated_apps = [] if 'group_id' in widget_filter: # if we are dealing with group dashboard widgets, use the group's deactivated apps deactivated_apps = self.group.get_deactivated_apps() widgets = [] """ We also sort each unique widget into the context to be accessed hard-coded""" for wc in widgets_configs: # check deactivated apps to see if widget can't be shown: if 'cosinnus_%s' % wc.app_name in deactivated_apps: continue # check block list for disallowed widgets (from overriding views) if "%s.%s" % (wc.app_name, wc.widget_name.replace( " ", "_")) in self.disallowed_widgets: continue widget_class = widget_registry.get(wc.app_name, wc.widget_name) if widget_class: widget = widget_class(self.request, wc) widgets.append(widget) widgets = self.reorder_equal_widgets(widgets) kwargs.update({ 'widgets': widgets, }) return super(DashboardWidgetMixin, self).get_context_data(**kwargs)
def widget_detail(request, id, offset=0): wc = get_object_or_404(WidgetConfig, id=int(id)) if not check_user_superuser(request.user) and \ wc.group and not (check_ug_membership(request.user, wc.group) or \ wc.group.public or wc.type == WidgetConfig.TYPE_MICROSITE) or \ wc.user and wc.user_id != request.user.pk: return HttpResponseForbidden('Access denied!') widget_class = widget_registry.get(wc.app_name, wc.widget_name) if widget_class is None: return render('cosinnus/widgets/not_found.html') widget = widget_class(request, wc) widget_content, rows_returned, has_more = widget.get_data(int(offset)) data = { 'X-Cosinnus-Widget-Content': widget_content, 'X-Cosinnus-Widget-Title': force_text(widget.title), 'X-Cosinnus-Widget-App-Name': force_text(wc.app_name), 'X-Cosinnus-Widget-Widget-Name': force_text(wc.widget_name), 'X-Cosinnus-Widget-Num-Rows-Returned': rows_returned, 'X-Cosinnus-Widget-Has-More-Data': 'true' if has_more else 'false', } title_url = widget.title_url if title_url is not None: data['X-Cosinnus-Widget-Title-URL'] = force_text(title_url) return JSONResponse(data)
def widget_add_group(request, group, app_name, widget_name): widget_class = widget_registry.get(app_name, widget_name) form_class = widget_class.get_setup_form_class() if request.method == "POST": form = form_class(request.POST) if form.is_valid(): widget = widget_class.create(request, group=group) widget.save_config(form.cleaned_data) return JSONResponse({'id': widget.id}) else: form = form_class() d = { 'form': form, 'submit_label': _('Add widget'), } c = RequestContext(request) return render_to_response('cosinnus/widgets/setup.html', d, c)
def widget_detail(request, id): wc = get_object_or_404(WidgetConfig, id=int(id)) if wc.group and not (check_ug_membership(request.user, wc.group) or wc.group.public) or \ wc.user and wc.user_id != request.user.pk: return HttpResponseForbidden('Access denied!') widget_class = widget_registry.get(wc.app_name, wc.widget_name) widget = widget_class(request, wc) data = widget.get_data() if isinstance(data, six.string_types): resp = HttpResponse(data) else: resp = JSONResponse(data) resp['X-Cosinnus-Widget-Title'] = force_text(widget.title) if widget.title_url is not None: resp['X-Cosinnus-Widget-Title-URL'] = force_text(widget.title_url) resp['X-Cosinnus-Widget-App-Name'] = force_text(wc.app_name) resp['X-Cosinnus-Widget-Widget-Name'] = force_text(wc.widget_name) return resp
def widget_edit(request, id): wc = get_object_or_404(WidgetConfig, id=int(id)) if wc.group and not check_ug_admin(request.user, wc.group) or \ wc.user and wc.user_id != request.user.pk: return HttpResponseForbidden('Access denied!') widget_class = widget_registry.get(wc.app_name, wc.widget_name) form_class = widget_class.get_setup_form_class() widget = widget_class(request, wc) if request.method == "POST": form = form_class(request.POST) if form.is_valid(): widget.save_config(form.cleaned_data) return JSONResponse({'id': widget.id}) else: form = form_class(initial=dict(widget.config)) d = { 'form': form, 'submit_label': _('Change'), } c = RequestContext(request) return render_to_response('cosinnus/widgets/setup.html', d, c)
def widget_add_group(request, group, app_name=None, widget_name=None): template_name = 'cosinnus/widgets/add_widget.html' extra_context = {'form_view': 'add'} if request.method == "POST": widget_class = widget_registry.get(app_name, widget_name) if widget_class is None: return render('cosinnus/widgets/not_found.html') form_class = widget_class.get_setup_form_class() if not widget_class.allow_on_group: return render('cosinnus/widgets/not_allowed_group.html') form = form_class(request.POST, group=group) if form.is_valid(): # the onl difference to user seems to be: if not group: widget = widget_class.create(request, user=request.user) else: widget = widget_class.create(request, group=group) widget.save_config(form.cleaned_data) return HttpResponse( widget.render(user=request.user, request=request, group=group)) raise Exception("Form was invalid for widget add: ", app_name, widget_name, form_class) else: data = [] form_active = True for app_name, widgets in widget_registry: for widget_name in widgets: widget_class = widget_registry.get(app_name, widget_name) if widget_class is None: #print ">>>>widg not found:", app_name, widget_name continue form_class = widget_class.get_setup_form_class() if not getattr(form_class, "template_name", None): #raise ImproperlyConfigured('Widget form "%s %s" has no attribute "template_name" configured!' % (app_name, widget_name)) #print '>> ignoring widget "%s %s" without template_name form: ' % (app_name, widget_name) continue context = {'form': form_class(group=group)} widget_form_content = render(request, form_class.template_name, context).content data.append({ 'app_name': app_name, 'widget_name': widget_name, 'widget_title': widget_class.get_widget_title(), 'form_content': widget_form_content, 'form_id': '%s_%s_%d' % (app_name, widget_name, uuid1()), 'form_active': form_active, }) form_active = False #only first form is active context = {'widget_data': data} context.update(extra_context) return render(request, template_name, context)
def widget_edit(request, id, app_name=None, widget_name=None): template_name = 'cosinnus/widgets/add_widget.html' extra_context = {'form_view': 'edit'} wc = get_object_or_404(WidgetConfig, id=int(id)) if wc.group and not check_ug_admin(request.user, wc.group) or \ wc.user and wc.user_id != request.user.pk: return HttpResponseForbidden('Access denied!') if app_name and widget_name and (wc.app_name != app_name or wc.widget_name != widget_name): #print ">>>>> THIS WIDGET WAS SET UP TO BE SWAPPED BY EDITING IT!" #print ">> TODO: create new widget using create function, transfer important values, then delete this widget! " # TODO: widget swapping disabled for now! raise Exception("Swapping of widget types is not enabled. \ Delete this widget and create a new one if you want one of a different type!" ) widget_class = widget_registry.get(wc.app_name, wc.widget_name) if widget_class is None: return render('cosinnus/widgets/not_found.html') form_class = widget_class.get_setup_form_class() widget = widget_class(request, wc) if request.method == "POST": form = form_class(request.POST, group=wc.group) if form.is_valid(): widget.save_config(form.cleaned_data) return HttpResponse( widget.render(user=request.user, request=request, group=wc.group)) raise Exception("Form was invalid for widget edit: ", app_name, widget_name, form_class) else: data = [] for app_name, widgets in widget_registry: for widget_name in widgets: form_active = False widget_class = widget_registry.get(app_name, widget_name) if widget_class is None: #print ">>>>widg not found:", app_name, widget_name continue form_class = widget_class.get_setup_form_class() if not getattr(form_class, "template_name", None): #raise ImproperlyConfigured('Widget form "%s %s" has no attribute "template_name" configured!' % (app_name, widget_name)) #print '>> ignoring widget "%s %s" without template_name form: ' % (app_name, widget_name) continue if app_name == widget.app_name and widget_name == widget.widget_name: # this is the form of the widget class that the editing widget is of # init the form with the current widgets config, and set the active widget to this one form_dict = dict([(k, v) for k, v in widget.config]) form_dict.update({ 'sort_field': widget.config.sort_field, }) context = { 'form': form_class(initial=form_dict, group=wc.group) } form_active = True else: # TODO: widget swapping disabled for now! continue #context = {'form': form_class(group=wc.group)} widget_form_content = render(request, form_class.template_name, context).content data.append({ 'app_name': app_name, 'widget_name': widget_name, 'widget_title': widget_class.get_widget_title(), 'form_content': widget_form_content, 'form_id': '%s_%s_%d' % (app_name, widget_name, uuid1()), 'form_active': form_active, }) context = { 'widget_data': data, 'widget_conf_id': widget.id, } context.update(extra_context) return render(request, template_name, context)