Exemplo n.º 1
0
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")
Exemplo n.º 2
0
def display_edit_ad(request, id):
    try:
        a = Ad.objects.get(id=id)
    except Ad.DoesNotExist:
        raise MyException("Ad not found")

    if request.user.id == a.author:
        vars = {}
        vars['adf'] = AdForm(instance=a)

        tags = Tag.objects.filter(ad=id)
        response = ""
        for tag in tags:
            response += tag.name + " "
        response = response[:-1]

        vars['tags'] = response
        vars['ad'] = a
        # if data is not correctly populated the "change ad" view will return a
        # flash message.
        try:
            vars['flash_message'] = request.session['flash_msg']
            del request.session['flash_msg']
        except KeyError:
            pass

        return render_to_response('edit_ad.html',
                                  vars,
                                  context_instance=RequestContext(request))
    else:
        raise MyException("You are not the author of this ad!")
Exemplo n.º 3
0
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")
Exemplo n.º 4
0
def display_add_ad_page(request):
    if request.method == "GET":
        vars = {}
        try:
            vars['flash_message'] = request.session['flash_msg']
            del request.session['flash_msg']
        except KeyError:
            pass
        if request.user.is_authenticated():
            vars['adf'] = AdForm()
            vars['ads'] = Ad.objects.filter(author=request.user.id)
            return render_to_response('add_ad.html',
                                      vars,
                                      context_instance=RequestContext(request))
        else:  # redirect to frontpage and display message
            request.session[
                'flash_msg'] = u"Само регистрирани потребители могат да добавят обяви"
            return HttpResponseRedirect("/")
    else:
        raise MyException("Only GET request allowed" + request.method)
Exemplo n.º 5
0
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")
Exemplo n.º 6
0
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")