#!/usr/bin/env python # import tornado.httpserver import tornado.ioloop import tornado.web import os import re import time import calendar import base64 import traceback import logging import cStringIO import json import cgi from urlparse import urlparse from server_config import account, storage from storage_base import WeaveStorageException from weave_handler import WeaveHandler class CollectionTimestampsHandler(WeaveHandler): @tornado.web.authenticated def get(self, name): self.check_account_match(name) ctx = storage.get_context(name) tsMap = storage.collection_timestamps(ctx) self.write(json.dumps(tsMap)) class CollectionCountsHandler(WeaveHandler): @tornado.web.authenticated def get(self, name): self.check_account_match(name) ctx = storage.get_context(name) ts = storage.collection_counts(ctx) self.write(json.dumps(ts)) class StorageHandler(WeaveHandler): @tornado.web.authenticated def get(self, name): self.check_account_match(name) pass class CollectionHandler(WeaveHandler): @tornado.web.authenticated def get(self, name, collection=None, id=None): self.check_account_match(name) if not collection: raise tornado.web.HTTPError(400, "Missing required collection") ctx = storage.get_context(name) wbo = storage.get(ctx, collection, id, query=self.request.arguments) if wbo: self.write(str(wbo)) else: raise tornado.web.HTTPError(404, "Not Found") def put(self, name, collection=None, id=None): self.check_account_match(name) if not collection: raise tornado.web.HTTPError(400, "Missing required collection") ctx = storage.get_context(name) try: ius = 'X-If-Unmodified-Since' in self.request.headers and self.request.headers['X-If-Unmodified-Since'] or None if ius and storage.collection_modification_date(ctx, collection) > float(ius): logging.error("Rejecting PUT because of modification date") raise tornado.web.HTTPError(412, "No overwrite") ts = storage.add_or_modify(ctx, collection, self.request.body, id=id, query=self.request.arguments) self.write(str(ts)) except WeaveStorageException, e: raise tornado.web.HTTPError(400, str(e)) def post(self, name, collection): self.check_account_match(name) if not collection: raise tornado.web.HTTPError(400, "Missing required collection") ctx = storage.get_context(name) ius = 'X-If-Unmodified-Since' in self.request.headers and self.request.headers['X-If-Unmodified-Since'] or None if ius and storage.collection_modification_date(ctx, collection) > float(ius): logging.error("Rejecting PUT because of modification date") raise tornado.web.HTTPError(412, "No overwrite") success_ids = [] failed_ids = {} # storage.begin_transaction() json_data = json.loads(self.request.body) ts = time.time() for j in json_data: try: storage.add_or_modify(ctx, collection, j) success_ids.append(j['id']) except Exception, e: try: failed_ids[j['id']] = str(e) except: pass # missing ID ends up here # storage.commit_transaction() self.write(json.dumps({'modified':ts, 'success':success_ids, 'failed':failed_ids})) def delete(self, name, collection, id=None): self.check_account_match(name) if not collection: raise tornado.web.HTTPError(400, "Missing required collection") ctx = storage.get_context(name) ius = 'X-If-Unmodified-Since' in self.request.headers and self.request.headers['X-If-Unmodified-Since'] or None if ius and storage.collection_modification_date(ctx, collection) > float(ius): logging.error("Rejecting PUT because of modification date") raise tornado.web.HTTPError(412, "No overwrite") if id: storage.delete(ctx, collection, id=id) else: storage.delete(ctx, collection, query=self.request.arguments) self.write(str(float(int(time.time() * 100)) / 100))