Skip to content

Instantly share code, notes, and snippets.

@bengolder
Forked from cesandoval/hidden_id.py
Created August 8, 2012 23:06
Show Gist options
  • Save bengolder/3299604 to your computer and use it in GitHub Desktop.
Save bengolder/3299604 to your computer and use it in GitHub Desktop.
hidden id
#from models.py
class DataLayer(Named, Authored, Dated, Noted):
geometry_type = models.CharField(max_length=50, null=True, blank=True)
srs = models.CharField(max_length=50, null=True, blank=True)
files = models.ManyToManyField('DataFile', null=True, blank=True )
layer_id = models.AutoField(primary_key=True)
def __unicode__(self):
return "DataLayer: %s" % self.name
#from forms.py
class LayerReviewForm(forms.ModelForm):
"""For editing and configuring the layer information for each layer."""
data_file_id = forms.IntegerField(
widget=forms.HiddenInput,
)
class Meta:
model = DataLayer
fields = ['name', 'notes', 'geometry_type', 'srs']
#from models.py
class DataFile(Dated):
"""Data files represent individual file uploads.
They are used to construct DataLayers.
"""
file = models.FileField(upload_to=get_upload_path)
upload = models.ForeignKey('UploadEvent', null=True, blank=True)
def get_upload_path(self, filename):
return 'uploads/%s/%s' % (self.upload.user.username, filename)
def __unicode__(self):
return "DataFile: %s" % self.file.url
def get_layer_data(self):
data = {}
f = self.file
zip_file = zipfile.ZipFile(f)
contents = zip_file.namelist()
proj = [n for n in contents if '.prj' in n]
if proj:
# guess the srs
proj_text = zip_file.open( proj[0] ).read()
data['notes'] = proj_text
data['srs'] = ''
else:
data['srs'] = ''
# give a default name
basename = os.path.splitext(contents[0])[0]
data['name'] = basename
f.close()
# get the id for the hidden field in the form
data['data_file_id'] = self.id
return data
#from views.py
@login_required
def review(request):
"""A view for uploading new data.
"""
user=User.objects.get(username='carlos')
if request.method == 'POST': # someone is giving us data
formset = LayerReviewFormSet(request.POST)
if formset.is_valid():
for form in formset:
# get the DataFile id from the form data
data_file_id = form.cleaned_data['data_file_id']
# now get the actual object associated with that id
data_file = DataFile.objects.get(id=data_file_id)
srs = form.cleaned_data['srs']
layer = DataLayer(srs = srs)
layer = form.save(commit=False)
layer.author = user
layer.save() # the DataLayer must exist before we can add
# relations to it
# add the relation
layer.files.add( data_file )
# resave the layer
layer.save()
else: # we are asking them to review data
# get the last upload of this user
upload = UploadEvent.objects.filter(user=user).order_by('-date')[0]
data_files = DataFile.objects.filter(upload=upload)
layer_data = [ f.get_layer_data() for f in data_files ]
formset = LayerReviewFormSet( initial=layer_data )
c = {
'formset':formset,
}
return render_to_response(
'webfinches/review.html',
RequestContext(request, c),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment