source: src/django_gheat/wlheatmap/__init__.py@ 9660

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

Poor mans API for adding meetbestanden to the database.

File size: 2.5 KB
Line 
1from django.shortcuts import render_to_response
2from django.http import HttpResponse,HttpResponseServerError
3from django.conf import settings
4from gheat.models import *
5from django.db import IntegrityError
6
7import gheat.dataimport
8import logging
9
10logger = logging.getLogger(__name__)
11logger.setLevel(logging.DEBUG)
12
13def js_layer_base(request):
14 return render_to_response('js/LayerBase.js', {'settings' : settings },
15 mimetype='application/javascript')
16
17def process(request):
18 response = HttpResponse()
19 ch = logging.StreamHandler(stream=response)
20 ch.setLevel(logging.DEBUG)
21 formatter = logging.Formatter('%(levelname)-6s - %(name)-20s - %(message)s')
22 ch.setFormatter(formatter)
23 logging.getLogger().addHandler(ch)
24
25 response.write('<pre>')
26 for f in MeetBestand.objects.filter(is_imported=False):
27 logger.info("%s | %s", f.bestand.name, f.meetrondje)
28 try:
29 gheat.dataimport.import_file(f.bestand.path, f.meetrondje)
30 except IntegrityError, e:
31 logger.error(e)
32 continue
33 f.is_imported = True
34 f.save()
35 response.write('</pre>')
36 return response
37
38from django import forms
39from django.core.files.base import ContentFile
40
41class MeetBestandForm(forms.Form):
42 naam = forms.ChoiceField(choices=Gebruiker.objects.values_list('naam','naam'))
43 kaart = forms.ChoiceField(choices=set(Apparatuur.objects.values_list('kaart','kaart')))
44 antenne = forms.ChoiceField(choices=set(Apparatuur.objects.values_list('antenne','antenne')))
45 bestand = forms.FileField()
46
47def add_meetbestand(request):
48 if request.method == 'POST':
49 form = MeetBestandForm(request.POST, request.FILES)
50 if form.is_valid():
51 gebruiker = Gebruiker.objects.get(naam=form.cleaned_data['naam'])
52 apparatuur, created = Apparatuur.objects.get_or_create(kaart=form.cleaned_data['kaart'],
53 antenne=form.cleaned_data['antenne'])
54 meetrondje, created = MeetRondje.objects.get_or_create(gebruiker=gebruiker,
55 datum=None,
56 naam=(form.cleaned_data['bestand']),
57 apparatuur=apparatuur)
58
59 meetbestand, created = MeetBestand.objects.get_or_create(meetrondje=meetrondje,is_imported=False)
60 if created:
61 meetbestand.bestand.save(form.cleaned_data['bestand'].name,form.cleaned_data['bestand'])
62 meetbestand.save()
63 return HttpResponse('OK')
64 else:
65 return HttpResponseServerError('ERROR File does already exists')
66
67 else:
68 form = MeetBestandForm()
69 return render_to_response('add_file.html', {'form': form})
Note: See TracBrowser for help on using the repository browser.