This commit is contained in:
j 2011-05-28 13:35:57 +02:00
parent 21d701d198
commit 6886c76255
8 changed files with 81 additions and 67 deletions

View file

@ -1,42 +0,0 @@
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division, with_statement
from django.db import models
from ox.django import fields
import managers
class Date(models.Model):
'''
Dates are dates in time that can be once or recurring,
From Mondays to Spring to 1989 to Roman Empire
'''
name = models.CharField(null=True, max_length=255, unique=True)
name_sort = models.CharField(null=True, max_length=255, unique=True)
name_find = models.TextField(default='', editable=True)
wikipediaId = models.CharField(max_length=1000, blank=True)
objects = managers.DateManager()
class Meta:
ordering = ('name_sort', )
#FIXME: how to deal with aliases
aliases = fields.TupleField(default=[])
#once|year|week|day
recurring = models.IntegerField(default=0)
#start yyyy-mm-dd|mm-dd|dow 00:00|00:00
#end yyyy-mm-dd|mm-dd|dow 00:00|00:01
start = models.CharField(null=True, max_length=255)
end = models.CharField(null=True, max_length=255)
def save(self, *args, **kwargs):
if not self.name_sort:
self.name_sort = self.name
self.name_find = self.name + '||'.join(self.aliases)
super(Date, self).save(*args, **kwargs)

View file

@ -6,6 +6,6 @@ from django.contrib import admin
import models
class DateAdmin(admin.ModelAdmin):
class EventAdmin(admin.ModelAdmin):
search_fields = ['name']
admin.site.register(models.Date, DateAdmin)
admin.site.register(models.Event, EventAdmin)

View file

@ -4,10 +4,10 @@
from django.db.models import Q, Manager
class DateManager(Manager):
class EventManager(Manager):
def get_query_set(self):
return super(DateManager, self).get_query_set()
return super(EventManager, self).get_query_set()
def find(self, q=''):
qs = self.get_query_set()

56
pandora/event/models.py Normal file
View file

@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division, with_statement
from django.db import models
from django.contrib.auth.models import User, Group
from ox.django import fields
import managers
class Event(models.Model):
'''
Events are events in time that can be once or recurring,
From Mondays to Spring to 1989 to Roman Empire
'''
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
user = models.ForeignKey(User, null=True, related_name='events')
name = models.CharField(null=True, max_length=255, unique=True)
name_sort = models.CharField(null=True, max_length=255, unique=True)
name_find = models.TextField(default='', editable=True)
wikipediaId = models.CharField(max_length=1000, blank=True)
alternativeNames = fields.TupleField(default=[])
objects = managers.EventManager()
class Meta:
ordering = ('name_sort', )
#start yyyy-mm-dd|mm-dd|dow 00:00|00:00
#end yyyy-mm-dd|mm-dd|dow 00:00|00:01
start = models.CharField(default='', max_length=255)
end = models.CharField(default='', max_length=255)
startTime = models.BigIntegerField(default=0)
endTime = models.BigIntegerField(default=0)
type = models.CharField(default='', max_length=255)
def save(self, *args, **kwargs):
if not self.name_sort:
self.name_sort = self.name
self.name_find = self.name + '||'.join(self.alternativeNames)
super(Event, self).save(*args, **kwargs)
def json(self):
return {
'name': self.name,
'alternativeNames': self.alternativeNames,
'start': self.start,
'end': self.end,
'type': self.type
}

View file

@ -11,20 +11,20 @@ from api.actions import actions
@login_required_json
def addDate(request):
def addEvent(request):
data = json.loads(request.POST['data'])
if models.Date.filter(name=data['name']).count() == 0:
place = models.Date(name = data['name'])
if models.Event.filter(name=data['name']).count() == 0:
place = models.Event(name = data['name'])
place.save()
response = json_response(status=200, text='created')
else:
response = json_response(status=403, text='place name exists')
return render_to_json_response(response)
actions.register(addDate, cache=False)
actions.register(addEvent, cache=False)
@login_required_json
def editDate(request):
def editEvent(request):
'''
param data
{
@ -34,34 +34,34 @@ def editDate(request):
date contains key/value pairs with place propterties
'''
data = json.loads(request.POST['data'])
Date = get_object_or_404_json(models.Date, pk=data['id'])
if Date.editable(request.user):
Event = get_object_or_404_json(models.Event, pk=data['id'])
if Event.editable(request.user):
conflict = False
names = [data['date']['name']] + data['date']['aliases']
for name in names: #FIXME: also check aliases!
if models.Date.filter(name=data['name']).exclude(id=Date.id).count() != 0:
if models.Event.filter(name=data['name']).exclude(id=Event.id).count() != 0:
conflict = True
if not conflict:
for key in data['date']:
setattr(Date, key, data['date'][key])
Date.save()
setattr(Event, key, data['date'][key])
Event.save()
response = json_response(status=200, text='updated')
else:
response = json_response(status=403, text='Date name/alias conflict')
response = json_response(status=403, text='Event name/alias conflict')
else:
response = json_response(status=403, text='permission denied')
return render_to_json_response(response)
actions.register(editDate, cache=False)
actions.register(editEvent, cache=False)
@login_required_json
def removeDate(request):
def removeEvent(request):
response = json_response(status=501, text='not implemented')
return render_to_json_response(response)
actions.register(removeDate, cache=False)
actions.register(removeEvent, cache=False)
def findDate(request):
def findEvent(request):
'''
param data
{'query': query, 'sort': array, 'range': array}
@ -91,13 +91,13 @@ Positions
query: query object, more on query syntax at
https://wiki.0x2620.org/wiki/pandora/QuerySyntax
ids: ids of dates for which positions are required
ids: ids of events for which positions are required
'''
data = json.loads(request.POST['data'])
response = json_response(status=200, text='ok')
response['data']['places'] = []
response['data']['events'] = []
#FIXME: add coordinates to limit search
for p in models.Date.objects.find(data['query']):
response['data']['dates'].append(p.json())
for p in models.Event.objects.find(data['query']):
response['data']['events'].append(p.json())
return render_to_json_response(response)
actions.register(findDate)
actions.register(findEvent)

View file

@ -124,7 +124,7 @@ INSTALLED_APPS = (
'annotation',
'app',
'archive',
'date',
'event',
'item',
'itemlist',
'person',