def render_to_response_with_refresh(self, relation_id, app_obj, msg=None): id_items = check_object_html_id(relation_id, types=[settings.SLUG_PLUGIN, settings.SLUG_APP]) # APP rendering if id_items[0] == settings.SLUG_APP: # Get placeholder slug in page placeholder_slug = app_obj.page.get().placeholder_slug # Plugin Rendering else: # Get placeholder_slug in relation placeholder_slug = app_obj.pages.get(pages=self.request.page).placeholder_slug placeholder_slug_items = check_placeholder_html_id(placeholder_slug) layout_section_slug = placeholder_slug_items[0] placeholder_id = placeholder_slug_items[2] rendering_context = RenderingContext(self.request) html = rendering_context.get_html_placeholder(layout_section_slug, placeholder_id, context=RequestContext(self.request)) datas = {'html': html, 'placeholder_slug': placeholder_slug} if msg: datas['msg'] = msg response = Response(status.HTTP_200_OK, datas) return self.render(response)
def post(self, request, relation_html_id): """ Update plugin modifications. If modifications are correct return confirmation message and the new render of the layout section; if not, return the plugin form with error messages Parameters : - relation_html_id : PluginRelation Id POST parameters : - form fields - csrf token """ pk = check_object_html_id(relation_html_id)[1] try: plugin_relation = PluginRelation.objects.filter( pages__website__exact=request.website, id__exact=pk)[0] except IndexError: raise Http404 # Create the plugin form plugin = plugin_relation.content_object PluginFormClass = plugin.get_admin_form() form = PluginFormClass(request.POST, instance=plugin) if form.is_valid(): plugin = form.save() placeholder_slug_items = check_placeholder_html_id( plugin_relation.placeholder_slug) layout_section_slug = placeholder_slug_items[0] rendering_context = RenderingContext(request) html_rendering = rendering_context.get_html_layout(layout_section_slug) response = Response(status.HTTP_200_OK, {"msg": MESSAGES.get('item_edit_success',""), 'html': html_rendering, 'layout_section_slug': layout_section_slug}) return self.render(response) else: # Invalid form => 400 BAD REQUEST # with forms (and errors..) html = render_to_string('administration/plugin/plugin-edit.html', {'form': form, 'plugin': plugin, 'plugin_relation_html_id': relation_html_id}, context_instance = RequestContext(request)) raise ErrorResponse(status.HTTP_400_BAD_REQUEST, {'msg': MESSAGES.get('invalid_data', ""), 'html': html})
def post(self, request, page_pk=None): """ Save App Page modifications. If correct return confirmation message if not, return the form with error messages """ if page_pk is not None: try: page = request.website.pages.select_related()\ .get(pk=page_pk) app_page = page.app_page_object except Page.DoesNotExist: raise ErrorResponse(status.HTTP_400_BAD_REQUEST, {'msg': MESSAGES.get('default_error', "")}) else: app_page = request.page.app_page_object page = request.page # Page App Admin Form PageAppForm = app_page.get_admin_form() form = PageAppForm(request.POST, instance=app_page) if form.is_valid(): new_app_page = form.save() # If page is the current page, # refresh the layout section if request.page == page: # Get layout slug placeholder_slug_items = check_placeholder_html_id( page.placeholder_slug) layout_section_slug = placeholder_slug_items[0] # Rendering layout section rendering_context = RenderingContext(request) html_rendering = rendering_context.get_html_layout( layout_section_slug) # Send response data_context = {'msg': MESSAGES.get('app_edit_success', ""), 'html': html_rendering, 'layout_section_slug': layout_section_slug} # Check if the page manager have to be displayed if page_pk: data_context['refresh_pages_list'] = True response = Response(status.HTTP_200_OK, data_context) else: data_context = {'msg': MESSAGES.get('app_edit_success', "")} # Check if the page manager have to be displayed if page_pk: data_context['refresh_pages_list'] = True response = Response(status.HTTP_200_OK, data_context) return self.render(response) # render_page = page.render_page(request) # if render_page.status_code == 200: # response = Response(status.HTTP_200_OK, # {"msg": MESSAGES.get('app_edit_success', ""), # 'html': render_page.content, # 'medias': render_page.medias}) # elif render_page.status_code in [301, 302]: # response = Response(status.HTTP_202_ACCEPTED, # {"msg": MESSAGES.get('redirection', ""), # 'location': render_page['location']}) # If form not valid => reload the edit form with messages else: data_context = {'form': form, 'object': app_page} if page_pk: data_context['page'] = page html = render_to_string('administration/app/app-edit.html', data_context, context_instance=RequestContext(request)) raise ErrorResponse(status.HTTP_400_BAD_REQUEST, {'msg': MESSAGES.get('invalid_data', ""), 'html': html})
def get(self, request, relation_html_id=None): """ Return plugin form to create or update a plugin. If `relation_html_id` is not None, we get the form of the existing plugin,else we get an empty form in order to create a new plugin. Parameters : - relation_html_id : Html ID of the plugin relation, e.g. 'plugin-relation-2' where '2' is the PluginRelation ID. GET parameters : (required if `pk` is None) - placeholder_id : Html ID of the placeholder, e.g. 'content-placeholder-1'. - plugin_type : Content Type ID of the new plugin. """ # ---- # Get a form of an existing plugin # ---- if relation_html_id is not None: # Get the ID relation pk = check_object_html_id(relation_html_id)[1] try: obj = PluginRelation.objects.filter( pages__website__exact=request.website, id__exact=pk)[0] except IndexError: raise Http404 # Create the plugin form plugin = obj.content_object PluginFormClass = plugin.get_admin_form() form = PluginFormClass(instance=plugin) # Get the html of the form content = render_to_string('administration/plugin/plugin-edit.html', {'form': form, 'plugin': plugin, 'plugin_relation_html_id': relation_html_id}, context_instance=RequestContext(request)) response = Response(status.HTTP_200_OK, {'html': content,}) return self.render(response) # ---- # Get an empty form to create a new plugin # ---- placeholder_id = request.GET.get('placeholder_id', None) plugin_type = request.GET.get('plugin_type', None) if placeholder_id and plugin_type: # Check if placeholder ID is valid check_placeholder_html_id(placeholder_id) try: # Get class of the plugin type plugin_ct = CTA().get_for_pk(plugin_type) PluginClass = plugin_ct.model_class() # Create an empty admin form PluginFormClass = PluginClass().get_admin_form() plugin_form = PluginFormClass() # Get html code of the form content = render_to_string('administration/plugin/plugin-create.html', {'form': plugin_form, 'placeholder_id': placeholder_id, 'plugin_type': plugin_type,}, context_instance=RequestContext(request)) response = Response(status.HTTP_200_OK, {'html': content,}) return self.render(response) except ContentType.DoesNotExist: raise ErrorResponse(status.HTTP_400_BAD_REQUEST, {'msg': MESSAGES.get('default_error', "")}) # Bad parameters => 400 else: raise ErrorResponse(status.HTTP_400_BAD_REQUEST, {'msg': MESSAGES.get('default_error', "")})
def post(self, request): """ Update a PluginRelation. Parameters : - placeholder_id : HTML ID of the new placeholder, eg "content-placeholder-1" - plugins_order[] : Ordered list of plugins HTML IDs in the new placeholder """ placeholder_html_id = request.POST.get('placeholder_id', None) plugins_order = request.POST.getlist('plugins_order[]') if placeholder_html_id and plugins_order: # Check placeholder HTML ID check_placeholder_html_id(placeholder_html_id, extras_id=[settings.HTML_ID_PLACEHOLDER_CLIPBOARD,]) i = 0 for plugin_id in plugins_order: # Check plugin HTML ID plugin_type, relation_id = check_object_html_id(plugin_id, types=[settings.SLUG_PLUGIN, settings.SLUG_APP]) # Be careful, can be a Page object or a relation object # In case you are moving the app and not a plugin if plugin_type == settings.SLUG_APP: if placeholder_html_id == settings.HTML_ID_PLACEHOLDER_CLIPBOARD: raise ErrorResponse(status.HTTP_400_BAD_REQUEST, {'msg': MESSAGES.get('default_error', "")}) # Get page object to manage app order obj = request.page # Plugin object else: try: obj = PluginRelation.objects.filter(pages__website__exact=request.website, id__exact=relation_id)[0] # 1. This new placeholder_html_id is website placeholder # - Add all pages # - Activate auto creation on new pages if not (is_page_placeholder_html_id(placeholder_html_id) or placeholder_html_id == settings.HTML_ID_PLACEHOLDER_CLIPBOARD): obj.pages.add(*request.website.pages.all()) if not obj.display_on_new_pages: obj.display_on_new_pages = True obj.save() else: # 2. This new placeholder_html_id is page placeholder # - Delete all pages # - Add current pages # - Deactivate auto creation in new page obj.pages.clear() obj.pages.add(request.page) if obj.display_on_new_pages: obj.display_on_new_pages = False obj.save() except IndexError: raise ErrorResponse(status.HTTP_400_BAD_REQUEST, {'msg': MESSAGES.get('default_error', "")}) # Update order obj.plugin_order = i if plugin_type == settings.SLUG_APP: if i > 5: obj.plugin_order -= 5 else: i = i + 10 # Update placeholder slug obj.placeholder_slug = placeholder_html_id obj.save() # Send a 200 Response response = Response(status.HTTP_200_OK, {"msg": MESSAGES.get('items_move_success', '')}) return self.render(response) # Bad parameters => 400 BAR REQUEST else: raise ErrorResponse(status.HTTP_400_BAD_REQUEST, {'msg': MESSAGES.get('default_error', "")})
def put(self, request): """ Create a new plugin. If modifications are correct return confirmation message and the new render of the layout section; if not, return the plugin form with error messages PUT parameters : - placeholder_id : Html ID of the placeholder, e.g. 'content-placeholder-1'. - plugin_type : Content type ID of the new plugin. - form fields - csrf token """ # Get PUT parameters request.PUT = self.DATA.copy() placeholder_html_id = request.PUT.get('placeholder_id', None) plugin_type = request.PUT.get('plugin_type', None) if placeholder_html_id and plugin_type: # Check if placeholder ID is valid placeholder_slug_items = check_placeholder_html_id(placeholder_html_id) layout_section_slug = placeholder_slug_items[0] # Get form of the plugin type try: content_type = CTA().get_for_pk(plugin_type) except ContentType.DoesNotExist: raise ErrorResponse(status.HTTP_400_BAD_REQUEST, {'msg': MESSAGES.get('default_error', "")}) PluginClass = content_type.model_class() plugin = PluginClass() PluginFormClass = plugin.get_admin_form() form = PluginFormClass(request.PUT, instance=plugin) if form.is_valid(): # Creation of the new plugin new_plugin = form.save() # Creation of the relation display_on_new_pages = (not is_page_placeholder_html_id(placeholder_html_id)) relation = PluginRelation.objects.create(content_object=new_plugin, placeholder_slug= placeholder_html_id, display_on_new_pages=display_on_new_pages) relation.pages.add(request.page) # At the moment, we displayed it on everypage if display_on_new_pages: for page in request.website.pages.all(): relation.pages.add(page) # Set right order try: last_relation = PluginRelation.objects.filter( pages=request.page, placeholder_slug=placeholder_html_id).order_by('-plugin_order')[0] plugin_order = last_relation.plugin_order + 10 except IndexError: plugin_order = 0 relation.plugin_order = plugin_order # Saving modifications relation.save() rendering_context = RenderingContext(request) plugin_html_medias = rendering_context\ .get_html_medias_for_plugin_relation(relation) html_rendering = rendering_context.get_html_layout(layout_section_slug) # Sending response response = Response(status.HTTP_200_OK, {'msg': MESSAGES.get('item_edit_success', ''), 'html': html_rendering, 'layout_section_slug': layout_section_slug, 'medias': plugin_html_medias}) return self.render(response) # Invalid Form => 400 BAD REQUEST else: html = render_to_string('administration/plugin/plugin-create.html', {'form': form, 'placeholder_id': placeholder_html_id, 'plugin_type': plugin_type}, context_instance=RequestContext(request)) raise ErrorResponse(status.HTTP_400_BAD_REQUEST, {'msg': MESSAGES.get('invalid_data', ""), 'html': html}) # Bad parameters => 400 BAD REQUEST else: raise ErrorResponse(status.HTTP_400_BAD_REQUEST, {'msg': MESSAGES.get('default_error', "")})
def post(self, request, page_pk=None): """ Save App Page modifications. If correct return confirmation message if not, return the form with error messages """ if page_pk is not None: try: page = request.website.pages.select_related()\ .get(pk=page_pk) app_page = page.app_page_object except Page.DoesNotExist: raise ErrorResponse(status.HTTP_400_BAD_REQUEST, {'msg': MESSAGES.get('default_error', "")}) else: app_page = request.page.app_page_object page = request.page # Page App Admin Form PageAppForm = app_page.get_admin_form() form = PageAppForm(request.POST, instance=app_page) if form.is_valid(): new_app_page = form.save() # If page is the current page, # refresh the layout section if request.page == page: # Get layout slug placeholder_slug_items = check_placeholder_html_id( page.placeholder_slug) layout_section_slug = placeholder_slug_items[0] # Rendering layout section rendering_context = RenderingContext(request) html_rendering = rendering_context.get_html_layout( layout_section_slug) # Send response data_context = { 'msg': MESSAGES.get('app_edit_success', ""), 'html': html_rendering, 'layout_section_slug': layout_section_slug } # Check if the page manager have to be displayed if page_pk: data_context['refresh_pages_list'] = True response = Response(status.HTTP_200_OK, data_context) else: data_context = {'msg': MESSAGES.get('app_edit_success', "")} # Check if the page manager have to be displayed if page_pk: data_context['refresh_pages_list'] = True response = Response(status.HTTP_200_OK, data_context) return self.render(response) # render_page = page.render_page(request) # if render_page.status_code == 200: # response = Response(status.HTTP_200_OK, # {"msg": MESSAGES.get('app_edit_success', ""), # 'html': render_page.content, # 'medias': render_page.medias}) # elif render_page.status_code in [301, 302]: # response = Response(status.HTTP_202_ACCEPTED, # {"msg": MESSAGES.get('redirection', ""), # 'location': render_page['location']}) # If form not valid => reload the edit form with messages else: data_context = {'form': form, 'object': app_page} if page_pk: data_context['page'] = page html = render_to_string('administration/app/app-edit.html', data_context, context_instance=RequestContext(request)) raise ErrorResponse(status.HTTP_400_BAD_REQUEST, { 'msg': MESSAGES.get('invalid_data', ""), 'html': html })