def add_ad_to_database(request): if request.POST: vars = {} adf = AdForm(request.POST, request.FILES) if adf.is_valid(): ad = Ad() if request.FILES: ad.has_pic = True else: ad.has_pic = False if ad.has_pic == True: w, h = get_image_dimensions(adf.cleaned_data['picture']) ad.picture_height = h ad.picture_width = w ad.title = adf.cleaned_data['title'] ad.content = adf.cleaned_data['content'] ad.adult_content = adf.cleaned_data['adult_content'] ad.author = request.user.id ad.save() if ad.has_pic == True: handle_uploaded_file(adf.cleaned_data['picture'], ad.id) #increase or add tags: tags = request.POST['tags'].split() for tag in tags: tag, created = Tag.objects.get_or_create(name=tag) #associate tag and ad tag.ad.add(ad) if created == True: #new tags already have count = 1 pass else: tag.count += 1 tag.save() return HttpResponseRedirect("/obqva/" + str(ad.id) + "/") else: vars = {} vars['adf'] = adf return render_to_response('search_by_tag_results.html', vars, context_instance=RequestContext(request)) else: raise MyException("Must be a POST request")
def add_ad_to_database(request): if request.POST: vars = {} adf = AdForm(request.POST, request.FILES) if adf.is_valid(): ad = Ad() if request.FILES: ad.has_pic = True else: ad.has_pic = False if ad.has_pic == True: w, h = get_image_dimensions( adf.cleaned_data['picture'] ) ad.picture_height = h ad.picture_width = w ad.title = adf.cleaned_data['title'] ad.content = adf.cleaned_data['content'] ad.adult_content = adf.cleaned_data['adult_content'] ad.author = request.user.id ad.save() if ad.has_pic == True: handle_uploaded_file( adf.cleaned_data['picture'] , ad.id) #increase or add tags: tags = request.POST['tags'].split() for tag in tags: tag, created = Tag.objects.get_or_create(name = tag) #associate tag and ad tag.ad.add(ad) if created == True: #new tags already have count = 1 pass else: tag.count += 1 tag.save() return HttpResponseRedirect("/obqva/" + str(ad.id) + "/") else: vars = {} vars['adf'] = adf return render_to_response('search_by_tag_results.html', vars, context_instance=RequestContext(request)) else: raise MyException("Must be a POST request")
def change_ad(request, id): if request.POST: try: a = Ad.objects.get(id = id) except Ad.DoesNotExist: raise MyException("Ad not found") if request.user.id == a.author: f = AdForm(request.POST, request.FILES, instance = a) if f.is_valid(): a.title = f.cleaned_data['title'] a.content = f.cleaned_data['content'] a.adult_content = f.cleaned_data['adult_content'] if request.FILES: a.picture = str(id) + ".jpg" a.has_pic = True handle_uploaded_file( f.cleaned_data['picture'] , id) w, h = get_image_dimensions( f.cleaned_data['picture'] ) a.picture_height = h a.picture_width = w else: a.has_pic = False a.save() org_tags = Tag.objects.filter(ad = id) tags = request.POST['tags'].split() # if tag was removed - delete or decrease count for ot in org_tags: if ot.name not in tags: if ot.count == 1: ot.delete() else: ot.count -= 1 ot.save() #remove assoc. between tag and ad ot.ad.remove(a) # if tags was introduced - add on increase count org_tags = org_tags.values_list("name",flat=True) for t in tags: if t not in org_tags: ntag, created = Tag.objects.get_or_create(name = t) #assoc. tag and ad ntag.ad.add(a) if created == False: ntag.count += 1 ntag.save() return HttpResponseRedirect("/obqva/" + id) else: request.session['flash_msg'] = "Невалидно попълнени данни" vars = {} vars['adf'] = f return render_to_response('edit_ad.html', vars, context_instance=RequestContext(request)) else: raise MyException("You are not the author of this ad!") else: raise MyException("Not a POST request")
def change_ad(request, id): if request.POST: try: a = Ad.objects.get(id=id) except Ad.DoesNotExist: raise MyException("Ad not found") if request.user.id == a.author: f = AdForm(request.POST, request.FILES, instance=a) if f.is_valid(): a.title = f.cleaned_data['title'] a.content = f.cleaned_data['content'] a.adult_content = f.cleaned_data['adult_content'] if request.FILES: a.picture = str(id) + ".jpg" a.has_pic = True handle_uploaded_file(f.cleaned_data['picture'], id) w, h = get_image_dimensions(f.cleaned_data['picture']) a.picture_height = h a.picture_width = w else: a.has_pic = False a.save() org_tags = Tag.objects.filter(ad=id) tags = request.POST['tags'].split() # if tag was removed - delete or decrease count for ot in org_tags: if ot.name not in tags: if ot.count == 1: ot.delete() else: ot.count -= 1 ot.save() #remove assoc. between tag and ad ot.ad.remove(a) # if tags was introduced - add on increase count org_tags = org_tags.values_list("name", flat=True) for t in tags: if t not in org_tags: ntag, created = Tag.objects.get_or_create(name=t) #assoc. tag and ad ntag.ad.add(a) if created == False: ntag.count += 1 ntag.save() return HttpResponseRedirect("/obqva/" + id) else: request.session['flash_msg'] = "Невалидно попълнени данни" vars = {} vars['adf'] = f return render_to_response( 'edit_ad.html', vars, context_instance=RequestContext(request)) else: raise MyException("You are not the author of this ad!") else: raise MyException("Not a POST request")