Пример #1
0
def my_gadgets(request):
	
	if request.GET.has_key("delete"):
		try:
			request.user.my_gadgets.get(pk=request.GET["delete"]).delete()
		except:
			pass # Silent error on hacking attempt
	
	gadget_registered = False
	if request.method == "POST":
		form = GadgetRegistryForm(data=request.POST, files=request.FILES)
		
		if form.is_valid():
			gadget = form.save(commit=False)
			gadget.by_user = request.user
			if not form.external_url():
				gadget.hosted_filename = form.cleaned_data["upload"]
			gadget.save()
			
			# New form
			gadget_registered = True
			form = GadgetRegistryForm()
	else:
		form = GadgetRegistryForm()
	
	my_gadgets = request.user.my_gadgets.all()
	return render_to_response('pygowave_server/gadgets/my_gadgets.html',
							  {"my_gadgets": my_gadgets,
							   "form": form,
							   "gadget_registered": gadget_registered},
							  context_instance=RequestContext(request))
Пример #2
0
def my_gadgets(request):
	
	if request.GET.has_key("delete"):
		try:
			request.user.my_gadgets.get(pk=request.GET["delete"]).delete()
		except:
			pass # Silent error on hacking attempt
	
	duplicate = False
	can_overwrite = False
	gadget_registered = False
	if request.method == "POST":
		if not request.GET.has_key("duplicate"):
			form = GadgetRegistryForm(data=request.POST, files=request.FILES)
			if form.is_valid():
				gadget = form.save(commit=False)
				gadget.by_user = request.user
				if not form.external_url():
					gadget.hosted_filename = form.cleaned_data["upload"]
				try:
					old_gadget = Gadget.objects.get(title=gadget.title)
				except ObjectDoesNotExist:
					gadget.save()
					
					# New form
					gadget_registered = True
					form = GadgetRegistryForm()
				else:
					duplicate = True
					if old_gadget.by_user == request.user:
						can_overwrite = True
					form = DuplicateGadgetForm(initial={
						"external": form.cleaned_data["external"],
						"title": form.cleaned_data["title"],
						"description": form.cleaned_data["description"],
						"url": form.cleaned_data["url"]
					})
		else:
			form = DuplicateGadgetForm(data=request.POST)
			if request.POST.has_key("cancel"):
				form = GadgetRegistryForm()
			elif form.is_valid():
				gadget = form.save(commit=False)
				gadget.by_user = request.user
				if not form.external_url():
					gadget.hosted_filename = form.cleaned_data["url"].split("/")[-1]
				try:
					old_gadget = Gadget.objects.get(title=gadget.title)
				except ObjectDoesNotExist:
					gadget.save()
					
					gadget_registered = True
					form = GadgetRegistryForm()
				else:
					if old_gadget.by_user == request.user:
						old_gadget.url = gadget.url
						old_gadget.description = gadget.description
						old_gadget.hosted_filename = gadget.hosted_filename
						old_gadget.save()
						
						gadget_registered = True
						form = GadgetRegistryForm()
					else:
						duplicate = True
			else:
				duplicate = True
	else:
		form = GadgetRegistryForm()
	
	my_gadgets = request.user.my_gadgets.all()
	return render_to_response('pygowave_server/gadgets/my_gadgets.html',
							  {"my_gadgets": my_gadgets,
							   "form": form,
							   "gadget_registered": gadget_registered,
							   "duplicate": duplicate,
							   "can_overwrite": can_overwrite},
							  context_instance=RequestContext(request))