source: src/django_gheat/wlheatmap/forms.py@ 9749

Last change on this file since 9749 was 9749, checked in by rick, 13 years ago

Some awefull quirks to avoid uploading files without authentication.

File size: 2.4 KB
Line 
1from django import forms
2from django.conf import settings
3from django.contrib.auth import authenticate, login
4from django.contrib.auth.decorators import login_required
5from django.core.files.base import ContentFile
6from django.db import IntegrityError
7from django.http import HttpResponse,HttpResponseServerError
8from django.shortcuts import render_to_response
9from gheat.models import *
10
11def get_apperatuur_choices(item):
12 return sorted(set(Apparatuur.objects.values_list(item,item)))
13
14class MeetBestandForm(forms.Form):
15 username = forms.CharField()
16 password = forms.CharField()
17 naam = forms.ChoiceField(choices=Gebruiker.objects.values_list('naam','naam'))
18 kaart = forms.ChoiceField(choices=get_apperatuur_choices('kaart'))
19 antenne = forms.ChoiceField(choices=get_apperatuur_choices('antenne'))
20 surveyid = forms.CharField()
21 bestand = forms.FileField()
22
23#
24# curl -Fusername=huub -Fpassword=bert -F naam=huub -F kaart=onbekend -F antenne=onbekend -F bestand=@manage.py -F surveyid=kerk http://localhost:8000/wlheatmap/add/meetbestand/
25def add_meetbestand(request):
26 if request.method == 'POST':
27 form = MeetBestandForm(request.POST, request.FILES)
28 if form.is_valid():
29 username = form.cleaned_data['username']
30 password = form.cleaned_data['password']
31 user = authenticate(username=username, password=password)
32 if user == None or not user.is_active:
33 return HttpResponseServerError('ERROR: Authentication Failed')
34
35 login(request, user)
36
37 gebruiker = Gebruiker.objects.get(naam=form.cleaned_data['naam'])
38 apparatuur, created = Apparatuur.objects.get_or_create(kaart=form.cleaned_data['kaart'],
39 antenne=form.cleaned_data['antenne'])
40 meetrondje, created = MeetRondje.objects.get_or_create(gebruiker=gebruiker,
41 datum=None,
42 naam=(form.cleaned_data['surveyid']),
43 apparatuur=apparatuur)
44
45 meetbestand, created = MeetBestand.objects.get_or_create(meetrondje=meetrondje,is_imported=False)
46 if created:
47 meetbestand.bestand.save(form.cleaned_data['bestand'].name,form.cleaned_data['bestand'])
48 meetbestand.save()
49 return HttpResponse('OK: Attached to meetrondje %s' % meetrondje)
50 else:
51 return HttpResponseServerError('ERROR: File does already exists')
52 else:
53 return HttpResponseServerError('ERROR: No valid input')
54 else:
55 form = MeetBestandForm()
56 return render_to_response('add_file.html', {'form': form})
57
Note: See TracBrowser for help on using the repository browser.