Browse files

more api calls

add UserProgram get api call
implemented filter argument to session handler
  • Loading branch information...
1 parent 92261a0 commit 21c2a8bbcfcd8ddc027731f2b766c8bbedb2e244 @poelzi committed Aug 3, 2010
Showing with 39 additions and 30 deletions.
  1. +30 −24 api/handlers.py
  2. +8 −5 api/urls.py
  3. +1 −1 db/models.py
View
54 api/handlers.py
@@ -2,7 +2,7 @@
from piston.doc import generate_doc
from piston.utils import rc, require_mime, require_extended, FormValidationError
-from uberclock.db.models import Detector, Session, Entry, LearnData
+from uberclock.db.models import Detector, Session, Entry, LearnData, UserProgram
from django.contrib.auth.models import User
from django.conf import settings
from django.core.exceptions import ValidationError
@@ -51,37 +51,33 @@ class DetectorHandler(BaseHandler):
'ident',
('default_user', ('id', 'username')))
-class DetectorListHandler(DetectorHandler):
+
+class UserProgramHandler(BaseHandler):
allowed_methods = ('GET',)
+ model = UserProgram
+ fields = ('id',
+ ('user', ('id', 'username')),
+ 'users_id',
+ 'default',
+ 'alarm_key',
+ 'name',
+ 'short_name',
+ 'default_wakeup',
+ 'default_sleep_time',
+ 'default_window',
+ 'lights_action',
+ 'wakeup_action')
-# SESSIONS
-class SessionListHandler(BaseHandler):
- allowed_methods = ('GET', 'PUT', 'POST')
+class SessionHandler(BaseHandler):
+ allowed_methods = ('GET', 'POST')
model = Session
- fields = ('id',
+ list_fields = ('id',
('user', ('id', 'username')),
'start', 'stop',
('detector', ('id', 'name')),
('program', ('id',)),
'closed', 'new')
-
- #fields = (re.compile('.*'),)
-
-
-
- #include = ('id',)
-
-
-# @staticmethod
-# def resource_uri():
-# return ('session_list_handler', [])
-
-
-class SessionHandler(BaseHandler):
- allowed_methods = ('GET', 'POST')
- model = Session
-
fields = ("id",
"start",
"stop",
@@ -98,6 +94,14 @@ class SessionHandler(BaseHandler):
"lights_action",
"wakeup_action")
+ def queryset(self, request):
+ if "filter" in request.GET:
+ if request.GET["filter"] == "active":
+ return self.model.objects.get_active_sessions()
+ if request.GET["filter"] == "new":
+ return self.model.objects.get_new_sessions()
+ return self.model.objects.all()
+
def create(self, request):
"""
Creates a new blogpost.
@@ -115,9 +119,11 @@ def create(self, request):
if "user" in attrs:
try:
usr = User.objects.get(id=int(attrs["user"]))
- except ValueError, Users.DoesNotExist:
+ except (ValueError, Users.DoesNotExist), e:
usr = User.objects.get(username=attrs["user"])
args["user"] = usr
+ if "program" in attrs:
+ args["program"] = UserProgram.objects.get(id=int(attrs["program"]))
if "detector" in attrs:
args["detector"] = Detector.get(id=attrs["detector"])
View
13 api/urls.py
@@ -8,23 +8,26 @@
actions_list_handler = Resource(handlers.ActionsHandler)
detector_handler = Resource(handlers.DetectorHandler)
-detector_list_handler = Resource(handlers.DetectorListHandler)
+
+userprogram_handler = Resource(handlers.UserProgramHandler)
+
session_handler = Resource(handlers.SessionHandler)
-session_list_handler = Resource(handlers.SessionListHandler)
session_entries_handler = Resource(handlers.SessionEntriesHandler)
session_learn_handler = Resource(handlers.SessionLearnHandler)
+
urlpatterns = patterns('',
url(r'^docs/', documentation_view),
url(r'^user/', user_list_handler, name="user_list_handler"),
url(r'^detector/(?P<id>[^/]+)/', detector_handler, name="detectors_handler"),
- url(r'^detector/$', detector_list_handler, name="detector_list_handler"),
+ url(r'^detector/$', detector_handler, name="detector_list_handler"),
url(r'^actions/', actions_list_handler, name="actions_list_handler"),
+ url(r'^userprogram/(?P<id>[^/]+)/', userprogram_handler, name="userprogram_handler"),
+ url(r'^userprogram/', userprogram_handler, name="userprogram_list_handler"),
url(r'^session/(?P<session>[^/]+)/learndata/', session_learn_handler, name="session_learn_handler"),
url(r'^session/(?P<session>[^/]+)/entries/', session_entries_handler, name="session_entries_handler"),
- url(r'^session/new/$', session_handler, name="session_handler"),
url(r'^session/(?P<id>[^/]+)/$', session_handler, name="session_handler"),
- url(r'^session/$', session_list_handler, name="session_list_handler"),
+ url(r'^session/$', session_handler, name="session_list_handler"),
)
View
2 db/models.py
@@ -207,7 +207,7 @@ def get_active_sessions(self, **kwargs):
return self.filter(stop__gt=start, closed=False, **kwargs)
def get_new_sessions(self, **kwargs):
- return self.filter(new=True, **kwargs)
+ return self.filter(new=True, closed=False, **kwargs)
def get_new_rf_id(self):
id_s = range(1, (2**(RF_ID_BIT_LENGHT)))

0 comments on commit 21c2a8b

Please sign in to comment.