Exemplo n.º 1
0
def create(request, template_name=None, next=None, action=None):
    """this method is used to create a page about something"""
    request_user = request.user
    if request.method == 'POST':
        f = ArticleForm(request.POST)
        if f.is_valid():
            title = f.cleaned_data['title']
            slug = slugify(title)
            category = f.cleaned_data['category']
            protection = f.cleaned_data['protection']
            visibility = f.cleaned_data['visibility']
            can_write = f.cleaned_data['can_write']
            can_read = f.cleaned_data['can_read']
            if Article.objects.filter(slug=slug).count() > 0:
                slug = slug + str(Article.objects.filter(slug__contains=slug).count())
            article = Article(author=request.user, slug=slug, title=title,category=category)

            #Check every thing related to permissions cal_protection and cal_visibility
            if article.permissions is None:
                    permissions = Permission(permission_name='%s_perms' % slug,
                                             visibility=visibility,
                                             protection=protection)
                    permissions.save()
                    article.permissions = permissions
                    article.permissions.can_write.add(request.user)
                    article.permissions.can_read.add(request.user)
            #@todo change "2" to CUSTOM
            if protection == "2":
                for user in can_write:
                        article.permissions.can_write.add(user)
            if visibility == "2":
                for user in can_read:
                        article.permissions.can_read.add(user)


            article.parent = Article.get_root()
            article.save()
            
            if TaggableManager:
                #tagging the article
                tags = f.cleaned_data['tags']
                if tags:
                    article.tags.set(*tags)
            #adding the article description only if the user has checked the add_description
            if not f.cleaned_data['contents'] == '':
                status = f.cleaned_data['status']
                contents = f.cleaned_data['contents']
                new_revision = Version(user=request.user, article=article, status=status,
                                      contents=contents)
                new_revision.save()
            #set the notification object
            #ToNotify(user=request_user, Article=article).save()
            if not request.is_ajax():
                if next == 'wall_home':
                    return HttpResponseRedirect(reverse(next))
                return HttpResponseRedirect(reverse(next, args=(article.get_url(),)))
            response = {'success':True}
        else:
            response = errors_as_json(f)
        if request.is_ajax():
            json = simplejson.dumps(response, ensure_ascii=False)
            return HttpResponse(json, mimetype="application/json")
    else:
        f = ArticleForm()

    c = RequestContext(request, {'wiki_form': f,
                                 'action':reverse(action),
                                 'wiki_edit_protection':False,
                                 'wiki_write': False,
                                 'wiki_read': False,
                                 })

    return render_to_response(template_name, c)
Exemplo n.º 2
0
def edit(request, wiki_url, template_name=None, next=None):
    """this view is used to edit a page"""
    (article, path, err) = fetch_from_url(request, wiki_url)
    if err:
        return err
    request_user = request.user
    # Check write permissions
    perm_err = check_permissions(request, article, check_write=True, check_locked=True)
    if perm_err:
        return perm_err

    if request.method == 'POST':
        f = ArticleForm(request.POST)
        if f.is_valid():
            article.cal_category = f.cleaned_data['category']
            article.permissions.protection = f.cleaned_data['protection']
            article.permissions.visibility = f.cleaned_data['visibility']
            can_write = f.cleaned_data['can_write']
            can_read = f.cleaned_data['can_read']
            title = f.cleaned_data['title']
            article.title = title
            slug = slugify(title)
            if Article.objects.filter(slug=slug).count() > 0:
                slug = slug + str(Article.objects.filter(slug__contains=slug).count())
            article.slug = slug
            #Check every thing related to permissions cal_protection and cal_visibility
            if f.cleaned_data['protection'] == "2":
                article.permissions.edit_can_write_obj(can_write)
                article.permissions.can_write.add(article.author)
            if f.cleaned_data['visibility'] == "2":
                article.permissions.edit_can_read_obj(can_read)
                article.permissions.can_read.add(article.author)

            article.permissions.save()
            #adding the place only if the user has checked the add_place
            #if f.cleaned['add_place']:

            article.save()
            #adding the article description only if the user has checked the add_description
            if not f.cleaned_data['contents'] == '':
                status = f.cleaned_data['status']
                contents = f.cleaned_data['contents']
                new_revision = Version(user=request.user, article=article, status=status,
                                      contents=contents)                                            
                # Check that something has actually been changed...
                if new_revision.get_diff():
                    new_revision.save()
            if TaggableManager:
                tags = f.cleaned_data['tags']
                if tags:
                    article.tags.set(*tags)
                else:
                    article.tags.clear()
            #notify all concerned users by the object by the new comment
            #users_tonotify = ToNotify.objects.filter(article=article).exclude(user=request_user)
            #for user_tonotify in users_tonotify:
                #user = user_tonotify.user
                #notification.send([user], "article_updated", {'article': article, 'user':request_user,})
            if not request.is_ajax():
                return HttpResponseRedirect(reverse(next, args=(article.get_url(),)))
            response = ({'success':'True'})
        else:
            response = errors_as_json(f)
        if request.is_ajax():
            json = simplejson.dumps(response, ensure_ascii=False)
            return HttpResponse(json, mimetype="application/json")
    else:
        can_read_name = ''
        can_read_id = ''
        for user in article.permissions.can_read.all():
            if not user == request_user:
                can_read_name = can_read_name + user.first_name+' '+user.last_name + ','
                can_read_id = can_read_id + str(user.id) + ','

        can_write_name = ''
        can_write_id = ''
        for user in article.permissions.can_write.all():
            if not user == request_user:
                can_write_name = can_write_name + user.first_name+' '+user.last_name + ','
                can_write_id = can_write_id + str(user.id) + ','
        
        if TaggableManager:
            tags = ''
            for tag in article.tags.all():
                tags = tags + tag.name + ','
            f = ArticleForm({
                  'title': article.title,
                  'contents': article.current_version.contents,
                  'category': article.category,
                  'protection':article.permissions.protection,
                  'visibility': article.permissions.visibility,
                  'can_read': can_read_id,
                  'can_write': can_write_id,
                  'tags' : tags
                  })
        else:
            f = ArticleForm({
                  'title': article.title,
                  'contents': article.current_version.contents,
                  'category': article.category,
                  'protection':article.permissions.protection,
                  'visibility': article.permissions.visibility,
                  'can_read': can_read_id,
                  'can_write': can_write_id,                  
                  })


    c = RequestContext(request, {'wiki_form': f,
                                 'wiki_write': True,
                                 'wiki_read': True,
                                 'wiki_article': article,
                                 'lock': True,
                                 'lock_url': reverse('lock_version', args=[article.id]),
                                 'unlock_url': reverse('unlock_version', args=[article.id]),
                                 'can_read_name': can_read_name,
                                 'can_write_name': can_write_name,
                                 'wiki_attachments_write': article.can_attach(request.user),
                                 'contents': article.current_version.contents,
                                 'title': article.title,
                                 })

    return render_to_response(template_name, c)