#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2015-06-09 20:53:40
# @Author  : Your Name (you@example.org)
# @Link    : http://example.org
# @Version : $Id$

import tornado.web
from tornado.web import asynchronous
from tornado.options import options
import json
from bson import ObjectId
from log import get_logger
import optionsSetting
from handler_baseHt import BaseHtHandler
from mongo_field_encoder import MongoFieldEncoder
from mongolog import MongoHandler
from mongokit.helpers import totimestamp, fromtimestamp
from auth import requires_auth
from tornado.httpclient import HTTPRequest
from utils.mongo_front import MongoFront
import datetime
try:
    from tornado.curl_httpclient import CurlAsyncHTTPClient as AsyncHTTPClient
except ImportError:
    from tornado.simple_httpclient import SimpleAsyncHTTPClient as AsyncHTTPClient
from model.version import Versions
log = get_logger()

class VersionsHandler(BaseHtHandler):
    """
    版本管理类
    """
    def __init__(self, application, request, **kwargs):
        super(VersionsHandler, self).__init__(application, request, **kwargs)

    @asynchronous
    @requires_auth
    def get(self):
        log.info('VersionHandler.get')
        where = {}
        version_id = self.get_argument("version_id",None)
        if version_id!=None:
             where = {"_id": ObjectId(version_id)}
        sort = [("created",-1)]
        version = options.dbconn.Versions.find(where).sort(sort)

        response={}
        response["entities"]=list(version) or []
        response["count"]=str(version.count())
        self.write(json.dumps(response,indent=2, ensure_ascii=False,cls=MongoFieldEncoder))
        self.finish()
        
    @asynchronous
    @requires_auth
    def post(self):
        log.info('VersionHandler.post')
        request = self.request.body
        created = datetime.datetime.utcnow()
        version = options.dbconn.Versions.from_json(request)
        version['created'] = created
      
        version.save()
      
        self.write(json.dumps({},indent=2, ensure_ascii=False,cls=MongoFieldEncoder))
        self.finish()


    @asynchronous
    @requires_auth
    def put(self):
        log.info("ActivityHandler.put:"+self.request.body)
        body_dic = json.loads(self.request.body,encoding="utf-8")
        version_id = body_dic.get("version_id",None)
        if version_id == None:
            self.fire_response_bad_request()
            return
        del body_dic["version_id"]

        if "tiptop" in body_dic:
            os = body_dic.get("os","")
            lookup = {"tiptop":True,"os":os}
            update = {"$unset":{"tiptop":""}}
            options.dbconn.Versions.find_and_modify(lookup, update,new=True)

        lookup = {"_id": ObjectId(version_id)}
        body_dic["modified"] = datetime.datetime.utcnow()
        option = {'$set': body_dic}
        
        version = options.dbconn.Versions.find_and_modify(lookup, option, new=True)
        self.finish()



    @asynchronous
    @requires_auth
    def delete(self):
        log.info("ActivityHandler.delete:"+self.request.body)
        version_id = self.get_argument("version_id",None)
        if version_id == None:
            self.fire_response_bad_request()
            return
        lookup = {"_id": ObjectId(version_id)}
        version = MongoFront.remove(lookup,"versions")
        self.finish()








9fff54fe3ce0c9a30ae1d36992c85be6a1cc61f9
<mask token>


