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

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

Give a hint of which values are available..

File size: 2.6 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 def get_help_text(model, item):
16 return "Reeds bekend: %s" % ','.join(model.objects.values_list(item,flat=True).distinct())
17 username = forms.CharField()
18 password = forms.CharField()
19 naam = forms.CharField(help_text=get_help_text(Gebruiker, 'naam'))
20 kaart = forms.CharField(help_text=get_help_text(Apparatuur, 'kaart'))
21 antenne = forms.CharField(help_text=get_help_text(Apparatuur, 'antenne'))
22 surveyid = forms.CharField(help_text=get_help_text(MeetRondje, 'naam'))
23 bestand = forms.FileField()
24
25#
26# 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/
27def add_meetbestand(request):
28 if request.method == 'POST':
29 form = MeetBestandForm(request.POST, request.FILES)
30 if form.is_valid():
31 username = form.cleaned_data['username']
32 password = form.cleaned_data['password']
33 user = authenticate(username=username, password=password)
34 if user == None or not user.is_active:
35 return HttpResponseServerError('ERROR: Authentication Failed')
36
37 login(request, user)
38
39 gebruiker, created = Gebruiker.objects.get_or_create(naam=form.cleaned_data['naam'])
40 apparatuur, created = Apparatuur.objects.get_or_create(kaart=form.cleaned_data['kaart'],
41 antenne=form.cleaned_data['antenne'])
42 meetrondje, created = MeetRondje.objects.get_or_create(gebruiker=gebruiker,
43 datum=None,
44 naam=(form.cleaned_data['surveyid']),
45 apparatuur=apparatuur)
46
47 meetbestand, created = MeetBestand.objects.get_or_create(meetrondje=meetrondje,is_imported=False)
48 if created:
49 meetbestand.bestand.save(form.cleaned_data['bestand'].name,form.cleaned_data['bestand'])
50 meetbestand.save()
51 return HttpResponse('OK: Attached to meetrondje %s' % meetrondje)
52 else:
53 return HttpResponseServerError('ERROR: File %s does already exists' % meetbestand)
54 else:
55 return HttpResponseServerError('ERROR: No valid input')
56 else:
57 form = MeetBestandForm()
58 return render_to_response('add_file.html', {'form': form})
59
Note: See TracBrowser for help on using the repository browser.