1 | from django import forms
|
---|
2 | from django.conf import settings
|
---|
3 | from django.contrib.auth import authenticate, login
|
---|
4 | from django.contrib.auth.decorators import login_required
|
---|
5 | from django.core.files.base import ContentFile
|
---|
6 | from django.db import IntegrityError
|
---|
7 | from django.http import HttpResponse,HttpResponseServerError
|
---|
8 | from django.shortcuts import render_to_response
|
---|
9 | from gheat.models import *
|
---|
10 |
|
---|
11 | def get_apperatuur_choices(item):
|
---|
12 | return sorted(set(Apparatuur.objects.values_list(item,item)))
|
---|
13 |
|
---|
14 | class 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/
|
---|
25 | def 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 |
|
---|