add support for clicrdv date and time data sources
This commit is contained in:
parent
75dfb5e3ba
commit
412dfd6ade
|
@ -0,0 +1,94 @@
|
|||
import base64
|
||||
import datetime
|
||||
import json
|
||||
import urllib2
|
||||
import time
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib import admin
|
||||
from django.db import models
|
||||
from django.utils.dateformat import format as date_format
|
||||
from django.utils.dateformat import time_format
|
||||
|
||||
from .models import BaseDataSource
|
||||
|
||||
|
||||
def get_clicrdv_req(url):
|
||||
url = 'https://%s/api/v1/%s' % (settings.CLICRDV_SERVER, url)
|
||||
if '?' in url:
|
||||
url = url + '&apikey=%s&format=json' % settings.CLICRDV_API_KEY
|
||||
else:
|
||||
url = url + '?apikey=%s&format=json' % settings.CLICRDV_API_KEY
|
||||
|
||||
req = urllib2.Request(url)
|
||||
username = settings.CLICRDV_API_USERNAME
|
||||
password = settings.CLICRDV_API_PASSWORD
|
||||
authheader = 'Basic ' + base64.encodestring('%s:%s' % (username, password))[:-1]
|
||||
req.add_header('Authorization', authheader)
|
||||
return req
|
||||
|
||||
def get_json(url):
|
||||
return json.load(urllib2.urlopen(get_clicrdv_req(url)))
|
||||
|
||||
def get_available_timeslots(intervention, date_start=None, date_end=None):
|
||||
timeslots = []
|
||||
iid = intervention
|
||||
gid = get_json('interventions/%s' % iid).get('group_id')
|
||||
request_url = 'availabletimeslots?intervention_ids[]=%s&group_id=%s' % (iid, gid)
|
||||
if date_start is None:
|
||||
date_start = datetime.datetime.today().strftime('%Y-%m-%d')
|
||||
if date_end is None:
|
||||
date_end = (datetime.datetime.today() + datetime.timedelta(366)).strftime('%Y-%m-%d')
|
||||
if date_start:
|
||||
request_url = request_url + '&start=%s' % urllib2.quote(date_start)
|
||||
if date_end:
|
||||
request_url = request_url + '&end=%s' % urllib2.quote(date_end)
|
||||
for timeslot in get_json(request_url).get('availabletimeslots'):
|
||||
timeslots.append(timeslot.get('start'))
|
||||
timeslots.sort()
|
||||
return timeslots
|
||||
|
||||
|
||||
class ClicRdvDateDataSource(BaseDataSource):
|
||||
intervention_id = models.CharField(max_length=50)
|
||||
|
||||
def get_data(self):
|
||||
dates = []
|
||||
for timeslot in get_available_timeslots(self.intervention_id):
|
||||
parsed = datetime.datetime.strptime(timeslot, '%Y-%m-%d %H:%M:%S')
|
||||
date = {'id': parsed.strftime('%Y-%m-%d'),
|
||||
'text': date_format(parsed, 'j F Y')}
|
||||
if date in dates:
|
||||
continue
|
||||
dates.append(date)
|
||||
return dates
|
||||
|
||||
|
||||
class ClicRdvDateTimeDataSource(BaseDataSource):
|
||||
intervention_id = models.CharField(max_length=50)
|
||||
|
||||
parameters = ('date',)
|
||||
|
||||
def get_data(self, date):
|
||||
data = []
|
||||
for timeslot in get_available_timeslots(self.intervention_id,
|
||||
date_start='%s 00:00:00' % date,
|
||||
date_end='%s 23:59:59' % date):
|
||||
parsed = datetime.datetime.strptime(timeslot, '%Y-%m-%d %H:%M:%S')
|
||||
timed = {'id': parsed.strftime('%H:%M:%S'),
|
||||
'text': time_format(parsed, 'H:i')}
|
||||
data.append(timed)
|
||||
data.sort(lambda x,y: cmp(x.get('id'), y.get('id')))
|
||||
return data
|
||||
|
||||
|
||||
class ClicRdvDateDataSourceAdmin(admin.ModelAdmin):
|
||||
prepopulated_fields = {'slug': ('title',)}
|
||||
|
||||
admin.site.register(ClicRdvDateDataSource, ClicRdvDateDataSourceAdmin)
|
||||
|
||||
|
||||
class ClicRdvDateTimeDataSourceAdmin(admin.ModelAdmin):
|
||||
prepopulated_fields = {'slug': ('title',)}
|
||||
|
||||
admin.site.register(ClicRdvDateTimeDataSource, ClicRdvDateTimeDataSourceAdmin)
|
|
@ -10,6 +10,7 @@ class BaseDataSource(models.Model):
|
|||
slug = models.SlugField()
|
||||
description = models.TextField()
|
||||
|
||||
parameters = None
|
||||
objects = InheritanceManager()
|
||||
|
||||
def __unicode__(self):
|
||||
|
@ -41,3 +42,6 @@ class CsvDataSource(BaseDataSource):
|
|||
data.append({'id': get_key(row), 'text': get_value(row)})
|
||||
|
||||
return data
|
||||
|
||||
|
||||
import clicrdv
|
||||
|
|
|
@ -12,8 +12,9 @@ class View(TemplateView):
|
|||
ds = BaseDataSource.objects.get_subclass(slug=datasource)
|
||||
context['title'] = ds.title
|
||||
context['description'] = ds.description
|
||||
from json import dumps
|
||||
context['sample'] = dumps(
|
||||
if not ds.parameters:
|
||||
from json import dumps
|
||||
context['sample'] = dumps(
|
||||
to_json('api').obj_to_response(self.request, ds.get_data()[:3]),
|
||||
indent=4,
|
||||
ensure_ascii=False,
|
||||
|
@ -22,24 +23,24 @@ class View(TemplateView):
|
|||
|
||||
view = View.as_view()
|
||||
|
||||
@to_json('api')
|
||||
def json(request, datasource):
|
||||
def get_data(request, datasource):
|
||||
ds = BaseDataSource.objects.get_subclass(slug=datasource)
|
||||
q = request.GET.get('q')
|
||||
kwargs = {}
|
||||
for param in (ds.parameters or ()):
|
||||
kwargs[param] = request.GET.get(param)
|
||||
if q:
|
||||
q = q.lower()
|
||||
return sorted([x for x in ds.get_data() if q in x['text'].lower()])
|
||||
return sorted([x for x in ds.get_data(**kwargs) if q in x['text'].lower()])
|
||||
else:
|
||||
return sorted(ds.get_data())
|
||||
return sorted(ds.get_data(**kwargs))
|
||||
|
||||
@to_json('api')
|
||||
def json(request, datasource):
|
||||
return get_data(request, datasource)
|
||||
|
||||
@to_json('api')
|
||||
def jsonp(request, datasource):
|
||||
request.GET = request.GET.copy()
|
||||
request.GET['format'] = 'jsonp'
|
||||
ds = BaseDataSource.objects.get_subclass(slug=datasource)
|
||||
q = request.GET.get('q')
|
||||
if q:
|
||||
q = q.lower()
|
||||
return sorted([x for x in ds.get_data() if q in x['text'].lower()])
|
||||
else:
|
||||
return sorted(ds.get_data())
|
||||
return get_data(request, datasource)
|
||||
|
|
|
@ -161,6 +161,11 @@ LOGGING = {
|
|||
|
||||
LOGIN_REDIRECT_URL = '/'
|
||||
|
||||
CLICRDV_SERVER = None
|
||||
CLICRDV_API_KEY = None
|
||||
CLICRDV_API_USERNAME = None
|
||||
CLICRDV_API_PASSWORD = None
|
||||
|
||||
try:
|
||||
from local_settings import *
|
||||
except ImportError:
|
||||
|
|
Loading…
Reference in New Issue