class VersionsHandler(BaseHtHandler):
    """
    版本管理类
    """

    def __init__(self, application, request, **kwargs):
        super(VersionsHandler, self).__init__(application, request, **kwargs)

    @asynchronous
    @requires_auth
    def get(self):
        log.info('VersionHandler.get')
        where = {}
        version_id = self.get_argument('version_id', None)
        if version_id != None:
            where = {'_id': ObjectId(version_id)}
        sort = [('created', -1)]
        version = options.dbconn.Versions.find(where).sort(sort)
        response = {}
        response['entities'] = list(version) or []
        response['count'] = str(version.count())
        self.write(json.dumps(response, indent=2, ensure_ascii=False, cls=
            MongoFieldEncoder))
        self.finish()

    @asynchronous
    @requires_auth
    def post(self):
        log.info('VersionHandler.post')
        request = self.request.body
        created = datetime.datetime.utcnow()
        version = options.dbconn.Versions.from_json(request)
        version['created'] = created
        version.save()
        self.write(json.dumps({}, indent=2, ensure_ascii=False, cls=
            MongoFieldEncoder))
        self.finish()

    @asynchronous
    @requires_auth
    def put(self):
        log.info('ActivityHandler.put:' + self.request.body)
        body_dic = json.loads(self.request.body, encoding='utf-8')
        version_id = body_dic.get('version_id', None)
        if version_id == None:
            self.fire_response_bad_request()
            return
        del body_dic['version_id']
        if 'tiptop' in body_dic:
            os = body_dic.get('os', '')
            lookup = {'tiptop': True, 'os': os}
            update = {'$unset': {'tiptop': ''}}
            options.dbconn.Versions.find_and_modify(lookup, update, new=True)
        lookup = {'_id': ObjectId(version_id)}
        body_dic['modified'] = datetime.datetime.utcnow()
        option = {'$set': body_dic}
        version = options.dbconn.Versions.find_and_modify(lookup, option,
            new=True)
        self.finish()

    @asynchronous
    @requires_auth
    def delete(self):
        log.info('ActivityHandler.delete:' + self.request.body)
        version_id = self.get_argument('version_id', None)
        if version_id == None:
            self.fire_response_bad_request()
            return
        lookup = {'_id': ObjectId(version_id)}
        version = MongoFront.remove(lookup, 'versions')
        self.finish()

<mask token>
try:
    from tornado.curl_httpclient import CurlAsyncHTTPClient as AsyncHTTPClient
except ImportError:
    from tornado.simple_httpclient import SimpleAsyncHTTPClient as AsyncHTTPClient
<mask token>


class VersionsHandler(BaseHtHandler):
    """
    版本管理类
    """

    def __init__(self, application, request, **kwargs):
        super(VersionsHandler, self).__init__(application, request, **kwargs)

    @asynchronous
    @requires_auth
    def get(self):
        log.info('VersionHandler.get')
        where = {}
        version_id = self.get_argument('version_id', None)
        if version_id != None:
            where = {'_id': ObjectId(version_id)}
        sort = [('created', -1)]
        version = options.dbconn.Versions.find(where).sort(sort)
        response = {}
        response['entities'] = list(version) or []
        response['count'] = str(version.count())
        self.write(json.dumps(response, indent=2, ensure_ascii=False, cls=
            MongoFieldEncoder))
        self.finish()

    @asynchronous
    @requires_auth
    def post(self):
        log.info('VersionHandler.post')
        request = self.request.body
        created = datetime.datetime.utcnow()
        version = options.dbconn.Versions.from_json(request)
        version['created'] = created
        version.save()
        self.write(json.dumps({}, indent=2, ensure_ascii=False, cls=
            MongoFieldEncoder))
        self.finish()

    @asynchronous
    @requires_auth
    def put(self):
        log.info('ActivityHandler.put:' + self.request.body)
        body_dic = json.loads(self.request.body, encoding='utf-8')
        version_id = body_dic.get('version_id', None)
        if version_id == None:
            self.fire_response_bad_request()
            return
        del body_dic['version_id']
        if 'tiptop' in body_dic:
            os = body_dic.get('os', '')
            lookup = {'tiptop': True, 'os': os}
            update = {'$unset': {'tiptop': ''}}
            options.dbconn.Versions.find_and_modify(lookup, update, new=True)
        lookup = {'_id': ObjectId(version_id)}
        body_dic['modified'] = datetime.datetime.utcnow()
        option = {'$set': body_dic}
        version = options.dbconn.Versions.find_and_modify(lookup, option,
            new=True)
        self.finish()

    @asynchronous
    @requires_auth
    def delete(self):
        log.info('ActivityHandler.delete:' + self.request.body)
        version_id = self.get_argument('version_id', None)
        if version_id == None:
            self.fire_response_bad_request()
            return
        lookup = {'_id': ObjectId(version_id)}
        version = MongoFront.remove(lookup, 'versions')
        self.finish()

<mask token>
try:
    from tornado.curl_httpclient import CurlAsyncHTTPClient as AsyncHTTPClient
except ImportError:
    from tornado.simple_httpclient import SimpleAsyncHTTPClient as AsyncHTTPClient
<mask token>
log = get_logger()


