예제 #1
0
def edit(request, url):
    #Makes sure user is logged in
    username = util.checkLoggedIn(request)
    if not username:
        return HttpResponseRedirect('/')
    error = ""
    if request.method == "POST":
        
        try:
            #Check if user clicked Delete Button
            if request.POST.has_key('Delete'):
                Post.objects.get(pk=url).delete()
                return HttpResponseRedirect('/')
            
            form = blogForms.newPostForm(request.POST)
            #Check if user uploaded image
            if 'image' in request.FILES:
                image = request.FILES['image']
            else:
                image = None
            #saves post, returning an error if failed
            error, post = save(request, form, image, "Post", url)
            
            if not error:
                return HttpResponseRedirect('/')
            else:
                return render_to_response("dashboard/newpost.html", {"form":form, "post":url, "error":error, "username":username, "edit":True, "action":url}, context_instance = RequestContext(request))
        except ObjectDoesNotExist:
            raise Http404
    else:
        #Loads the post into the form, so it can be edited
        try:
            # Makes sure post exists
            post = Post.objects.get(pk=url)
            url = str(url)

            if post:                
                # Gets all categories for post
                categories = Categories.objects.filter(Post__title=post.title)

                # Gets if the post is featured
                featured = Featured.objects.filter(Post__title = post.title)
                if featured:
                    isFeatued = True
                else:
                    isFeatued = False

                # Checks if the image was included in post
                if post.image:
                    imageInPost = True
                else:
                    imageInPost = False

                # Sets initial value
                form = blogForms.newPostForm(initial = {'title':post.title, 'sourceUrl':post.sourceUrl, 'content':post.content, 'categories': categories, 'featured': isFeatued, 'imageInPost': imageInPost})
                return render_to_response("dashboard/newpost.html", {"form":form, "post":url, "error":error, "username":username, "edit":True, "action":url}, context_instance = RequestContext(request))
            else:
                raise Http404
        except ObjectDoesNotExist:
            raise Http404
예제 #2
0
def newpost(request):
    #Makes sure user is logged in, else redirect to home page
    username = util.checkLoggedIn(request)
    if not username:
        return HttpResponseRedirect('/')
    error = ""
    
    
    if request.method == "POST":
        
        
        form = blogForms.newPostForm(request.POST)
        #Check if there is an image
        if 'image' in request.FILES:
            image = request.FILES['image']
        else:
            image = None
        #saves post, returning an error if failed
        error, post = save(request, form, image, "Post")
        if not error:
            if not settings.DEBUG:
                tweet = post.title + " - " + "http://www.theconnectedwire.com/" + post.link
                twitter.updateTwitter(tweet)
                return HttpResponseRedirect('/' + "?tweet=" + tweet)
            else:
                return HttpResponseRedirect('/')
    
    else:
        form = blogForms.newPostForm()
    return render_to_response("dashboard/newpost.html", {"form":form, "error": error, "username": username, "action":"newpost"}, context_instance = RequestContext(request))