Exemplo n.º 1
0
def add_article(request, type):

  if not request.user.is_authenticated():
    return HttpResponseRedirect('base/login')
  c = {}
  c.update(csrf(request))

  if request.method == "POST":
    form = EditForm(request.POST, request.FILES)

    if form.is_valid():
      article = form.save(commit=False)
      profile_owner = UserProfile.objects.get(user__username__contains=request.user.username)
      article.owner = profile_owner
      if type=="article":
        article.is_project = False
      else:
        article.is_project = True
      article.save()
      # return HttpResponseRedirect("/") 
      return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
      # return HttpResponse(profile_owner.user.username) 
      
    # return HttpResponse("invalid form")
  else:
    form = EditForm()
  
  not_working = "not working"
  return render(request, 'article/add_article.html', 
    {"form" : form, 
    "token" : c, 
    "working" : not_working,
    "type" : type,
    })
Exemplo n.º 2
0
def edit(request, id=None):
  if not request.user.is_authenticated():
    return HttpResponse("not logged int")

  c = {}
  c.update(csrf(request))

  if id:
      article = get_object_or_404(Article, pk=id)
      if str(article.owner) != str(request.user):
          return HttpResponseRedirect("/")
  else:
      article = Article(author=request.user)

  if request.POST:
      form = EditForm(request.POST, request.FILES, instance=article)
      if form.is_valid():
          form.image = request.POST.get('image', False)
          form.save()

          # If the save was successful, redirect to another page
          return HttpResponseRedirect('/')

  else:
      form = EditForm(instance=article)

  return render(request, 'article/edit_article.html', {
      'form': form,
      'article_id' : id,
      'the_user' : request.user,
      'token' : c,
  }, context_instance=RequestContext(request))