def post(self, request, pk): domain = get_object_or_404(request.website.ndds, pk=pk) if 'primary' in request.POST: request.website.domain = domain request.website.save() response = Response( status.HTTP_202_ACCEPTED, { 'location': 'http://%s%s#admin' % (domain.domain, request.page.get_absolute_url()) }) else: form = DomainWAForm(request.POST, instance=domain) if form.is_valid(): form.save() response = Response( status.HTTP_200_OK, {'msg': MESSAGES.get('item_edit_success', "")}) else: html = render_to_string( 'administration/website/domain.html', { 'form': form, 'edit': True }, context_instance=RequestContext(request)) response = Response(status.HTTP_400_BAD_REQUEST, { "html": html, 'msg': MESSAGES.get('default_error', "") }) return self.render(response)
def post(self, request, pk): domain = get_object_or_404(request.website.ndds, pk=pk) if 'primary' in request.POST: request.website.domain = domain request.website.save() response = Response(status.HTTP_202_ACCEPTED, {'location': 'http://%s%s#admin' % (domain.domain, request.page.get_absolute_url())}) else: form = DomainWAForm(request.POST, instance=domain) if form.is_valid(): form.save() response = Response(status.HTTP_200_OK, {'msg': MESSAGES.get('item_edit_success', "")}) else: html = render_to_string('administration/website/domain.html', {'form': form, 'edit': True}, context_instance = RequestContext(request)) response = Response(status.HTTP_400_BAD_REQUEST, {"html": html, 'msg': MESSAGES.get('default_error', "")}) return self.render(response)
def post(self, request, relation_id, plugin, action_pk=None): links_html_id = request.POST.getlist('links_id[]') if links_html_id: # New ordering items order = 1 for link_id in map(lambda s: s.split('-')[1], links_html_id): try: obj = Link.objects.get(pk=link_id) if obj.plugin == plugin: obj.order = order obj.save() order += 1 except Link.DoesNotExist: pass # Rendering new content html = request.page.render_page(request).content if isinstance(html, HTMLRendering): html = html.content response = Response( status.HTTP_200_OK, { 'msg': MESSAGES.get('items_edit_success', ""), 'html': html, 'placeholder_type': placeholder_type, 'html_id': relation_id }) return self.render(response) else: raise ErrorResponse(status.HTTP_400_BAD_REQUEST, {'msg': MESSAGES.get('default_error', "")})
def post(self, request): """ Handle POST requests, managing the user authentication. """ try: user = User.objects.get(email=request.POST['email']) user = authenticate(username=user.username, password=request.POST['password']) if user is not None: if user.is_active: login(request, user) response = Response(status.HTTP_200_OK, {"msg": MESSAGES.get('user_authenticated', "")}) else: response = Response(status.HTTP_400_BAD_REQUEST, {"msg": MESSAGES.get('inactive_user', "")}) else: response = Response(status.HTTP_400_BAD_REQUEST, {"msg": MESSAGES.get('bad_login_pwd', "")}) except User.DoesNotExist: response = Response(status.HTTP_400_BAD_REQUEST, {"msg": MESSAGES.get('bad_login_pwd', "")}) return self.render(response)
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 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, relation_id, plugin, action_pk=None): links_html_id = request.POST.getlist('links_id[]') if links_html_id: # New ordering items order = 1 for link_id in map(lambda s: s.split('-')[1], links_html_id): try: obj = Link.objects.get(pk=link_id) if obj.plugin == plugin: obj.order = order obj.save() order += 1 except Link.DoesNotExist: pass # Rendering new content html = request.page.render_page(request).content if isinstance(html, HTMLRendering): html = html.content response = Response(status.HTTP_200_OK, {'msg': MESSAGES.get('items_edit_success', ""), 'html': html, 'placeholder_type': placeholder_type, 'html_id': relation_id}) return self.render(response) else: raise ErrorResponse(status.HTTP_400_BAD_REQUEST, {'msg': MESSAGES.get('default_error', "")})
def put(self, request, pk=None): post_values = self.DATA.copy() post_values['website'] = request.website.id form = PageWAForm(post_values) if form.is_valid(): page = form.save() # Add the new page on auto_display PluginRelation plugins_relation = PluginRelation.objects.filter( display_on_new_pages=True, pages__website=request.website) for plugin_relation in plugins_relation: plugin_relation.pages.add(page) response = Response( status.HTTP_202_ACCEPTED, { "msg": MESSAGES.get('redirection', ""), 'location': page.get_absolute_url() }) else: content = render_to_string( 'administration/page/page-create.html', { 'form': form, }, context_instance=RequestContext(request)) response = Response(status.HTTP_400_BAD_REQUEST, { "html": content, "msg": MESSAGES.get('default_error', "") }) 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, relation_id, app_obj, object_pk, *args, **kwargs): Model = self.get_model() try: obj = Model.objects.get(pk=object_pk) except Model.DoesNotExist: raise ErrorResponse(status.HTTP_404_NOT_FOUND, {'msg': MESSAGES.get('default_error', '')}) else: obj.delete() return self.render_to_response_with_refresh(relation_id, app_obj, msg=MESSAGES.get('item_delete_success', ''))
def delete(self, request, pk): domain = get_object_or_404(request.website.ndds, pk=pk) # You cannot delete the primary domain if request.website.domain == domain: response = Response(status.HTTP_400_BAD_REQUEST, {'msg': MESSAGES.get('default_error', "")}) else: domain.delete() response = Response(status.HTTP_200_OK, {"msg": MESSAGES.get('item_delete_success', "")}) # Send response return self.render(response)
def delete(self, request, pk): domain = get_object_or_404(request.website.ndds, pk=pk) # You cannot delete the primary domain if request.website.domain == domain: response = Response(status.HTTP_400_BAD_REQUEST, {'msg': MESSAGES.get('default_error', "")}) else: domain.delete() response = Response( status.HTTP_200_OK, {"msg": MESSAGES.get('item_delete_success', "")}) # Send response return self.render(response)
def base_view(self, request, html_id_object, url_action): """ Basic View of actions admin. This method gets the object related to the request and return the action asked. """ # Get and check app/plugin object HTML ID # Types accepted : PluginRelation or App # If slug not valid => raise HTTP_400_BAD_REQUEST object_type, object_id = check_object_html_id( html_id_object, types=[settings.SLUG_PLUGIN, settings.SLUG_APP]) # Case #1 - Object Type : PluginRelation if object_type == settings.SLUG_PLUGIN: # Get plugin relation try: obj_relation = PluginRelation.objects\ .get(id__exact=object_id) except PluginRelation.DoesNotExist: # If the plugin is not found => 404 raise ErrorResponse(status.HTTP_404_NOT_FOUND, {'msg': MESSAGES.get('default_error', "")}) # Get plugin object obj = obj_relation.content_object # Case #2 - Object Type : App # Necessarily : object_type == settings.SLUG_APP: else: # Get app object obj = request.page.app_page_object # We check that slug parameter is correct if obj.pk != int(object_id): raise ErrorResponse(status.HTTP_404_NOT_FOUND, {'msg': MESSAGES.get('default_error', "")}) # Formatting url action # (add '/' at the begining and the ending) if url_action[0] != '/': url_action = '/' + url_action if url_action[-1] != '/': url_action = url_action + '/' # Dispatcher View try: match = resolve(url_action, urlconf=obj.get_actions_urlconf()) return match.func(request, html_id_object, obj, **match.kwargs) except Http404: raise ErrorResponse(status.HTTP_404_NOT_FOUND, {'msg': MESSAGES.get('action_not_found', "")})
def get(self, request, page_pk=None): """ Return the form to edit a app page. If 'page_pk' parameter is None, returns the edit form of the current page (ie request.page), else returns the edit form of the page with the id 'page_pk'. """ # Get page with ID 'page_pk' 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: page = request.page app_page = request.page.app_page_object # Page App Admin Form PageAppForm = app_page.get_admin_form() form = PageAppForm(instance=app_page) data_context = {'form': form, 'object': app_page} if page_pk: data_context['page'] = page content = render_to_string('administration/app/app-edit.html', data_context, context_instance=RequestContext(request)) response = Response(status.HTTP_200_OK, { 'html': content, }) return self.render(response)
def put(self, request, relation_id, app_obj, object_pk=None, *args, **kwargs): self.object = None form = self.get_form_class()(**self.get_form_kwargs()) if form.is_valid(): obj = form.save(commit=False) obj.album = Album.objects.get(pk=kwargs['album_pk']) form.save() return self.render_to_response_with_refresh(relation_id, app_obj, msg=MESSAGES.get('item_creation_success', '')) else: extra_context = {'form': form, 'add': True} return self.render_to_response(self.get_context_data(**extra_context), status_code=status.HTTP_400_BAD_REQUEST, msg=MESSAGES.get('invalid_data', ''))
def get(self, request, page_pk=None): """ Return the form to edit a app page. If 'page_pk' parameter is None, returns the edit form of the current page (ie request.page), else returns the edit form of the page with the id 'page_pk'. """ # Get page with ID 'page_pk' 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: page = request.page app_page = request.page.app_page_object # Page App Admin Form PageAppForm = app_page.get_admin_form() form = PageAppForm(instance=app_page) data_context = {'form': form, 'object': app_page} if page_pk: data_context['page'] = page content = render_to_string('administration/app/app-edit.html', data_context, context_instance=RequestContext(request)) response = Response(status.HTTP_200_OK, {'html': content,}) return self.render(response)
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 put(self, request): form = DomainWAForm(self.DATA) if form.is_valid(): site = form.save() request.website.ndds.add(site) response = Response(status.HTTP_200_OK, {'msg': MESSAGES.get('item_creation_success', "")}) else: html = render_to_string('administration/website/domain.html', {'form': form, 'edit': False}, context_instance = RequestContext(request)) response = Response(status.HTTP_400_BAD_REQUEST, {"html": html, 'msg': MESSAGES.get('default_error', "")}) return self.render(response)
def post(self, request, relation_id, app_obj, action_obj_pk=None, *args, **kwargs): objects_order = request.POST.getlist('action_objects_order[]') order = 1 for object_id in objects_order: object_pk = object_id.split('action-object-')[1] obj = self.get_object(object_pk) try: setattr(obj, self.sortable_field, order) except AttributeError: raise ErrorResponse(status.HTTP_400_BAD_REQUEST, {'msg': MESSAGES.get('default_error', "")}) obj.save() order += 1 return self.render_to_response_with_refresh(relation_id, app_obj, msg=MESSAGES.get('item_edit_success', ''))
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 post(self, request, pk=None): """ Change the page Theme """ request.website.theme = request.POST['theme_slug'] request.website.save() response = Response(status.HTTP_200_OK, {"msg": MESSAGES.get('theme_edit_succes', "")}) return self.render(response)
def put(self, request): form = DomainWAForm(self.DATA) if form.is_valid(): site = form.save() request.website.ndds.add(site) response = Response( status.HTTP_200_OK, {'msg': MESSAGES.get('item_creation_success', "")}) else: html = render_to_string('administration/website/domain.html', { 'form': form, 'edit': False }, context_instance=RequestContext(request)) response = Response(status.HTTP_400_BAD_REQUEST, { "html": html, 'msg': MESSAGES.get('default_error', "") }) return self.render(response)
def post(self, request): form = Website_ReferencementForm(request.POST, instance=request.website) if form.is_valid(): form.save() response = Response(status.HTTP_200_OK, {}) return self.render(response) else: raise ErrorResponse(status.HTTP_400_BAD_REQUEST, {'msg': MESSAGES.get('default_error', "")})
def put(self, request, relation_id, app_obj, object_pk=None, *args, **kwargs): self.object = None form = self.get_edit_form(*args, app_obj=app_obj) if form.is_valid(): obj = form.save(commit=False) field_name = '%s_id' % self.get_obj_field(app_obj) if hasattr(obj, field_name): setattr(obj, field_name, app_obj.pk) form.save() # Refresh placeholder return self.render_to_response_with_refresh(relation_id, app_obj, msg=MESSAGES.get('item_creation_success', '')) else: extra_context = {'form': form, 'add': True} return self.render_to_response(self.get_context_data(**extra_context), status_code=status.HTTP_400_BAD_REQUEST, msg=MESSAGES.get('invalid_data', ''))
def post(self, request): form = Website_AnalyticsForm(request.POST, instance=request.website) if form.is_valid(): form.save() response = Response(status.HTTP_200_OK, {}) return self.render(response) else: raise ErrorResponse(status.HTTP_400_BAD_REQUEST, {'msg': MESSAGES.get('default_error', "")})
def put(self, request, relation_id, app_obj, object_pk=None, *args, **kwargs): self.object = None form = self.get_form_class()(**self.get_form_kwargs()) if form.is_valid(): obj = form.save(commit=False) obj.album = Album.objects.get(pk=kwargs['album_pk']) form.save() return self.render_to_response_with_refresh( relation_id, app_obj, msg=MESSAGES.get('item_creation_success', '')) else: extra_context = {'form': form, 'add': True} return self.render_to_response( self.get_context_data(**extra_context), status_code=status.HTTP_400_BAD_REQUEST, msg=MESSAGES.get('invalid_data', ''))
def put(self, request, pk=None): post_values = self.DATA.copy() post_values['website'] = request.website.id form = PageWAForm(post_values) if form.is_valid(): page = form.save() # Add the new page on auto_display PluginRelation plugins_relation = PluginRelation.objects.filter(display_on_new_pages=True, pages__website=request.website) for plugin_relation in plugins_relation: plugin_relation.pages.add(page) response = Response(status.HTTP_202_ACCEPTED, {"msg": MESSAGES.get('redirection', ""), 'location': page.get_absolute_url()}) else: content = render_to_string('administration/page/page-create.html', {'form': form,}, context_instance = RequestContext(request)) response = Response(status.HTTP_400_BAD_REQUEST, {"html": content, "msg": MESSAGES.get('default_error', "")}) return self.render(response)
def post(self, request, relation_id, app_obj, object_pk, *args, **kwargs): self.kwargs['pk'] = object_pk self.object = self.get_object() # If the current path of the page is concerned by the edited object check_redirection = False if hasattr(self.object, 'get_absolute_url'): obj_abs_url_before_edit = self.object.get_absolute_url() complete_page_url = request.path_info.split(settings.URL_ADMIN_SEP)[0] if complete_page_url == obj_abs_url_before_edit: check_redirection = True form = self.get_edit_form(instance=self.object, app_obj=app_obj) if form.is_valid(): form.save() if check_redirection: # We must check if redirection is needed obj_abs_url_after_edit = self.object.get_absolute_url() if obj_abs_url_before_edit != obj_abs_url_after_edit: # The absolute url object was changed, so reload page response = Response(status.HTTP_202_ACCEPTED, {"msg": MESSAGES.get('redirection', ""), 'location': obj_abs_url_after_edit}) return self.render(response) # Response with refresh placeholder return self.render_to_response_with_refresh(relation_id, app_obj, msg=MESSAGES.get('item_edit_success', '')) else: extra_context = {'form': form, 'add': False} return self.render_to_response(self.get_context_data(**extra_context), status_code=status.HTTP_400_BAD_REQUEST, msg=MESSAGES.get('invalid_data', ''))
def post(self, request): if 'old_password' in request.POST and request.POST[ 'old_password'] == "": userform = EditCurrentUser(request.user, request.POST) if userform.is_valid(): userform.save(request.user) response = Response(status.HTTP_200_OK, {}) return self.render(response) else: passform = PasswordChangeForm(request.user) html = render_to_string( 'administration/users/current-user.html', { 'userform': userform, 'passform': passform, }, context_instance=RequestContext(request)) response = Response(status.HTTP_400_BAD_REQUEST, {"html": html}) return self.render(response) else: userform = EditCurrentUser(request.user, request.POST) passform = PasswordChangeForm(request.user, data=request.POST) if userform.is_valid() and passform.is_valid(): userform.save(request.user) passform.save(request.user) response = Response(status.HTTP_200_OK, {}) return self.render(response) else: html = render_to_string( 'administration/users/current-user.html', { 'userform': userform, 'passform': passform, }, context_instance=RequestContext(request)) response = Response(status.HTTP_400_BAD_REQUEST, {"html": html}) return self.render(response) raise ErrorResponse(status.HTTP_400_BAD_REQUEST, {'msg': MESSAGES.get('default_error', "")})
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, pk=None): # First, get the directory object and check if is owned by request.website if pk == "0": managed_dir = request.website.file_manager().root else: managed_dir = Directory.objects.get(pk=pk) if managed_dir.get_root().filemanager.get().website.get() != request.website: raise ErrorResponse(status.HTTP_400_BAD_REQUEST, {'msg': MESSAGES.get('default_error', "")}) # Check User display mode for filemanager (NOt implemented yet) # # mode = request.user.profile.get().file_manager_display_mode # Generate templates files_list = render_files_list_html(request, managed_dir) # Return html code width status.HTTP_200_OK response = Response(status.HTTP_200_OK, {"html": files_list}) return self.render(response)
def post(self, request): if 'old_password' in request.POST and request.POST['old_password'] == "": userform = EditCurrentUser(request.user, request.POST) if userform.is_valid(): userform.save(request.user) response = Response(status.HTTP_200_OK, {}) return self.render(response) else: passform = PasswordChangeForm(request.user) html = render_to_string('administration/users/current-user.html', {'userform': userform, 'passform': passform,}, context_instance = RequestContext(request)) response = Response(status.HTTP_400_BAD_REQUEST, {"html": html}) return self.render(response) else: userform = EditCurrentUser(request.user, request.POST) passform = PasswordChangeForm(request.user, data=request.POST) if userform.is_valid() and passform.is_valid(): userform.save(request.user) passform.save(request.user) response = Response(status.HTTP_200_OK, {}) return self.render(response) else: html = render_to_string('administration/users/current-user.html', {'userform': userform, 'passform': passform,}, context_instance = RequestContext(request)) response = Response(status.HTTP_400_BAD_REQUEST, {"html": html}) return self.render(response) raise ErrorResponse(status.HTTP_400_BAD_REQUEST, {'msg': MESSAGES.get('default_error', "")})
def get(self, request, pk=None): # First, get the directory object and check if is owned by request.website if pk == "0": managed_dir = request.website.file_manager().root else: managed_dir = Directory.objects.get(pk=pk) if managed_dir.get_root().filemanager.get().website.get( ) != request.website: raise ErrorResponse(status.HTTP_400_BAD_REQUEST, {'msg': MESSAGES.get('default_error', "")}) # Check User display mode for filemanager (NOt implemented yet) # # mode = request.user.profile.get().file_manager_display_mode # Generate templates files_list = render_files_list_html(request, managed_dir) # Return html code width status.HTTP_200_OK response = Response(status.HTTP_200_OK, {"html": files_list}) return self.render(response)
def delete(self, request, relation_html_id): """ Delete a plugin. Parameters : - pk : PluginRelation.id. """ 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 obj.delete() response = Response(status.HTTP_200_OK, {"msg": MESSAGES.get('plugin_delete_success', '')}) return self.render(response)
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 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)
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 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, 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)
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, 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})