Ejemplo n.º 1
0
def new_post(request):
    if request.method == 'POST':
        validate_blog = BlogpostForm(request.POST)
        if validate_blog.is_valid():
            blog_post = validate_blog.save(commit=False)
            blog_post.user = request.user
            blog_post.meta_description = request.POST['meta_description']
            blog_post.status = 'D'
            if request.POST.get('status') == "P":
                if request.user.user_roles == "Admin" or request.user.is_special or request.user.is_superuser:
                    blog_post.status = 'P'

            elif request.POST.get('status') == "T":
                if request.user.user_roles == "Admin" or request.user.is_special or request.user.is_superuser:
                    blog_post.status = 'T'
            blog_post.save()
            if request.POST.get('tags', ''):
                tags = request.POST.get('tags')
                tags = tags.split(',')
                for tag in tags:
                    blog_tag = Tags.objects.filter(name=tag)
                    if blog_tag:
                        blog_tag = blog_tag[0]
                    else:
                        blog_tag = Tags.objects.create(name=tag)
                    blog_post.tags.add(blog_tag)

            sg = sendgrid.SendGridClient(settings.SG_USER, settings.SG_PWD)
            sending_msg = sendgrid.Mail()
            sending_msg.set_subject("New blog post has been created")

            blog_url = 'https://www.micropyramid.com/blog/view-post/' + str(
                blog_post.slug) + '/'
            message = '<p>New blog post has been created by ' + str(
                request.user) + ' with the name ' + str(
                    blog_post.title) + ' in the category '
            message += str(
                blog_post.category.name
            ) + '.</p>' + '<p>Please <a href="' + blog_url + '">click here</a> to view the blog post in the site.</p>'

            sending_msg.set_html(message)
            sending_msg.set_text('New blog post has been created')
            sending_msg.set_from(request.user.email)
            sending_msg.add_to(
                [user.email for user in User.objects.filter(is_admin=True)])
            sg.send(sending_msg)

            data = {'error': False, 'response': 'Blog Post created'}
        else:
            data = {'error': True, 'response': validate_blog.errors}
        return HttpResponse(json.dumps(data),
                            content_type='application/json; charset=utf-8')

    categories = Category.objects.all()
    c = {}
    c.update(csrf(request))
    return render(request, 'admin/blog/blog-new.html', {
        'categories': categories,
        'csrf_token': c['csrf_token']
    })
Ejemplo n.º 2
0
def new_post(request):
    if request.method == 'POST':
        validate_blog = BlogpostForm(request.POST)
        if validate_blog.is_valid():
            blog_post = validate_blog.save(commit=False)
            blog_post.user = request.user
            blog_post.status = 'D'
            if request.POST.get('status') == "P":
                if request.user.user_roles == "Admin" or request.user.is_special or request.user.is_superuser:
                    blog_post.status = 'P'

            elif request.POST.get('status') == "T":
                if request.user.user_roles == "Admin" or request.user.is_special or request.user.is_superuser:
                    blog_post.status = 'T'
            blog_post.save()
            if request.POST.get('tags', ''):
                tags = request.POST.get('tags')
                tags = tags.split(',')
                for tag in tags:
                    blog_tag = Tags.objects.filter(name=tag)
                    if blog_tag:
                        blog_tag = blog_tag[0]
                    else:
                        blog_tag = Tags.objects.create(name=tag)
                    blog_post.tags.add(blog_tag)
            data = {'error': False, 'response': 'Blog Post created'}
        else:
            data = {'error': True, 'response': validate_blog.errors}
        return HttpResponse(json.dumps(data))
    categories = Category.objects.all()
    c = {}
    c.update(csrf(request))
    return render(request, 'admin/blog/blog-new.html', {'categories': categories, 'csrf_token': c['csrf_token']})