class VersionsHandler(BaseHtHandler):
    """
    版本管理类
    """

    def __init__(self, application, request, **kwargs):
        super(VersionsHandler, self).__init__(application, request, **kwargs)

    @asynchronous
    @requires_auth
    def get(self):
        log.info('VersionHandler.get')
        where = {}
        version_id = self.get_argument('version_id', None)
        if version_id != None:
            where = {'_id': ObjectId(version_id)}
        sort = [('created', -1)]
        version = options.dbconn.Versions.find(where).sort(sort)
        response = {}
        response['entities'] = list(version) or []
        response['count'] = str(version.count())
        self.write(json.dumps(response, indent=2, ensure_ascii=False, cls=
            MongoFieldEncoder))
        self.finish()

    @asynchronous
    @requires_auth
    def post(self):
        log.info('VersionHandler.post')
        request = self.request.body
        created = datetime.datetime.utcnow()
        version = options.dbconn.Versions.from_json(request)
        version['created'] = created
        version.save()
        self.write(json.dumps({}, indent=2, ensure_ascii=False, cls=
            MongoFieldEncoder))
        self.finish()

    @asynchronous
    @requires_auth
    def put(self):
        log.info('ActivityHandler.put:' + self.request.body)
        body_dic = json.loads(self.request.body, encoding='utf-8')
        version_id = body_dic.get('version_id', None)
        if version_id == None:
            self.fire_response_bad_request()
            return
        del body_dic['version_id']
        if 'tiptop' in body_dic:
            os = body_dic.get('os', '')
            lookup = {'tiptop': True, 'os': os}
            update = {'$unset': {'tiptop': ''}}
            options.dbconn.Versions.find_and_modify(lookup, update, new=True)
        lookup = {'_id': ObjectId(version_id)}
        body_dic['modified'] = datetime.datetime.utcnow()
        option = {'$set': body_dic}
        version = options.dbconn.Versions.find_and_modify(lookup, option,
            new=True)
        self.finish()

    @asynchronous
    @requires_auth
    def delete(self):
        log.info('ActivityHandler.delete:' + self.request.body)
        version_id = self.get_argument('version_id', None)
        if version_id == None:
            self.fire_response_bad_request()
            return
        lookup = {'_id': ObjectId(version_id)}
        version = MongoFront.remove(lookup, 'versions')
        self.finish()

import tornado.web
from tornado.web import asynchronous
from tornado.options import options
import json
from bson import ObjectId
from log import get_logger
import optionsSetting
from handler_baseHt import BaseHtHandler
from mongo_field_encoder import MongoFieldEncoder
from mongolog import MongoHandler
from mongokit.helpers import totimestamp, fromtimestamp
from auth import requires_auth
from tornado.httpclient import HTTPRequest
from utils.mongo_front import MongoFront
import datetime
try:
    from tornado.curl_httpclient import CurlAsyncHTTPClient as AsyncHTTPClient
except ImportError:
    from tornado.simple_httpclient import SimpleAsyncHTTPClient as AsyncHTTPClient
from model.version import Versions
log = get_logger()


class VersionsHandler(BaseHtHandler):
    """
    版本管理类
    """

    def __init__(self, application, request, **kwargs):
        super(VersionsHandler, self).__init__(application, request, **kwargs)

    @asynchronous
    @requires_auth
    def get(self):
        log.info('VersionHandler.get')
        where = {}
        version_id = self.get_argument('version_id', None)
        if version_id != None:
            where = {'_id': ObjectId(version_id)}
        sort = [('created', -1)]
        version = options.dbconn.Versions.find(where).sort(sort)
        response = {}
        response['entities'] = list(version) or []
        response['count'] = str(version.count())
        self.write(json.dumps(response, indent=2, ensure_ascii=False, cls=
            MongoFieldEncoder))
        self.finish()

    @asynchronous
    @requires_auth
    def post(self):
        log.info('VersionHandler.post')
        request = self.request.body
        created = datetime.datetime.utcnow()
        version = options.dbconn.Versions.from_json(request)
        version['created'] = created
        version.save()
        self.write(json.dumps({}, indent=2, ensure_ascii=False, cls=
            MongoFieldEncoder))
        self.finish()

    @asynchronous
    @requires_auth
    def put(self):
        log.info('ActivityHandler.put:' + self.request.body)
        body_dic = json.loads(self.request.body, encoding='utf-8')
        version_id = body_dic.get('version_id', None)
        if version_id == None:
            self.fire_response_bad_request()
            return
        del body_dic['version_id']
        if 'tiptop' in body_dic:
            os = body_dic.get('os', '')
            lookup = {'tiptop': True, 'os': os}
            update = {'$unset': {'tiptop': ''}}
            options.dbconn.Versions.find_and_modify(lookup, update, new=True)
        lookup = {'_id': ObjectId(version_id)}
        body_dic['modified'] = datetime.datetime.utcnow()
        option = {'$set': body_dic}
        version = options.dbconn.Versions.find_and_modify(lookup, option,
            new=True)
        self.finish()

    @asynchronous
    @requires_auth
    def delete(self):
        log.info('ActivityHandler.delete:' + self.request.body)
        version_id = self.get_argument('version_id', None)
        if version_id == None:
            self.fire_response_bad_request()
            return
        lookup = {'_id': ObjectId(version_id)}
        version = MongoFront.remove(lookup, 'versions')
        self.finish()

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2015-06-09 20:53:40
# @Author  : Your Name (you@example.org)
# @Link    : http://example.org
# @Version : $Id$

