Exemplo n.º 1
0
    def edit_blog_article(self, client, slug, params = None):
        """Shows edit-view when there are no parameters, otherwise try to save the related article

        :param Client client: The requesting client
        :param str slug: The article slug reference
        :param dict params: Optional; contains the form parameters
        :return: dict - Status and html-layout data response
        """
        if params == None:            
            article = BlogArticle.objects.get(slug = slug)
            form = EditBlogArticleForm(initial={'title':article.title,'text':article.text})
            main = render_to_string("blog/edit_article.html", {"article":article,'profile': client.profile,'form':form})
            return {'data':{'dom':{'main':main}}}
        else:
            form = EditBlogArticleForm(params)
            if form.is_valid():
                article = BlogArticle.objects.get(slug = slug)
                article.title = form.cleaned_data['title']
                article.text = form.cleaned_data['text']
                article.author = client.profile
                article.save()
                client_response, tpl_params = self._get_blog(client)
                client_response.update({
                    'status': {
                        'code':'BLOG_ARTICLE_SAVED',
                        'i18n':'%s: %s' % (client.profile.username,_('Article updated succesfully...')),
                        'type': HWIOS.ws_realm._t['notify-info'],
                    }
                })
                #notify others viewing the main blog
                notify_others(client, client_response,'/blog/modified/', '/blog/', tpl_params)
                #notify othere viewing this article
                _response, _tpl_params = self._get_blog_article(client, slug)
                _response.update(client_response)
                notify_others(client, _response,'/blog/%s/modified/' % slug, '/blog/%s/' % slug, _tpl_params)
                client_response['status']['state'] = client.set_uri('/blog/')
                publish_activity(client.profile, _('Blog article edited'),'/blog/%s/' % slug,[0,0,4,0,0])
                return client_response
            else:                
                article = BlogArticle.objects.get(slug = slug)
                main = render_to_string("blog/edit_article.html", {"article":article,'profile': client.profile,'form':form})
                client_response =  {
                    'status':{
                        'code':'FORM_INVALID',
                        'i18n':_('Invalid form!'),
                        'type': HWIOS.ws_realm._t['notify-info']
                    },
                    'data':{'dom':{'main':main}}
                }
                return client_response
Exemplo n.º 2
0
    def create_blog_article(self, client, params):
        """Handles creation of a new blog article, and notification to other watching clients

        :param Client client: The requesting client
        :param dict params: Contains the form parameters
        :return: dict - Status and html-layout data response
        """
        form = EditBlogArticleForm(params)
        if form.is_valid():
            article = BlogArticle()
            article.title = form.cleaned_data['title']
            article.text = form.cleaned_data['text']
            article.author = client.profile
            article.save()
            client_response, tpl_params = self._get_blog(client)
            client_response.update({
                'status':{
                    'code':'BLOG_ARTICLE_CREATED',
                    'i18n': '%s: %s' % (client.profile.username, _('Article created succesfully...')),
                    'type': HWIOS.ws_realm._t['notify-info'],
                    'state': '/blog/'
                }
            })
            notify_others(client, client_response,'/blog/modified/', '/blog/', tpl_params)
            publish_activity(client.profile, _('Blog article created'),'/blog/%s/' % article.slug,[0,1,4,0,0])
            return client_response
        else:
            articles = BlogArticle.objects.all()
            main = render_to_string("blog/read_blog.html", {"articles":articles,'profile': client.profile,'form':form})
            response =  {
                'status':{
                    'code':'FORM_INVALID',
                    'i18n':_('Invalid form!'),
                    'type': HWIOS.ws_realm._t['notify-info']
                },
                'data':{
                    'dom':{'main':main}
                }
            }
            return response