Ejemplo n.º 3
0
def new_post(request):
    if request.method == 'POST':
        validate_blog = BlogpostForm(request.POST)
        if validate_blog.is_valid():
            blog_post = validate_blog.save(commit=False)
            blog_post.user = request.user
            blog_post.meta_description = request.POST['meta_description']
            blog_post.status = 'D'
            if request.POST.get('status') == "P":
                if request.user.user_roles == "Admin" or request.user.is_special or request.user.is_superuser:
                    blog_post.status = 'P'
                    blog_post.published_on = datetime.datetime.now().date()

            elif request.POST.get('status') == "T":
                if request.user.user_roles == "Admin" or request.user.is_special or request.user.is_superuser:
                    blog_post.status = 'T'
            blog_post.save()
            if request.user.is_superuser and request.POST.get('slug'):
                blog_post.slug = request.POST.get('slug')
            else:
                tempslug = slugify(blog_post.title)

            blog_post.save()

            if request.POST.get('tags', ''):
                tags = request.POST.get('tags')
                tags = tags.split(',')
                for tag in tags:
                    blog_tag = Tags.objects.filter(name=tag)
                    if blog_tag:
                        blog_tag = blog_tag[0]
                    else:
                        blog_tag = Tags.objects.create(name=tag)
                    blog_post.tags.add(blog_tag)

            sg = sendgrid.SendGridClient(settings.SG_USER, settings.SG_PWD)
            sending_msg = sendgrid.Mail()
            sending_msg.set_subject("New blog post has been created")

            blog_url = 'https://www.micropyramid.com/blog/' + str(blog_post.slug) + '/'
            message = '<p>New blog post has been created by ' + str(request.user) + ' with the name ' + str(blog_post.title) + ' in the category '
            message += str(blog_post.category.name) + '.</p>' + '<p>Please <a href="' + blog_url + '">click here</a> to view the blog post in the site.</p>'

            sending_msg.set_html(message)
            sending_msg.set_text('New blog post has been created')
            sending_msg.set_from(request.user.email)
            sending_msg.add_to([user.email for user in User.objects.filter(is_admin=True)])
            sg.send(sending_msg)

            cache._cache.flush_all()
            data = {'error': False, 'response': 'Blog Post created'}
        else:
            data = {'error': True, 'response': validate_blog.errors}
        return HttpResponse(json.dumps(data), content_type='application/json; charset=utf-8')

    categories = Category.objects.all()
    c = {}
    c.update(csrf(request))
    return render(request, 'admin/blog/blog-new.html', {'categories': categories, 'csrf_token': c['csrf_token']})
Ejemplo n.º 4
0
 def test_blogpostform(self):
     form = BlogpostForm(
         data={
             'title': 'python introduction', 'content': 'This is content',
             'category': self.category.id, 'status': 'D',
             'meta_description': 'meta', 'is_superuser': '******'
             })
     self.assertTrue(form.is_valid())
Ejemplo n.º 5
0
 def test_blogpostform(self):
     form = BlogpostForm(
         data={
             'title': 'python introduction', 'content': 'This is content',
             'category': self.category.id, 'status': 'D',
             'meta_description': 'meta', 'is_superuser': '******',
             'excerpt': "Description"
             })
     self.assertTrue(form.is_valid())
Ejemplo n.º 6
0
 def test_blogpostform(self):
     form = BlogpostForm(
         data={
             'title': 'python introduction',
             'content': 'This is content',
             'category': self.category.id,
             'status': 'D'
         })
     self.assertTrue(form.is_valid())
Ejemplo n.º 7
0
def edit_blog_post(request, blog_slug):
    if request.method == 'POST':
        current_post = get_object_or_404(Post, slug=blog_slug)
        validate_blog = BlogpostForm(request.POST, instance=current_post)
        if validate_blog.is_valid():
            blog_post = validate_blog.save(commit=False)
            blog_post.meta_description = request.POST['meta_description']
            blog_post.status = 'D'
            if request.POST.get('status') == "P":
                if request.user.user_roles == "Admin" or request.user.is_special or request.user.is_superuser:
                    blog_post.status = 'P'
            elif request.POST.get('status') == "T":
                if request.user.user_roles == "Admin" or request.user.is_special or request.user.is_superuser:
                    blog_post.status = 'T'
            if request.user.is_superuser and request.POST.get('slug'):
                blog_post.slug = request.POST.get('slug')
            else:
                tempslug = slugify(blog_post.title)

            blog_post.save()
            blog_post.tags.clear()
            if request.POST.get('tags', ''):
                for tag in blog_post.tags.all():
                    blog_post.tags.remove(tag)
                tags = request.POST.get('tags')
                tags = tags.split(',')
                for tag in tags:
                    blog_tag = Tags.objects.filter(name=tag)
                    if blog_tag:
                        blog_tag = blog_tag[0]
                    else:
                        blog_tag = Tags.objects.create(name=tag)

                    blog_post.tags.add(blog_tag)

            cache._cache.flush_all()
            data = {'error': False, 'response': 'Blog Post edited'}
        else:
            data = {'error': True, 'response': validate_blog.errors}
        return HttpResponse(json.dumps(data), content_type='application/json; charset=utf-8')

    blog_post = get_object_or_404(Post, slug=blog_slug)
    categories = Category.objects.all()
    if request.user.is_superuser or blog_post.user == request.user:
        c = {}
        c.update(csrf(request))
        return render(request, 'admin/blog/blog-edit.html', {'blog_post': blog_post, 'categories': categories, 'csrf_token': c['csrf_token']})
    else:
        return render_to_response('admin/accessdenied.html')
