def get(self, request): """ Change and preview the layout of a placeholder """ layout_section_slug = request.GET.get('layout_section_slug', None) layout_template_slug = request.GET.get('layout_template_slug', None) plugin_relation_default = request.GET.getlist('plugin_relation_default[]') plugin_relation_default_placeholder = request.GET.getlist('plugin_relation_default_placeholder[]') if not layout_section_slug: raise _400_BAD_REQUEST rendering_context = RenderingContext(request) html_layout_rendering = rendering_context.get_html_layout(layout_section_slug, template_file_preview=layout_template_slug) # Make data for refresh default plugin_relation_default_data = [] for i in xrange(len(plugin_relation_default)): plugin_relation_default_data.append({ 'id': plugin_relation_default[i], 'layout_slug': plugin_relation_default_placeholder[i].split(settings.HTML_ID_PLACEHOLDER)[0]}) # Refresh of default layout section layout_default_infos_refresh = rendering_context.refresh_default(layout_section_slug, plugin_relation_default_data) response = Response(status.HTTP_200_OK, {"msg": MESSAGES.get('layout_edit_succes', ""), 'default_to_add': layout_default_infos_refresh['add'], 'default_to_delete': layout_default_infos_refresh['delete'], 'layout_section_slug': layout_section_slug, 'html': html_layout_rendering}) return self.render(response)
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 delete(self, request, pk): page = get_object_or_404(request.website.pages, pk=pk) url_home_page = request.website.get_url_home_page() # We can't delete the home page if page.get_absolute_url() == url_home_page: response = Response( status.HTTP_400_BAD_REQUEST, {"msg": MESSAGES.get('delete_home_page_error', "")}) return self.render(response) # Need redirection if page is currently displayed if request.page == page: redirection = True else: redirection = False # Deleting page page.delete() # Make response if redirection: response = Response(status.HTTP_202_ACCEPTED, {'location': url_home_page}) else: # Refresh Menu navigation: navigation_html = RenderingContext(request).html_navigation response = Response( status.HTTP_200_OK, { "id": pk, "navigation_html": navigation_html, "msg": MESSAGES.get('page_delete_success', "") }) # Send response return self.render(response)
def post(self, request): """ Change the page layout """ layout_section_slug = request.POST.get('layout_section_slug', None) layout_template_slug = request.POST.get('layout_template_slug', None) if not layout_section_slug and not layout_template_slug: raise _400_BAD_REQUEST rendering_context = RenderingContext(request) rendering_context.set_layout_template_file(layout_section_slug, layout_template_slug) response = Response(status.HTTP_200_OK, {"msg": MESSAGES.get('layout_edit_succes', "")}) return self.render(response)
def get(self, request): layout_section_slug = request.GET.get('layout_section_slug', None) if not layout_section_slug: response = Response(status.HTTP_400_BAD_REQUEST, {"msg": MESSAGES.get('default_error', "")}) return self.render(response) rendering_context = RenderingContext(request) html_rendering = rendering_context.get_html_layout(layout_section_slug) response = Response(status.HTTP_200_OK, {'html': html_rendering, 'msg': MESSAGES.get('items_edit_success', "")}) return self.render(response)
def get(self, request): layout_section_slug = request.GET.get('layout_section_slug', None) if not layout_section_slug: response = Response(status.HTTP_400_BAD_REQUEST, {"msg": MESSAGES.get('default_error', "")}) return self.render(response) rendering_context = RenderingContext(request) html_rendering = rendering_context.get_html_layout(layout_section_slug) response = Response(status.HTTP_200_OK, { 'html': html_rendering, 'msg': MESSAGES.get('items_edit_success', "") }) return self.render(response)
def get(self, request): """ Return the list of layouts """ layout_section_slug = request.GET.get('layout_section_slug', None) if not layout_section_slug: raise _400_BAD_REQUEST else: rendering_context = RenderingContext(request) current_layout_slug = rendering_context.get_layout_template_slug(layout_section_slug) # current_layout_slug = os.path.basename(current_layout) list_layouts = layouts_info() html = render_to_string('administration/manifest/layouts-list.html', {'list_layouts': list_layouts, 'layout_section_slug': layout_section_slug, 'current_layout_slug': current_layout_slug}, context_instance = RequestContext(request)) response = Response(status.HTTP_200_OK, {"html": html}) return self.render(response)
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 render_website(request, path_info, render_content_only=False): 'Important view that display the requested path of the requested site' # Get the infos of page to display page_infos = get_sha1_params_url(path_info) # Urls not valid if page_infos['error']: return None # Save the app url for url dispatcher request.url_app = page_infos['params'] # ---------------------- # Get concerned page # -- try: request.page = request.website.pages.select_related().get( sha1=page_infos['sha1']) except Page.DoesNotExist: return None # ---------------------- # WebSite admin Url # -- if page_infos['admin']: request.is_admin_url = True try: # Loading admin website views match = resolve(page_infos['params_admin'], urlconf=settings.URLCONF_WEBSITE_ADMIN) return match.func(request, **match.kwargs) except Exception: raise else: request.is_admin_url = False # ---------------------- # WebSite Page rendering # -- if request.page: if request.website.in_maintenance and not request.is_admin: templates = [ os.path.join('themes', request.website.theme, settings.TEMPLATE_MAINTENANCE_DEFAULT), os.path.join('themes', settings.TEMPLATE_MAINTENANCE_DEFAULT) ] response = render_to_response( templates, context_instance=RequestContext(request)) response.status_code = 503 return response # If page is a draft, 404 for visitor and display for administrator if request.page.draft and not request.is_admin: raise Http404 # Rendering Context of Modulo3 rendering_context = RenderingContext(request) # rendering_context.debug() # Maybe App sent redirection or http response. if rendering_context.http_response: return rendering_context.http_response if render_content_only: return render_to_string(rendering_context.theme_templates, {'rendering_context': rendering_context}, context_instance=RequestContext(request)) else: return render_to_response(rendering_context.theme_templates, {'rendering_context': rendering_context}, context_instance=RequestContext(request))
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 })
def post(self, request, pk): """ Modify the page """ # Get page which is currently updated page = get_object_or_404(request.website.pages, pk=pk) # Saving url of current page old_url_current_page = request.page.get_absolute_url() # Settings Refresh refresh_manager = False refresh_page = False msg_user = None # ---------------------- # Moving Page Management # ---------------------- if 'move' in request.POST: if 'previous' in request.POST: page_top = get_object_or_404(request.website.pages, pk=request.POST['previous']) page.move_to(page_top, 'right') else: if 'next' in request.POST: page_top = get_object_or_404(request.website.pages, pk=request.POST['next']) page.move_to(page_top, 'left') else: if 'parent' in request.POST: page_top = get_object_or_404(request.website.pages, pk=request.POST['parent']) page.move_to(page_top, 'first-child') # We save updates. page.save() # Ask refresh manager refresh_manager = True # Messgae fo user msg_user = MESSAGES.get('items_move_success', '') # ---------------------- # Settings page as draft # ---------------------- elif 'draft' in request.POST: page.draft = not page.draft page.save() refresh_manager = True msg_user = MESSAGES.get('page_draft_toggle', '') # ---------------------- # Updating settings page # ---------------------- else: # Get POST values post_values = request.POST.copy() post_values['website'] = request.website.id # Creation of form form = PageWAForm(post_values, instance=page) if form.is_valid(): page = form.save() # We ask content updating if page == request.page: refresh_page = True # Message for user msg_user = MESSAGES.get('app_edit_success', '') else: # We reload the edit form with errors content = render_to_string( 'administration/page/page-edit.html', { 'form': form, 'page': page }, context_instance=RequestContext(request)) response = Response( status.HTTP_203_NON_AUTHORITATIVE_INFORMATION, { "html": content, "msg": MESSAGES.get('default_error', "") }) return self.render(response) # --------------- # Refresh Website # --------------- # Update cache for current page displayed. request.page = get_object_or_404(request.website.pages, pk=request.page.id) # Check if we need reload current page # if current url changed or refresh content asked. new_url_current_page = request.page.get_absolute_url() if old_url_current_page != new_url_current_page or refresh_page: response = Response(status.HTTP_202_ACCEPTED, {'location': new_url_current_page}) return self.render(response) # Else we refresh only page manager and navigation. # Page manager: if refresh_manager: pages_list = request.website.pages.all() page_manager_html = render_to_string( 'administration/page/page-list.html', { 'pages': pages_list, }, context_instance=RequestContext(request)) else: page_manager_html = None navigation_html = RenderingContext(request).html_navigation # Response response = Response( status.HTTP_200_OK, { "manager_html": page_manager_html, "navigation_html": navigation_html, # "page_html": page_content_html, "msg": msg_user }) return self.render(response)