def test_slug_edition_published_can_edit(self): settings.COOP_CMS_CAN_EDIT_ARTICLE_SLUG = True self._log_as_staff_editor() article_class = get_article_class() article = mommy.make(article_class, slug="test", publication=BaseArticle.PUBLISHED) view_name = 'admin:%s_%s_change' % (get_model_app(article_class), get_model_name(article_class)) url = reverse(view_name, args=[article.id]) response = self.client.get(url) self.assertEqual(200, response.status_code) soup = BeautifulSoup(response.content) if is_localized(): from modeltranslation.utils import build_localized_fieldname for (lang, _name) in settings.LANGUAGES: field_name = build_localized_fieldname('slug', lang) self.assertEqual( soup.select("#id_" + field_name)[0]["type"], "text") else: self.assertEqual(soup.select("#id_slug")[0]["type"], "text")
def view_all_articles(request): """all article""" articles_admin_url = newsletters_admin_url = add_article_url = add_newsletter_url = None if request.user.is_staff: article_class = get_article_class() view_name = 'admin:{0}_{1}_changelist'.format(get_model_app(article_class), get_model_name(article_class)) articles_admin_url = reverse(view_name) newsletters_admin_url = reverse('admin:coop_cms_newsletter_changelist') add_newsletter_url = reverse('admin:coop_cms_newsletter_add') article_class = get_article_class() content_type = ContentType.objects.get_for_model(article_class) perm = '{0}.add_{1}'.format(content_type.app_label, content_type.model) if request.user.has_perm(perm): add_article_url = reverse('coop_cms_new_article') return render( request, 'coop_cms/view_all_articles.html', { 'articles': article_class.objects.filter(sites__id=settings.SITE_ID).order_by('-id')[:10], 'newsletters': models.Newsletter.objects.all().order_by('-id')[:10], 'editable': True, 'articles_list_url': articles_admin_url, 'newsletters_list_url': newsletters_admin_url, 'add_article_url': add_article_url, 'add_newsletter_url': add_newsletter_url, } )
def view_all_articles(request): """all article""" articles_admin_url = newsletters_admin_url = add_article_url = add_newsletter_url = None if request.user.is_staff: article_class = get_article_class() view_name = 'admin:{0}_{1}_changelist'.format(get_model_app(article_class), get_model_name(article_class)) articles_admin_url = reverse(view_name) newsletters_admin_url = reverse('admin:coop_cms_newsletter_changelist') add_newsletter_url = reverse('admin:coop_cms_newsletter_add') article_class = get_article_class() content_type = ContentType.objects.get_for_model(article_class) perm = '{0}.add_{1}'.format(content_type.app_label, content_type.model) if request.user.has_perm(perm): add_article_url = reverse('coop_cms_new_article') return render( request, 'coop_cms/view_all_articles.html', { 'articles': article_class.objects.filter(sites__id=settings.SITE_ID).order_by('-id')[:10], 'newsletters': models.Newsletter.objects.all().order_by('-id')[:10], 'editable': True, 'articles_list_url': articles_admin_url, 'newsletters_list_url': newsletters_admin_url, 'add_article_url': add_article_url, 'add_newsletter_url': add_newsletter_url, } )
def django_admin_edit_article(request, context): """show edit article in admin""" if request and request.user.is_staff and 'article' in context: article_class = get_article_class() article = context['article'] view_name = 'admin:{0}_{1}_change'.format(get_model_app(article_class), get_model_name(article_class)) return make_link( reverse(view_name, args=[article.id]), _('Article admin'), 'fugue/table.png', classes=['icon', 'alert_on_click'] )
def django_admin_edit_article(request, context): """show edit article in admin""" if request and request.user.is_staff and 'article' in context: article_class = get_article_class() article = context['article'] view_name = 'admin:{0}_{1}_change'.format( get_model_app(article_class), get_model_name(article_class)) return make_link(reverse(view_name, args=[article.id]), _('Article admin'), 'table', classes=['icon', 'alert_on_click'])
def newsletter_admin(request, context): """show menu""" newsletter = context.get('newsletter') object_class = newsletter.__class__ view_name = 'admin:{0}_{1}_change'.format(get_model_app(object_class), get_model_name(object_class)) try: return make_link( reverse(view_name, args=[newsletter.id]), _('Admin {0}').format(get_model_label(object_class)), 'fugue/table.png', classes=['icon', 'alert_on_click'] ) except NoReverseMatch: pass
def newsletter_admin(request, context): """show menu""" newsletter = context.get('newsletter') object_class = newsletter.__class__ view_name = 'admin:{0}_{1}_change'.format(get_model_app(object_class), get_model_name(object_class)) try: return make_link(reverse(view_name, args=[newsletter.id]), _('Admin {0}').format(get_model_label(object_class)), 'table', classes=['icon', 'alert_on_click']) except NoReverseMatch: pass
def django_admin_edit_object(request, context): """show edit object in admin""" if request and request.user.is_staff and context.get('object', None): obj = context['object'] object_class = obj.__class__ view_name = 'admin:{0}_{1}_change'.format(get_model_app(object_class), get_model_name(object_class)) try: return make_link( reverse(view_name, args=[obj.id]), _('Admin {0}').format(get_model_label(object_class)), 'fugue/table.png', classes=['icon', 'alert_on_click'] ) except NoReverseMatch: pass
def django_admin_edit_object(request, context): """show edit object in admin""" if request and request.user.is_staff and context.get('object', None): obj = context['object'] object_class = obj.__class__ view_name = 'admin:{0}_{1}_change'.format(get_model_app(object_class), get_model_name(object_class)) try: return make_link(reverse(view_name, args=[obj.id]), _('Admin {0}').format( get_model_label(object_class)), 'table', classes=['icon', 'alert_on_click']) except NoReverseMatch: pass
def django_admin_add_object(request, context): """show add object""" if request and request.user.is_staff and (context.get('object', None) or context.get('model', None)): object_class = context.get('model', None) if not object_class: object_class = context['object'].__class__ view_name = 'admin:{0}_{1}_add'.format(get_model_app(object_class), get_model_name(object_class)) try: return make_link( reverse(view_name), _('Add {0}').format(get_model_label(object_class)), 'fugue/table.png', classes=['icon', 'alert_on_click'] ) except NoReverseMatch: pass
def django_admin_list_objects(request, context): """show menu""" if request and request.user.is_staff and (context.get('object', None) or context.get('model', None)): object_class = context.get('model', None) if not object_class: object_class = context['object'].__class__ try: view_name = 'admin:{0}_{1}_changelist'.format( get_model_app(object_class), get_model_name(object_class)) return make_link(reverse(view_name), _('List {0}').format( get_model_label(object_class)), 'table', classes=['icon', 'alert_on_click']) except NoReverseMatch: pass
def django_admin_navtree(request, context): """show menu""" if request and request.user.is_staff: coop_cms_navtrees = context.get('coop_cms_navtrees', None) if coop_cms_navtrees: tree_class = get_navtree_class() admin_tree_name = "{0}_{1}".format(get_model_app(tree_class), get_model_name(tree_class)) if len(coop_cms_navtrees) == 1: tree = coop_cms_navtrees[0] url = reverse('admin:{0}_change'.format(admin_tree_name), args=[tree.id]) label = _('Navigation tree') else: url = reverse('admin:{0}_changelist'.format(admin_tree_name)) label = _('Navigation trees') return make_link( url, label, 'fugue/leaf-plant.png', classes=['icon', 'alert_on_click'] )
def process_nav_edition(request, tree_id): """This handle ajax request sent by the tree component""" if request.method == 'POST' and request.is_ajax() and 'msg_id' in request.POST: try: # Get the current tree tree_class = get_navtree_class() tree = get_object_or_404(tree_class, id=tree_id) # check permissions perm_name = "{0}.change_{1}".format(get_model_app(tree_class), get_model_name(tree_class)) if not request.user.has_perm(perm_name): raise PermissionDenied functions = ( view_navnode, rename_navnode, remove_navnode, move_navnode, add_navnode, get_suggest_list, navnode_in_navigation, ) supported_msg = {} # create a map between message name and handler # use the function name as message id for fct in functions: supported_msg[fct.__name__] = fct # Call the handler corresponding to the requested message response = supported_msg[request.POST['msg_id']](request, tree) # If no exception raise: Success response['status'] = 'success' response.setdefault('message', 'Ok') # if no message defined in response, add something except KeyError as msg: response = {'status': 'error', 'message': "Unsupported message : {0}".format(msg)} except PermissionDenied: response = {'status': 'error', 'message': "You are not allowed to add a node"} except ValidationError as ex: response = {'status': 'error', 'message': ' - '.join(ex.messages)} except Exception as msg: logger.exception("process_nav_edition") response = {'status': 'error', 'message': "An error occured : {0}".format(msg)} # return the result as json object return HttpResponse(json.dumps(response), content_type='application/json') raise Http404
def django_admin_navtree(request, context): """show menu""" if request and request.user.is_staff: coop_cms_navtrees = context.get('coop_cms_navtrees', None) if coop_cms_navtrees: tree_class = get_navtree_class() admin_tree_name = "{0}_{1}".format(get_model_app(tree_class), get_model_name(tree_class)) if len(coop_cms_navtrees) == 1: tree = coop_cms_navtrees[0] url = reverse('admin:{0}_change'.format(admin_tree_name), args=[tree.id]) label = _('Navigation tree') else: url = reverse('admin:{0}_changelist'.format(admin_tree_name)) label = _('Navigation trees') return make_link(url, label, 'leaf', classes=['icon', 'alert_on_click'])
def test_slug_edition_published_can_edit(self): settings.COOP_CMS_CAN_EDIT_ARTICLE_SLUG = True self._log_as_staff_editor() article_class = get_article_class() article = mommy.make(article_class, slug="test", publication=BaseArticle.PUBLISHED) view_name = 'admin:%s_%s_change' % (get_model_app(article_class), get_model_name(article_class)) url = reverse(view_name, args=[article.id]) response = self.client.get(url) self.assertEqual(200, response.status_code) soup = BeautifulSoup(response.content) if is_localized(): from modeltranslation.utils import build_localized_fieldname for (lang, _name) in settings.LANGUAGES: field_name = build_localized_fieldname('slug', lang) self.assertEqual(soup.select("#id_" + field_name)[0]["type"], "text") else: self.assertEqual(soup.select("#id_slug")[0]["type"], "text")
def process_nav_edition(request, tree_id): """This handle ajax request sent by the tree component""" if request.method == 'POST' and request.is_ajax( ) and 'msg_id' in request.POST: try: # Get the current tree tree_class = get_navtree_class() tree = get_object_or_404(tree_class, id=tree_id) # check permissions perm_name = "{0}.change_{1}".format(get_model_app(tree_class), get_model_name(tree_class)) if not request.user.has_perm(perm_name): raise PermissionDenied functions = ( view_navnode, rename_navnode, remove_navnode, move_navnode, add_navnode, get_suggest_list, navnode_in_navigation, ) supported_msg = {} # create a map between message name and handler # use the function name as message id for fct in functions: supported_msg[fct.__name__] = fct # Call the handler corresponding to the requested message response = supported_msg[request.POST['msg_id']](request, tree) # If no exception raise: Success response['status'] = 'success' response.setdefault( 'message', 'Ok') # if no message defined in response, add something except KeyError as msg: response = { 'status': 'error', 'message': "Unsupported message : {0}".format(msg) } except PermissionDenied: response = { 'status': 'error', 'message': "You are not allowed to add a node" } except ValidationError as ex: response = {'status': 'error', 'message': ' - '.join(ex.messages)} except Exception as msg: logger.exception("process_nav_edition") response = { 'status': 'error', 'message': "An error occured : {0}".format(msg) } # return the result as json object return HttpResponse(json.dumps(response), content_type='application/json') raise Http404