Beispiel #1
0
def create_dataset(request):
	if request.method != 'POST':
		#user has not filled out the upload form yet
		form = NewDataSetForm()
		add_formset = GeoLocationFormSet(prefix='add')
		remove_formset = RemoveGeoLocationFormSet(prefix='remove')
		return render_to_response('datasets/create_dataset.html', {'form': form, 'add_formset': add_formset, 'remove_formset':remove_formset, 'user':request.user,})
	else:
		# Create all the forms and formsets from the post data
		form = NewDataSetForm(request.POST, request.FILES)
		add_formset = GeoLocationFormSet(request.POST, prefix='add')
		remove_formset = RemoveGeoLocationFormSet(request.POST, prefix='remove')
		
		if form.is_valid():
			name = form.cleaned_data['name']
			description = form.cleaned_data['description']
			uploaded_files = form.cleaned_data['files']
			tags = form.cleaned_data['tags']
			
			new_dataset = DataSet.objects.create(creator=request.user, name=name, description=description, slug=slugify(name))
			new_dataset.tags.update_tags(tags, user=request.user)
			
			# Add all the locations specified in the add_formset
			if not add_formset.is_valid():
				print 'The add formset for the geolocations for the edit dataset page was not valid'
			else:
				# Get all of the geolocations
				for add_form in add_formset.forms:
					try:
						location = add_form.cleaned_data['add_location']
						import re
						# This pattern will match on [[-#.#, -#.#], '*', *
						location_pattern = re.compile(r"""^\[\[(?P<lat>-?\d+\.\d+), (?P<lng>-?\d+\.\d+)\], '(?P<name>.+?)',(?:.*)""")
						location_match = location_pattern.match(location)
						location_dict = location_match.groupdict()
						lat = location_dict['lat']
						lng = location_dict['lng']
						canonical_name = unicode(location_dict['name'])
						
						# If the location already exists, use it otherwise create a new one
						try:
							geoloc = GeoLoc.objects.get(longitude=lng, latitude=lat, canonical_name=canonical_name)
						except:
							geoloc = GeoLoc(longitude=lng, latitude=lat, canonical_name=canonical_name)
							geoloc.save()
						
						# Add the found/created location to the dataset
						new_dataset.geolocations.add(geoloc)
					except:
						print 'There was a problem in adding the location from the hidden field to the database'
			
			
			if not remove_formset.is_valid():
				print 'The remove formset for the geolocations for the edit dataset page was not valid'
			else:
				for remove_form in remove_formset.forms:
					try:
						location = remove_form.cleaned_data['remove_location']
						import re
						# This pattern will match on [[-#.#, -#.#], '*', *
						location_pattern = re.compile(r"""^\[\[(?P<lat>-?\d+\.\d+), (?P<lng>-?\d+\.\d+)\], '(?P<name>.+?)',(?:.*)""")
						location_match = location_pattern.match(location)
						location_dict = location_match.groupdict()
						lat = location_dict['lat']
						lng = location_dict['lng']
						canonical_name = unicode(location_dict['name'])

						try:
							geoloc = GeoLoc.objects.get(longitude=lng,latitude=lat, canonical_name=canonical_name)
						except:
							print "The geolocation to be removed does not appear to have been attached to this dataset"
						
						# Remove the geoloc from the dataset.  Note that the geoloc will still be in the database incase other things were attached.
						new_dataset.geolocations.remove(geoloc)
					except:
						print 'There was a problem in removing the location from the dataset'
			
			for uploaded_file in uploaded_files:
			 	new_datasetfile = DataSetFile(parent_dataset=new_dataset, file_contents=uploaded_file)
				new_datasetfile.file_contents.save(uploaded_file.name, uploaded_file, save=True)
				new_datasetfile.save()
			
			#show them the page for the dataset we just created
			return HttpResponseRedirect(reverse('epic.datasets.views.view_dataset', kwargs={'item_id':new_dataset.id,'slug':new_dataset.slug}))
		else:
			#form wasn't filled out correctly
			return render_to_response('datasets/create_dataset.html', {'form':form, 'add_formset': add_formset, 'remove_formset':remove_formset, 'user':request.user})