Ejemplo n.º 8
0
def edit_blog_post(request, blog_slug):
    if request.method == 'POST':
        current_post = get_object_or_404(Post, slug=blog_slug)
        validate_blog = BlogpostForm(request.POST, instance=current_post)
        if validate_blog.is_valid():
            blog_post = validate_blog.save(commit=False)
            blog_post.meta_description = request.POST['meta_description']
            blog_post.status = 'D'
            if request.POST.get('status') == "P":
                if request.user.user_roles == "Admin" or request.user.is_special or request.user.is_superuser:
                    blog_post.status = 'P'
            elif request.POST.get('status') == "T":
                if request.user.user_roles == "Admin" or request.user.is_special or request.user.is_superuser:
                    blog_post.status = 'T'
            blog_post.save()
            blog_post.tags.clear()
            if request.POST.get('tags', ''):
                for tag in blog_post.tags.all():
                    blog_post.tags.remove(tag)
                tags = request.POST.get('tags')
                tags = tags.split(',')
                for tag in tags:
                    blog_tag = Tags.objects.filter(name=tag)
                    if blog_tag:
                        blog_tag = blog_tag[0]
                    else:
                        blog_tag = Tags.objects.create(name=tag)

                    blog_post.tags.add(blog_tag)
            data = {'error': False, 'response': 'Blog Post edited'}
        else:
            data = {'error': True, 'response': validate_blog.errors}
        return HttpResponse(json.dumps(data),
                            content_type='application/json; charset=utf-8')

    blog_post = get_object_or_404(Post, slug=blog_slug)
    categories = Category.objects.all()
    if request.user.is_superuser or blog_post.user == request.user:
        c = {}
        c.update(csrf(request))
        return render(
            request, 'admin/blog/blog-edit.html', {
                'blog_post': blog_post,
                'categories': categories,
                'csrf_token': c['csrf_token']
            })
    else:
        return render_to_response('admin/accessdenied.html')
Ejemplo n.º 9
0
def edit_blog_post(request,blog_slug):
    if request.method == 'POST':
        current_post = Post.objects.get(slug = blog_slug)
        print len(request.POST.get('content'))
        validate_blog = BlogpostForm(request.POST,instance=current_post)
        if validate_blog.is_valid():
            blog_post = validate_blog.save(commit=False)
            blog_post.user=request.user
            if request.POST.get('status'):
                blog_post.status='D'
            else:
                blog_post.status='P'

            if request.POST.get('featured_post',''):
                blog_post.featured_post==request.POST.get('featured_post')

            if 'featuredimage' in request.FILES:
                if current_post.featured_image:
                    os.remove(BLOG_IMAGES + current_post.featured_image)
                blog_post.featured_image=store_image(request.FILES.get('featuredimage'),BLOG_IMAGES)

            blog_post.save()

            if request.POST.get('tags',''):
                for tag in blog_post.tags.all():
                    blog_post.tags.remove(tag)
                tags = request.POST.get('tags')
                tags = tags.split(',')
                for tag in tags:
                    blog_tag = Tags.objects.filter(name=tag)
                    if blog_tag:
                        blog_tag = blog_tag[0]
                    else:
                        blog_tag = Tags.objects.create(name=tag)

                    blog_post.tags.add(blog_tag)
            data = {'error':False,'response':'Blog Post created'}
        else:
            data = {'error':True,'response':validate_blog.errors}
        return HttpResponse(json.dumps(data))

    blog_post = Post.objects.get(slug=blog_slug)
    categories = Category.objects.all()
    c = {}
    c.update(csrf(request))
    return render_to_response('admin/blog/blog-edit.html',{'blog_post':blog_post,'categories':categories,'csrf_token':c['csrf_token']})
