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

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

Example.

File size: 2.6 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
47#
48# curl -F naam=huub -F kaart=onbekend -F antenne=onbekend -F bestand=@manage.py http://localhost:8000/wlheatp/add/meetbestand/
49def add_meetbestand(request):
50 if request.method == 'POST':
51 form = MeetBestandForm(request.POST, request.FILES)
52 if form.is_valid():
53 gebruiker = Gebruiker.objects.get(naam=form.cleaned_data['naam'])
54 apparatuur, created = Apparatuur.objects.get_or_create(kaart=form.cleaned_data['kaart'],
55 antenne=form.cleaned_data['antenne'])
56 meetrondje, created = MeetRondje.objects.get_or_create(gebruiker=gebruiker,
57 datum=None,
58 naam=(form.cleaned_data['bestand']),
59 apparatuur=apparatuur)
60
61 meetbestand, created = MeetBestand.objects.get_or_create(meetrondje=meetrondje,is_imported=False)
62 if created:
63 meetbestand.bestand.save(form.cleaned_data['bestand'].name,form.cleaned_data['bestand'])
64 meetbestand.save()
65 return HttpResponse('OK')
66 else:
67 return HttpResponseServerError('ERROR File does already exists')
68
69 else:
70 form = MeetBestandForm()
71 return render_to_response('add_file.html', {'form': form})
Note: See TracBrowser for help on using the repository browser.