Ignore:
Timestamp:
Sep 4, 2011, 6:14:03 PM (13 years ago)
Author:
rick
Message:

Poor mans API for adding meetbestanden to the database.

Location:
src/django_gheat/wlheatmap
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • src/django_gheat/wlheatmap/__init__.py

    r9658 r9660  
    11from django.shortcuts import render_to_response
    2 from django.http import HttpResponse
     2from django.http import HttpResponse,HttpResponseServerError
    33from django.conf import settings
    4 from gheat.models import MeetBestand
     4from gheat.models import *
    55from django.db import IntegrityError
    66
     
    3535  response.write('</pre>')
    3636  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})
  • src/django_gheat/wlheatmap/urls.py

    r9658 r9660  
    2020    url(r'^filters/$', 'filters.serve_filters'),
    2121    url(r'^process/$', 'process'),
     22    url(r'^add/meetbestand/$', 'add_meetbestand'),
    2223)
Note: See TracChangeset for help on using the changeset viewer.