Ejemplo n.º 10
0
def edit_blog_post(request,blog_slug):
    if request.method == 'POST':
        current_post = Post.objects.get(slug = blog_slug)
        print len(request.POST.get('content'))
        validate_blog = BlogpostForm(request.POST,instance=current_post)
        if validate_blog.is_valid():
            blog_post = validate_blog.save(commit=False)
            blog_post.user=request.user
            if request.POST.get('status'):
                blog_post.status='D'
            else:
                blog_post.status='P'

            if request.POST.get('featured_post',''):
                blog_post.featured_post==request.POST.get('featured_post')

            if 'featuredimage' in request.FILES:
                if current_post.featured_image:
                    os.remove(BLOG_IMAGES + current_post.featured_image)
                blog_post.featured_image=store_image(request.FILES.get('featuredimage'),BLOG_IMAGES)

            blog_post.save()

            if request.POST.get('tags',''):
                for tag in blog_post.tags.all():
                    blog_post.tags.remove(tag)
                tags = request.POST.get('tags')
                tags = tags.split(',')
                for tag in tags:
                    blog_tag = Tags.objects.filter(name=tag)
                    if blog_tag:
                        blog_tag = blog_tag[0]
                    else:
                        blog_tag = Tags.objects.create(name=tag)

                    blog_post.tags.add(blog_tag)
            data = {'error':False,'response':'Blog Post created'}
        else:
            data = {'error':True,'response':validate_blog.errors}
        return HttpResponse(json.dumps(data))

    blog_post = Post.objects.get(slug=blog_slug)
    categories = Category.objects.all()
    c = {}
    c.update(csrf(request))
    return render_to_response('admin/blog/blog-edit.html',{'blog_post':blog_post,'categories':categories,'csrf_token':c['csrf_token']})
Ejemplo n.º 11
0
def edit_blog_post(request, blog_slug):
    if request.method == "POST":
        current_post = get_object_or_404(Post, slug=blog_slug)
        validate_blog = BlogpostForm(request.POST, instance=current_post)
        if validate_blog.is_valid():
            blog_post = validate_blog.save(commit=False)
            blog_post.status = "D"
            if request.POST.get("status") == "P":
                if request.user.user_roles == "Admin" or request.user.is_special or request.user.is_superuser:
                    blog_post.status = "P"
            elif request.POST.get("status") == "T":
                if request.user.user_roles == "Admin" or request.user.is_special or request.user.is_superuser:
                    blog_post.status = "T"
            blog_post.save()
            blog_post.tags.clear()
            if request.POST.get("tags", ""):
                for tag in blog_post.tags.all():
                    blog_post.tags.remove(tag)
                tags = request.POST.get("tags")
                tags = tags.split(",")
                for tag in tags:
                    blog_tag = Tags.objects.filter(name=tag)
                    if blog_tag:
                        blog_tag = blog_tag[0]
                    else:
                        blog_tag = Tags.objects.create(name=tag)

                    blog_post.tags.add(blog_tag)
            data = {"error": False, "response": "Blog Post edited"}
        else:
            data = {"error": True, "response": validate_blog.errors}
        return HttpResponse(json.dumps(data), content_type="application/json; charset=utf-8")

    blog_post = get_object_or_404(Post, slug=blog_slug)
    categories = Category.objects.all()
    if request.user.is_superuser or blog_post.user == request.user:
        c = {}
        c.update(csrf(request))
        return render(
            request,
            "admin/blog/blog-edit.html",
            {"blog_post": blog_post, "categories": categories, "csrf_token": c["csrf_token"]},
        )
    else:
        return render_to_response("admin/accessdenied.html")
