示例#1
0
def main():
    parser = argparse.ArgumentParser(description='Return AP Election data')
    parser.add_argument('-d', '--file', action='store')
    parser.add_argument('--races', action='store')
    parser.add_argument('-o', '--output', action='store')
    parser.add_argument('--json', action='store_true')
    parser.add_argument('--csv', action='store_true')
    parser.add_argument('--tsv', action='store_true')

    args = parser.parse_args()

    if args.file:

        race_ids = None
        if args.races:
            race_ids = args.races.split(',')

        electiondate, races = utils.open_file(args.file, race_ids)
        payload = utils.load_results(electiondate, races)

        if args.json:
            utils.output_json(payload)

        elif args.output and args.output == 'json':
            utils.output_json(payload)

        elif args.output and args.output == 'tsv':
            utils.output_tsv(payload)

        else:
            utils.output_csv(payload)

    else:
        print """Please specify a data file with -d '/path/to/json/file.json'"""
示例#2
0
 def get(self, _id=None):
     if _id is not None:
         ret = self._get_one(_id)
     else:
         collection = get_ticket_coll()
         ret = collection.find()
     return output_json(ret, 200)
示例#3
0
    def post(self):
        ret = None
        data = request.get_json(True)
        expected = ['schema', 'data', 'status']
        for i in expected:
            if i not in data.keys():
                raise ValueError("%s not found" % i)

        # get schema
        schema_id = ObjectId(data["schema"])
        schema_coll = get_schema_coll()
        schema = schema_coll.find_one({"_id": schema_id})
        logging.debug(schema)
        if not schema:
            raise Exception("schema '%s' not found" % data['schema'])

        # check status
        if data['status'] not in schema['status_list']:
            msg = "status '%s'not found on schema '%s'. Expected: '%s'"
            msg = msg % (data['status'], schema['name'], schema['status_list'])
            raise Exception(msg)

        data['open_date'] = datetime.datetime.now()
        logging.debug(data)

        collection = get_ticket_coll()
        collection.insert_one(data)
        self.pub(data)
        return output_json({'success': True}, 200)
示例#4
0
 def get(self, _id=None):
     if _id is not None:
         ret = self._get_one(_id)
     else:
         collection = get_ticket_coll()
         ret = collection.find()
     return output_json(ret, 200)
示例#5
0
    def post(self):
        ret = None
        data = request.get_json(True)
        expected = ["schema", "data", "status"]
        for i in expected:
            if i not in data.keys():
                raise ValueError("%s not found" % i)

        # get schema
        schema_id = ObjectId(data["schema"])
        schema_coll = get_schema_coll()
        schema = schema_coll.find_one({"_id": schema_id})
        logging.debug(schema)
        if not schema:
            raise Exception("schema '%s' not found" % data["schema"])

        # check status
        if data["status"] not in schema["status_list"]:
            msg = "status '%s'not found on schema '%s'. Expected: '%s'"
            msg = msg % (data["status"], schema["name"], schema["status_list"])
            raise Exception(msg)

        data["open_date"] = datetime.datetime.now()
        logging.debug(data)

        collection = get_ticket_coll()
        collection.insert_one(data)
        self.pub(data)
        return output_json({"success": True}, 200)
示例#6
0
    def post(self):
        data = request.get_json(True)
        self.validate_post(data)
        data = self.clear_data(data)

        schema_coll = get_schema_coll()
        ret = schema_coll.insert_one(data)

        return output_json({'success': True}, 200)
示例#7
0
    def put(self, _id):
        logging.debug("Getting just one: %s " % _id)
        my_ticket = self._get_one(_id)
        old_ticket = copy.deepcopy(my_ticket)
        _id = my_ticket['_id']
        data = request.get_json(True)

        # todo :: change signal
        if 'data' in data:
            my_ticket['data'].update(data['data'])

        if 'status' in data:
            my_ticket['status'] = data['status']

        coll = get_ticket_coll()
        coll.replace_one({"_id": _id}, my_ticket)

        self.pub(my_ticket, old_ticket)
        return output_json(my_ticket, 200)
示例#8
0
    def put(self, _id):
        logging.debug("Getting just one: %s " % _id)
        my_ticket = self._get_one(_id)
        old_ticket = copy.deepcopy(my_ticket)
        _id = my_ticket["_id"]
        data = request.get_json(True)

        # todo :: change signal
        if "data" in data:
            my_ticket["data"].update(data["data"])

        if "status" in data:
            my_ticket["status"] = data["status"]

        coll = get_ticket_coll()
        coll.replace_one({"_id": _id}, my_ticket)

        self.pub(my_ticket, old_ticket)
        return output_json(my_ticket, 200)
示例#9
0
 def get(self, _id=None):
     if _id is not None:
         return self._get_one(_id)
     schema_coll = get_schema_coll()
     ret = schema_coll.find()
     return output_json(ret, 200)
示例#10
0
 def _get_one(self, _id):
     logging.debug("Getting just one: %s " % _id)
     _id = ObjectId(_id)
     coll = get_schema_coll()
     ret = coll.find_one({"_id": _id})
     return output_json(ret, 200)