Beispiel #2
0
def edit_dataset(request, item_id, slug=None):
	dataset = get_object_or_404(DataSet, pk=item_id)
	user = request.user
	
	# Make sure the current user is the creator of the dataset.
	if user != dataset.creator:
		return HttpResponseRedirect(reverse("epic.datasets.views.view_dataset",
			kwargs={ "item_id": dataset.id, "slug":slug, }))
	
	if request.method != "POST":
		current_tags = dataset.tags.get_edit_string(user=request.user)
		initial_data_dictionary = {
			"name": dataset.name,
			"description": dataset.description,
			"tags": current_tags,
		}
		
		form = EditDataSetForm(initial=initial_data_dictionary)
		initial_location_data = []
		geolocs = GeoLoc.objects.filter(datasets=dataset.id)
		for geoloc in geolocs:
			initial_location_data.append({'add_location':geoloc,})
		add_formset = GeoLocationFormSet(prefix='add', initial=initial_location_data)
		remove_formset = RemoveGeoLocationFormSet(prefix='remove')
	else:
		form = EditDataSetForm(request.POST)
		add_formset = GeoLocationFormSet(request.POST, prefix='add')
		remove_formset = RemoveGeoLocationFormSet(request.POST, prefix='remove')
			
		if form.is_valid():
			dataset.name = form.cleaned_data["name"]
			dataset.description = form.cleaned_data["description"]
			dataset.slug = slugify(dataset.name)
			dataset.save()
			
			tags = form.cleaned_data["tags"]
			dataset.tags.update_tags(tags, user=user)
			
			# add all the locations from the add_formset if the add_formset is valid
			if not add_formset.is_valid():
				print 'The add formset for the geolocations for the edit dataset page was not valid'
			else:
				# Get all of the geolocations
				for add_form in add_formset.forms:
					try:
						location = add_form.cleaned_data['add_location']
						import re
						# This pattern will match on [[-#.#, -#.#], '*', *
						location_pattern = re.compile(r"""^\[\[(?P<lat>-?\d+\.\d+), (?P<lng>-?\d+\.\d+)\], '(?P<name>.+?)',(?:.*)""")
						location_match = location_pattern.match(location)
						location_dict = location_match.groupdict()
						lat = location_dict['lat']
						lng = location_dict['lng']
						canonical_name = unicode(location_dict['name'])
						# If the location already exists, use it otherwise create a new one
						try:
							geoloc = GeoLoc.objects.get(longitude=lng, latitude=lat, canonical_name=canonical_name)
						except:
							geoloc = GeoLoc(longitude=lng, latitude=lat, canonical_name=canonical_name)
							geoloc.save()
						
						# Add the found/created location to the dataset
						dataset.geolocations.add(geoloc)
					except:
						print 'There was a problem in adding the location from the hidden field to the database'
			
			# Remove all the locations from the remove_formset if the remove_formset is valid.  it is important that
			# this step happens after the add because if the user edited a page and added a location then removed it 
			# before saving there is both an add and a remove for that location in the same page.
			if not remove_formset.is_valid():
				print 'The remove formset for the geolocations for the edit dataset page was not valid'
			else:
				for remove_form in remove_formset.forms:
					try:
						location = remove_form.cleaned_data['remove_location']
						import re
						# This pattern will match on [[-#.#, -#.#], '*', *
						location_pattern = re.compile(r"""^\[\[(?P<lat>-?\d+\.\d+), (?P<lng>-?\d+\.\d+)\], '(?P<name>.+?)',(?:.*)""")
						location_match = location_pattern.match(location)
						location_dict = location_match.groupdict()
						lat = location_dict['lat']
						lng = location_dict['lng']
						canonical_name = unicode(location_dict['name'])
						try:
							geoloc = GeoLoc.objects.get(longitude=lng,latitude=lat, canonical_name=canonical_name)
						except:
							print "The geolocation to be removed does not appear to have been attached to this dataset"
						
						# Remove the geoloc from the dataset.  Note that the geoloc will still be in the database incase other things were attached.
						dataset.geolocations.remove(geoloc)
					except:
						print 'There was a problem in removing the location from the dataset'
			
			return HttpResponseRedirect(reverse("epic.datasets.views.view_dataset",
				kwargs={ "item_id": dataset.id, 'slug':slug, }))
			
	return render_to_response("datasets/edit_dataset.html", 
							  {
							  	'dataset': dataset,
								'user': user,
								'form': form,
								'add_formset': add_formset,
								'remove_formset':remove_formset,
							  })