Ejemplo n.º 12
0
def edit_blog_post(request, blog_slug):
    if request.method == 'POST':
        current_post = Post.objects.get(slug=blog_slug)
        validate_blog = BlogpostForm(request.POST, instance=current_post)
        if validate_blog.is_valid():
            blog_post = validate_blog.save(commit=False)
            blog_post.status = 'D'
            if request.POST.get('status') == "P":
                if request.user.user_roles == "Admin" or request.user.is_special or request.user.is_superuser:
                    blog_post.status = 'P'
            elif request.POST.get('status') == "T":
                if request.user.user_roles == "Admin" or request.user.is_special or request.user.is_superuser:
                    blog_post.status = 'T'
            blog_post.save()

            if request.POST.get('tags', ''):
                for tag in blog_post.tags.all():
                    blog_post.tags.remove(tag)
                tags = request.POST.get('tags')
                tags = tags.split(',')
                for tag in tags:
                    blog_tag = Tags.objects.filter(name=tag)
                    if blog_tag:
                        blog_tag = blog_tag[0]
                    else:
                        blog_tag = Tags.objects.create(name=tag)

                    blog_post.tags.add(blog_tag)
            data = {'error': False, 'response': 'Blog Post edited'}
        else:
            data = {'error': True, 'response': validate_blog.errors}
        return HttpResponse(json.dumps(data))
    blog_post = Post.objects.get(slug=blog_slug)
    categories = Category.objects.all()
    if request.user.is_superuser or blog_post.user == request.user:
        c = {}
        c.update(csrf(request))
        return render(request, 'admin/blog/blog-edit.html', {'blog_post': blog_post, 'categories': categories, 'csrf_token': c['csrf_token']})
    else:
        return render_to_response('admin/accessdenied.html')
Ejemplo n.º 13
0
def admin_new_post(request):
    if request.method == 'POST':
        validate_blog = BlogpostForm(request.POST)
        if validate_blog.is_valid():
            blog_post = validate_blog.save(commit=False)
            blog_post.user=request.user

            if request.POST.get('status',''):
                blog_post.status='D'
            else:
                blog_post.status='P'

            if request.POST.get('featured_post',''):
                blog_post.featured_post=request.POST.get('featured_post')
                if 'featuredimage' in request.FILES:
                    blog_post.featured_image=store_image(request.FILES.get('featuredimage'),BLOG_IMAGES)
                else:
                    blog_post.featured_image=''

            blog_post.save()
            if request.POST.get('tags',''):
                tags = request.POST.get('tags')
                tags = tags.split(',')
                for tag in tags:
                    blog_tag = Tags.objects.filter(name=tag)
                    if blog_tag:
                        blog_tag = blog_tag[0]
                    else:
                        blog_tag = Tags.objects.create(name=tag)

                    blog_post.tags.add(blog_tag)
            data = {'error':False,'response':'Blog Post created'}
        else:
            data = {'error':True,'response':validate_blog.errors}
        return HttpResponse(json.dumps(data))
    categories = Category.objects.all()
    c = {}
    c.update(csrf(request))
    return render_to_response('admin/blog/blog-new.html',{'categories':categories,'csrf_token':c['csrf_token']})
Ejemplo n.º 14
0
def admin_new_post(request):
    if request.method == 'POST':
        validate_blog = BlogpostForm(request.POST)
        if validate_blog.is_valid():
            blog_post = validate_blog.save(commit=False)
            blog_post.user=request.user

            if request.POST.get('status',''):
                blog_post.status='D'
            else:
                blog_post.status='P'

            if request.POST.get('featured_post',''):
                blog_post.featured_post=request.POST.get('featured_post')
                if 'featuredimage' in request.FILES:
                    blog_post.featured_image=store_image(request.FILES.get('featuredimage'),BLOG_IMAGES)
                else:
                    blog_post.featured_image=''

            blog_post.save()
            if request.POST.get('tags',''):
                tags = request.POST.get('tags')
                tags = tags.split(',')
                for tag in tags:
                    blog_tag = Tags.objects.filter(name=tag)
                    if blog_tag:
                        blog_tag = blog_tag[0]
                    else:
                        blog_tag = Tags.objects.create(name=tag)

                    blog_post.tags.add(blog_tag)
            data = {'error':False,'response':'Blog Post created'}
        else:
            data = {'error':True,'response':validate_blog.errors}
        return HttpResponse(json.dumps(data))
    categories = Category.objects.all()
    c = {}
    c.update(csrf(request))
    return render_to_response('admin/blog/blog-new.html',{'categories':categories,'csrf_token':c['csrf_token']})
Ejemplo n.º 15
0
	def test_blogpostform(self):
		form = BlogpostForm(data={'title':'python introduction','content':'This is content','category':self.c.id,'status':'D'})
		self.assertTrue(form.is_valid())
