Пример #1
0
def PrizeNew(request):
	if not request.user.is_authenticated():
		return HttpResponseRedirect("/epanel")
	else: 
		if request.method == 'POST': 
			form = OfferNewForm(request.POST)
			if(form.is_valid()):
				offer = Offer(**form.cleaned_data)

				# set slug
				slug = slugify(request.POST['title'])						
				offer.slug = slug

				# set company_id
				user_email = request.user.email
				company = Company.objects.get(contact_email=user_email)
				offer.company_id = company.id
				offer.status = "B"

				offer.save()

				# redirect to media page
				return HttpResponseRedirect("/epanel/premios/multimedia/%d" % offer.id)
		else: 
			form = OfferNewForm()

		context = {'form':form, 'title': 'nuevo', 'submit': 'Guardar premio'}
		return render_to_response('epanel/offer/new.html', context, context_instance=RequestContext(request))
Пример #2
0
def PrizeEdit(request, offer_id):
	if not request.user.is_authenticated():
		return HttpResponseRedirect("/epanel")
	else: 
		offer = Offer.objects.get(pk=offer_id)
		if offer is None: 
			return HttpResponseRedirect("/epanel")
		else: 
			if request.method == 'POST': 
				form = OfferNewForm(request.POST)
				if(form.is_valid()):

					new_offer = Offer(**form.cleaned_data)
					new_offer.id = offer.id
					new_offer.slug = offer.slug
					new_offer.company = offer.company	
					new_offer.media_type = offer.media_type
					new_offer.media_url = offer.media_url
					new_offer.media_image = offer.media_image
					new_offer.media_thumb = offer.media_thumb	
					new_offer.status = offer.status			
					new_offer.save()

					# update elasticsearch
					prize = Offer.objects.get(pk=offer.id)
					if(prize.status == 'C'):
						es = ES("localhost:9200")
						prizeES = es.get("prize","prize",prize.id)
						prizeES.title = prize.title
						prizeES.description = prize.description
						prizeES.vote_limit = prize.vote_limit
						prizeES.vote_source = prize.vote_source
						prizeES.category = prize.category.name
						prizeES.save()						

					# redirect to edit page
					return HttpResponseRedirect("/epanel/premios/editar/%d" % offer.id)
			else: 
				form = OfferNewForm(initial={
						'title': offer.title,
						'prize_name': offer.prize_name,
						'stock': offer.stock,
						'category': offer.category,
						'vote_limit': offer.vote_limit,
						'vote_source': offer.vote_source,
						'description': offer.description,
						'conditions': offer.conditions,
					}, auto_id=False)

		context = {'form':form, 'title': 'editar', 'submit': 'Editar premio'}
		return render_to_response('epanel/offer/new.html', context, context_instance=RequestContext(request))