import tornado.web
from tornado.web import asynchronous
from tornado.options import options
import json
from bson import ObjectId
from log import get_logger
import optionsSetting
from handler_baseHt import BaseHtHandler
from mongo_field_encoder import MongoFieldEncoder
from mongolog import MongoHandler
from mongokit.helpers import totimestamp, fromtimestamp
from auth import requires_auth
from tornado.httpclient import HTTPRequest
from utils.mongo_front import MongoFront
import datetime
try:
    from tornado.curl_httpclient import CurlAsyncHTTPClient as AsyncHTTPClient
except ImportError:
    from tornado.simple_httpclient import SimpleAsyncHTTPClient as AsyncHTTPClient
from model.version import Versions
log = get_logger()

class VersionsHandler(BaseHtHandler):
    """
    版本管理类
    """
    def __init__(self, application, request, **kwargs):
        super(VersionsHandler, self).__init__(application, request, **kwargs)

    @asynchronous
    @requires_auth
    def get(self):
        log.info('VersionHandler.get')
        where = {}
        version_id = self.get_argument("version_id",None)
        if version_id!=None:
             where = {"_id": ObjectId(version_id)}
        sort = [("created",-1)]
        version = options.dbconn.Versions.find(where).sort(sort)

        response={}
        response["entities"]=list(version) or []
        response["count"]=str(version.count())
        self.write(json.dumps(response,indent=2, ensure_ascii=False,cls=MongoFieldEncoder))
        self.finish()
        
    @asynchronous
    @requires_auth
    def post(self):
        log.info('VersionHandler.post')
        request = self.request.body
        created = datetime.datetime.utcnow()
        version = options.dbconn.Versions.from_json(request)
        version['created'] = created
      
        version.save()
      
        self.write(json.dumps({},indent=2, ensure_ascii=False,cls=MongoFieldEncoder))
        self.finish()


    @asynchronous
    @requires_auth
    def put(self):
        log.info("ActivityHandler.put:"+self.request.body)
        body_dic = json.loads(self.request.body,encoding="utf-8")
        version_id = body_dic.get("version_id",None)
        if version_id == None:
            self.fire_response_bad_request()
            return
        del body_dic["version_id"]

        if "tiptop" in body_dic:
            os = body_dic.get("os","")
            lookup = {"tiptop":True,"os":os}
            update = {"$unset":{"tiptop":""}}
            options.dbconn.Versions.find_and_modify(lookup, update,new=True)

        lookup = {"_id": ObjectId(version_id)}
        body_dic["modified"] = datetime.datetime.utcnow()
        option = {'$set': body_dic}
        
        version = options.dbconn.Versions.find_and_modify(lookup, option, new=True)
        self.finish()



    @asynchronous
    @requires_auth
    def delete(self):
        log.info("ActivityHandler.delete:"+self.request.body)
        version_id = self.get_argument("version_id",None)
        if version_id == None:
            self.fire_response_bad_request()
            return
        lookup = {"_id": ObjectId(version_id)}
        version = MongoFront.remove(lookup,"versions")
        self.finish()