Ejemplo n.º 16
0
def edit_blog_post(request, blog_slug):
    blog_post = get_object_or_404(Post, slugs__slug=blog_slug)
    active_slug = blog_post.slug
    old_blog_status = blog_post.status
    if active_slug != blog_slug:
        return redirect(reverse('micro_blog:edit_blog_post',
                                kwargs={'blog_slug': active_slug})
                        )
    if not blog_post.is_editable_by(request.user):
        return render_to_response('admin/accessdenied.html')

    BlogSlugFormSet = inlineformset_factory(Post, Post_Slugs,
                                            can_delete=True, extra=3, fields=('slug', 'is_active'),
                                            formset=CustomBlogSlugInlineFormSet
                                            )
    if request.method == 'POST':
        blog_form = BlogpostForm(request.POST, instance=blog_post)
        blogslugs_formset = BlogSlugFormSet(request.POST, instance=blog_post)
        if blog_form.is_valid() and blogslugs_formset.is_valid():
            blog_post = blog_form.save(commit=False)
            if blogslugs_formset.is_valid():
                blog_post.status = old_blog_status
                blog_post.meta_description = request.POST.get(
                    'meta_description')
                if request.POST.get('status') == "P":
                    if request.user.user_roles == "Admin" or request.user.is_special or request.user.is_superuser:
                        blog_post.status = 'P'
                elif request.POST.get('status') == "T":
                    if request.user.user_roles == "Admin" or request.user.is_special or request.user.is_superuser:
                        blog_post.status = 'T'
                elif (request.POST.get('status') == "R" or
                      request.POST.get('status') == "D"
                      ):
                    blog_post.status = request.POST.get('status')
                else:
                    pass

                blog_post.save()
                blogslugs_formset.save()
                # If no slugs are specified, then create one using title.
                if not blog_post.slugs.all():
                    blog_post.create_blog_slug([slugify(blog_post.title)])
                blog_post.check_and_activate_slug()

                blog_post.tags.clear()
                if request.POST.get('tags', ''):
                    for tag in blog_post.tags.all():
                        blog_post.tags.remove(tag)
                    tags = request.POST.get('tags')
                    tags = tags.split(',')
                    for tag in tags:
                        blog_tag = Tags.objects.filter(name=tag)
                        if blog_tag:
                            blog_tag = blog_tag[0]
                        else:
                            blog_tag = Tags.objects.create(name=tag)

                        blog_post.tags.add(blog_tag)
                cache._cache.flush_all()
                return redirect(reverse('micro_blog:admin_post_list'))
    else:
        blog_form = BlogpostForm(instance=blog_post)
        blogslugs_formset = BlogSlugFormSet(instance=blog_post)

    categories = Category.objects.all()
    c = {}
    c.update(csrf(request))
    return render(request, 'admin/blog/blog-edit.html', {'blog_post': blog_post,
                                                         'categories': categories, 'csrf_token': c['csrf_token'],
                                                         'blogslugs_formset': blogslugs_formset, 'blog_form': blog_form}
                  )
