1 | from django.shortcuts import render_to_response
|
---|
2 | from django.http import HttpResponse,HttpResponseServerError
|
---|
3 | from django.conf import settings
|
---|
4 | from gheat.models import *
|
---|
5 | from django.db import IntegrityError
|
---|
6 |
|
---|
7 | import gheat.dataimport
|
---|
8 | import logging
|
---|
9 |
|
---|
10 | logger = logging.getLogger(__name__)
|
---|
11 | logger.setLevel(logging.DEBUG)
|
---|
12 |
|
---|
13 | def js_layer_base(request):
|
---|
14 | return render_to_response('js/LayerBase.js', {'settings' : settings },
|
---|
15 | mimetype='application/javascript')
|
---|
16 |
|
---|
17 | def 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 |
|
---|
38 | from django import forms
|
---|
39 | from django.core.files.base import ContentFile
|
---|
40 |
|
---|
41 | class 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 | def 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})
|
---|