def configure_widget(request, content_type_id, object_id): """ Get a widget in page and call it's configure method This view updates the page. """ def response(request, *args, **kwargs): if request.is_ajax(): return ConfigureWidgetJsonResponse(*args, **kwargs) else: return http.HttpResponseRedirect(reverse('home_page')) page = get_page(request.user, request.session, for_update=True) widget, wil = get_widget_or_404(page, content_type_id, object_id) form = widget.display_form(request.POST, request.FILES) if not form.is_valid(): errs = dict( (key, map(force_unicode, value)) for key, value in form.errors.items() ) return response(request, 'KO', errs) wil.configure(form.cleaned_data, widget.get_widget_in_page_configuration) page.save() return response(request, 'OK', [])
def configure_widget(request, content_type_id, object_id): """ Get a widget in page and call it's configure method This view updates the page. """ def response(request, *args, **kwargs): if request.is_ajax(): return ConfigureWidgetJsonResponse(*args, **kwargs) else: return http.HttpResponseRedirect(reverse('home_page')) page = get_page(request.user, request.session, for_update=True) widget, wil = get_widget_or_404(page, content_type_id, object_id) form = widget.display_form(request.POST, request.FILES) if not form.is_valid(): errs = dict((key, map(force_unicode, value)) for key, value in form.errors.items()) return response(request, 'KO', errs) wil.configure(form.cleaned_data, widget.get_widget_in_page_configuration) page.save() return response(request, 'OK', [])
def set_state(request, content_type_id, object_id): """ This view updates the page. """ page = get_page(request.user, request.session, for_update=True) widget, wil = get_widget_or_404(page, content_type_id, object_id) try: wil.state = request.POST.get('state') page.save() except (ValueError), e: # TODO DataError return http.HttpResponseBadRequest(json.serialize(['KO', 'Invalid state value']))
def get_display_form(request, content_type_id, object_id): """ This view does not update the page. """ page = get_page(request.user, request.session) widget, wil = get_widget_or_404(page, content_type_id, object_id) form = widget.display_form(None, initial=wil.config) response = render_to_response(widget.get_template(widget.DISPLAY_FORM_TEMPLATE), {'form': form, 'widget': widget}, context_instance=RequestContext(request)) add_never_cache_headers( response ) return response
def widget_action(request, content_type_id, object_id, to_call): """ Get a widget in page and call it's to_call method This view can update the page by POST request. """ for_update = request.method == 'POST' page = get_page(request.user, request.session, for_update=for_update) widget, wil = get_widget_or_404(page, content_type_id, object_id) try: result = widget.action_dispatcher(request, wil, to_call=to_call) except WidgetFetchDataFailedException, e: return render_to_response('widgets.widget/content500.html')
def set_state(request, content_type_id, object_id): """ This view updates the page. """ page = get_page(request.user, request.session, for_update=True) widget, wil = get_widget_or_404(page, content_type_id, object_id) try: wil.state = request.POST.get('state') page.save() except (ValueError), e: # TODO DataError return http.HttpResponseBadRequest( json.serialize(['KO', 'Invalid state value']))
def get_display_form(request, content_type_id, object_id): """ This view does not update the page. """ page = get_page(request.user, request.session) widget, wil = get_widget_or_404(page, content_type_id, object_id) form = widget.display_form(None, initial=wil.config) response = render_to_response(widget.get_template( widget.DISPLAY_FORM_TEMPLATE), { 'form': form, 'widget': widget }, context_instance=RequestContext(request)) add_never_cache_headers(response) return response
def get_content(request, content_type_id, object_id): """ Get rendered widget content This view always fetches data for now and generates cache. This view does not update the page. """ lock_key = 'get_content_lock:%s:%s' % (content_type_id, object_id) lock = cache.get(lock_key) if lock: # content locked - failed import - do not download return render_to_response('widgets.widget/content500.html') page = get_page(request.user, request.session) widget, wil = get_widget_or_404(page, content_type_id, object_id) tab = request.GET.get('tab', None) tab = request.POST.get('tab', tab) old = socket.getdefaulttimeout() socket.setdefaulttimeout(5) try: # it would be better to lock here, but we cannot do so because of mailwidget out = http.HttpResponse( widget.get_content(wil.config, RequestContext(request), allow_fetch=True, tab=tab)) cache.delete(lock_key) return out except WidgetFetchDataFailedException, e: # TODO: # or should this be cached as well? # there will be inconsistency with mypage.widgets.templatetags.widgets.EMPTY_CONTENT log.warning(u'Fetching for %s failed (%r).', widget, e) # lock for 5 minutes to avoid other threads from doing the same mistake cache.set(lock_key, 'LOCKED', 300) return render_to_response('widgets.widget/content500.html')
def get_content(request, content_type_id, object_id): """ Get rendered widget content This view always fetches data for now and generates cache. This view does not update the page. """ lock_key = 'get_content_lock:%s:%s' % (content_type_id, object_id) lock = cache.get(lock_key) if lock: # content locked - failed import - do not download return render_to_response('widgets.widget/content500.html') page = get_page(request.user, request.session) widget, wil = get_widget_or_404(page, content_type_id, object_id) tab = request.GET.get('tab', None) tab = request.POST.get('tab', tab) old = socket.getdefaulttimeout() socket.setdefaulttimeout(5) try: # it would be better to lock here, but we cannot do so because of mailwidget out = http.HttpResponse(widget.get_content(wil.config, RequestContext(request), allow_fetch=True, tab=tab)) cache.delete(lock_key) return out except WidgetFetchDataFailedException, e: # TODO: # or should this be cached as well? # there will be inconsistency with mypage.widgets.templatetags.widgets.EMPTY_CONTENT log.warning(u'Fetching for %s failed (%r).', widget, e) # lock for 5 minutes to avoid other threads from doing the same mistake cache.set(lock_key, 'LOCKED', 300) return render_to_response('widgets.widget/content500.html')