Ejemplo n.º 17
0
def new_post(request):
    BlogSlugFormSet = inlineformset_factory(Post, Post_Slugs,
                                            can_delete=True, extra=3, fields=('slug', 'is_active'),
                                            formset=CustomBlogSlugInlineFormSet
                                            )
    if request.method == 'POST':
        blog_form = BlogpostForm(request.POST)
        blogslugs_formset = BlogSlugFormSet(request.POST)
        if blog_form.is_valid() and blogslugs_formset.is_valid():
            blog_post = blog_form.save(commit=False)
            blogslugs_formset = BlogSlugFormSet(
                request.POST, instance=blog_post)
            if blogslugs_formset.is_valid():
                blog_post.user = request.user
                if request.POST.get('meta_description'):
                    blog_post.meta_description = request.POST[
                        'meta_description']
                blog_post.status = 'D'
                if request.POST.get('status') == "P":
                    if request.user.user_roles == "Admin" or request.user.is_special or request.user.is_superuser:
                        blog_post.status = 'P'
                        if not blog_post.published_on:
                            blog_post.published_on = datetime.datetime.now(
                            ).date()

                elif request.POST.get('status') == "T":
                    if request.user.user_roles == "Admin" or request.user.is_special or request.user.is_superuser:
                        blog_post.status = 'T'

                elif request.POST.get('status') == "R":
                    blog_post.status = 'R'

                blog_post.save()
                blogslugs_formset.save()
                # If no slugs are specified, then create one using title.
                if not blog_post.slugs.all():
                    blog_post.create_blog_slug([slugify(blog_post.title)])
                blog_post.check_and_activate_slug()

                if request.POST.get('tags', ''):
                    tags = request.POST.get('tags')
                    tags = tags.split(',')
                    for tag in tags:
                        blog_tag = Tags.objects.filter(name=tag)
                        if blog_tag:
                            blog_tag = blog_tag[0]
                        else:
                            blog_tag = Tags.objects.create(name=tag)
                        blog_post.tags.add(blog_tag)

                sg = sendgrid.SendGridClient(settings.SG_USER, settings.SG_PWD)
                sending_msg = sendgrid.Mail()
                sending_msg.set_subject("New blog post has been created")

                blog_url = 'https://www.micropyramid.com/blog/' + \
                    str(blog_post.slug) + '/'
                message = '<p>New blog post has been created by ' + str(request.user) + ' with the name ' + str(
                    blog_post.title) + ' in the category ' + str(blog_post.category.name) + '.</p>'

                sending_msg.set_html(message)
                sending_msg.set_text('New blog post has been created')
                sending_msg.set_from(request.user.email)
                sending_msg.add_to(
                    [user.email for user in User.objects.filter(is_admin=True)])
                sg.send(sending_msg)

                cache._cache.flush_all()
                return redirect(reverse('micro_blog:admin_post_list'))
    else:
        blog_form = BlogpostForm()
        blogslugs_formset = BlogSlugFormSet(instance=Post())

    categories = Category.objects.all()
    c = {}
    c.update(csrf(request))
    return render(request, 'admin/blog/blog-new.html',
                  {'categories': categories, 'csrf_token': c['csrf_token'],
                   'blogslugs_formset': blogslugs_formset, 'blog_form': blog_form}
                  )
Ejemplo n.º 18
0
def new_post(request):
    if request.method == "POST":
        validate_blog = BlogpostForm(request.POST)
        if validate_blog.is_valid():
            blog_post = validate_blog.save(commit=False)
            blog_post.user = request.user
            blog_post.status = "D"
            if request.POST.get("status") == "P":
                if request.user.user_roles == "Admin" or request.user.is_special or request.user.is_superuser:
                    blog_post.status = "P"

            elif request.POST.get("status") == "T":
                if request.user.user_roles == "Admin" or request.user.is_special or request.user.is_superuser:
                    blog_post.status = "T"
            blog_post.save()
            if request.POST.get("tags", ""):
                tags = request.POST.get("tags")
                tags = tags.split(",")
                for tag in tags:
                    blog_tag = Tags.objects.filter(name=tag)
                    if blog_tag:
                        blog_tag = blog_tag[0]
                    else:
                        blog_tag = Tags.objects.create(name=tag)
                    blog_post.tags.add(blog_tag)

            sg = sendgrid.SendGridClient(settings.SG_USER, settings.SG_PWD)
            sending_msg = sendgrid.Mail()
            sending_msg.set_subject("New blog post has been created")

            blog_url = "http://www.micropyramid.com/blog/view-post/" + str(blog_post.slug) + "/"
            message = (
                "<p>New blog post has been created by "
                + str(request.user)
                + " with the name "
                + str(blog_post.title)
                + " in the category "
            )
            message += (
                str(blog_post.category.name)
                + ".</p>"
                + '<p>Please <a href="'
                + blog_url
                + '">click here</a> to view the blog post in the site.</p>'
            )

            sending_msg.set_html(message)
            sending_msg.set_text("New blog post has been created")
            sending_msg.set_from(request.user.email)
            sending_msg.add_to([user.email for user in User.objects.filter(is_admin=True)])
            sg.send(sending_msg)

            data = {"error": False, "response": "Blog Post created"}
        else:
            data = {"error": True, "response": validate_blog.errors}
        return HttpResponse(json.dumps(data), content_type="application/json; charset=utf-8")

    categories = Category.objects.all()
    c = {}
    c.update(csrf(request))
    return render(request, "admin/blog/blog-new.html", {"categories": categories, "csrf_token": c["csrf_token"]})