示例#1
0
    def form_valid(self, form):
        """
		If the form is valid, save the associated model.
		"""
        self.object = form.save(commit=False)
        if settings.REALTIME_EDITOR:
            if get_pad_usercount(
                    self.object.pk
            ) <= 1 or self.object.state == self.model.objects.get(
                    pk=self.object.pk).state:
                self.object.body = getHTML(self.object)
            else:
                messages.warning(
                    self.request,
                    'The article state cannot be change at this moment because currently there are more than one user editing this article. You can save your changes in present state.'
                )
                return super(ArticleEditView, self).form_invalid(form)
        self.object.save()
        form.save_m2m()
        if self.is_visible():
            self.process_visible()
        if self.is_publishable():
            self.process_publishable()
        if self.object.state.final:
            self.process_final()
        return super(ArticleEditView, self).form_valid(form)
示例#2
0
def article_autosave(request, pk):
    if request.user.is_authenticated:
        if request.method == 'POST':
            article = Articles.objects.get(pk=pk)
            article.body = getHTML(article)
            data = {'success': "Done", 'html': article.body}
            article.save()
            return JsonResponse(data)

    else:
        return redirect('login')
示例#3
0
def edit_article(request, pk):
    """
	A function to edit an article. The function check whether the method is post,
	if not it will check if the article belong to group or community and
	whether the user is a member of that group or cummunity. If the user is not a member,
	he will not be allowed to edit this article. If the user is a member of the group and not the community
	than he will not be allowed to edit this article
	"""
    if request.user.is_authenticated:
        if request.method == 'POST':
            if 'state' in request.POST and request.POST['state'] == 'save':
                article = Articles.objects.get(pk=pk)
                article.title = request.POST['title']
                article.body = getHTML(article)
                try:
                    article.image = request.FILES['article_image']
                    article.save(update_fields=["title", "body", "image"])
                except:
                    article.save(update_fields=["title", "body"])
                current_state = request.POST['current']
                notify_edit_article(request.user, article, current_state)
                return redirect('article_view', pk=article.pk)
            else:
                article = Articles.objects.get(pk=pk)
                title = request.POST['title']
                body = getHTML(article)
                current_state = request.POST['current']
                try:
                    current_state = States.objects.get(name=current_state)
                    if 'private' in request.POST:
                        to_state = States.objects.get(name='private')
                        article.state = to_state
                        #Article got rejected
                        notify_update_article_state(request.user, article,
                                                    'rejected')
                        create_resource_feed(article, "article_edit",
                                             request.user)

                    else:
                        to_state = request.POST['state']
                        to_state = States.objects.get(name=to_state)

                        if current_state.name == 'draft' and to_state.name == 'visible' and 'belongs_to' in request.POST:
                            article.state = to_state
                            create_resource_feed(article, 'article_edit',
                                                 request.user)

                        elif current_state.name == 'visible' and to_state.name == 'publish' and 'belongs_to' in request.POST:
                            article.state = to_state

                        else:
                            transitions = Transitions.objects.get(
                                from_state=current_state, to_state=to_state)
                            article.state = to_state

                            if (to_state.name == 'publishable'):
                                notify_update_article_state(
                                    request.user, article, 'publishable')
                                create_resource_feed(article,
                                                     "article_no_edit",
                                                     request.user)

                            # sending group feed and personal notificaions when an article goes from draft to private state.
                            if (to_state.name == 'private'):
                                create_resource_feed(article, "article_edit",
                                                     request.user)
                                notify_update_article_state(
                                    request.user, article, 'private')

                            # sending group feed and personal notification to all publishers and admins of group.
                            if (current_state.name == 'private'
                                    and to_state.name == 'visible'):
                                create_resource_feed(article,
                                                     "article_no_edit",
                                                     request.user)
                                notify_update_article_state(
                                    request.user, article, 'visible')

                    article.title = title
                    article.body = body
                    try:
                        article.image = request.FILES['article_image']
                        article.save(
                            update_fields=["title", "body", "image", "state"])
                    except:
                        article.save(update_fields=["title", "body", "state"])
                except Transitions.DoesNotExist:
                    message = "transition doesn' exist"
                except States.DoesNotExist:
                    message = "state doesn' exist"
                if to_state.name == 'publish':
                    article.published_on = datetime.datetime.now()
                    article.published_by = request.user
                    article.save()
                    create_resource_feed(article, 'article_published',
                                         article.created_by)
                    notify_update_article_state(request.user, article,
                                                'published')
                return redirect('article_view', pk=pk)
        else:
            message = ""
            transition = ""
            cmember = ""
            gmember = ""
            private = ""
            try:
                # print ("Hello")
                article = CommunityArticles.objects.get(article=pk)
                # print ("Hello2")

                if article.article.state == States.objects.get(
                        name='draft'
                ) and article.article.created_by != request.user:
                    return redirect('home')
                if article.article.state == States.objects.get(name='publish'):
                    return redirect('article_view', pk=pk)
                belongs_to = 'community'
                # print ("Hello3")

                try:
                    cmember = CommunityMembership.objects.get(
                        user=request.user.id, community=article.community.pk)
                    sessionid = create_session_community(
                        request, article.community.id)
                    try:
                        transition = Transitions.objects.get(
                            from_state=article.article.state)
                        state1 = States.objects.get(name='draft')
                        state2 = States.objects.get(name='private')
                        if transition.from_state == state1 and transition.to_state == state2:
                            transition.to_state = States.objects.get(
                                name='visible')

                    except Transitions.DoesNotExist:
                        message = "transition doesn't exist"
                    except States.DoesNotExist:
                        message = "state doesn't exist"
                except CommunityMembership.DoesNotExist:
                    cmember = 'FALSE'
            except CommunityArticles.DoesNotExist:
                try:
                    # print ("Hello4")

                    article = GroupArticles.objects.get(article=pk)
                    if article.article.state == States.objects.get(
                            name='publish'):
                        return redirect('article_view', pk=pk)
                    if article.article.state == States.objects.get(
                            name='draft'
                    ) and article.article.created_by != request.user:
                        return redirect('home')
                    belongs_to = 'group'
                    private = ''
                    try:
                        # print ("Hello5")

                        communitygroup = CommunityGroups.objects.get(
                            group=article.group.pk)
                        cmember = CommunityMembership.objects.get(
                            user=request.user.id,
                            community=communitygroup.community.pk)
                        try:
                            gmember = GroupMembership.objects.get(
                                user=request.user.id, group=article.group.pk)
                            sessionid = create_session_group(
                                request, article.group.id)
                        except GroupMembership.DoesNotExist:
                            gmember = 'FALSE'
                        try:
                            transition = Transitions.objects.get(
                                from_state=article.article.state)
                            state1 = States.objects.get(name='visible')
                            state2 = States.objects.get(name='publishable')
                            if transition.from_state == state1 and transition.to_state == state2:
                                transition.to_state = States.objects.get(
                                    name='publish')
                                private = States.objects.get(name='private')

                        except Transitions.DoesNotExist:
                            message = "transition doesn't exist"
                    except CommunityMembership.DoesNotExist:
                        message = 'You are not a member of <h3>%s</h3> community. Please subscribe to the community.' % (
                            communitygroup.community.name)
                except GroupArticles.DoesNotExist:
                    # print ("Hello6")

                    raise Http404
            padid = get_pad_id(article.article.id)
            response = render(
                request, 'edit_article.html', {
                    'article': article,
                    'cmember': cmember,
                    'gmember': gmember,
                    'message': message,
                    'belongs_to': belongs_to,
                    'transition': transition,
                    'private': private,
                    'url': settings.SERVERURL,
                    'padid': padid
                })
            response.set_cookie('sessionID', sessionid)
            return response
    else:
        return redirect('login')