Example #1
0
File: lib.py Project: benscott/fuse
def list_resources(app):
    """
    Iterator; list of all resources
    :param app:
    :return:
    """

    # Dictionary of all resources to be added
    resources = {
        # FIXME: Update <str:id> to ensure mongo GUID
        '/{slug}/<string:identifier>/': RecordAPIResource,
        '/{slug}/': ListAPIResource,
        '/{slug}/transcription': CustomAPIResource,
        '/{slug}/transcription/<string:identifier>/': CustomAPIResource,
        '/{slug}.schema.json': SchemaAPIResource
    }

    # Get all slugs from json schema files - use the config json schema
    # files as the schema source of truth - if a config file is deleted
    # it can then persist in the mongo collection for data versioning
    # But will not be available via the API
    for slug in Schema().list_files(app.config['SCHEMA_DIR']).keys():
        for endpoint, resource in resources.items():
            slugged_endpoint = endpoint.format(slug=slug)
            yield slug, slugged_endpoint, resource
Example #2
0
    def get(self):
        """
        List records
        :return:
         ---
         description: List {slug} records
         ---
        """
        pagination = pagination_parser()
        # Convert page and limit to skip and limit
        query_args = dict(limit=pagination['limit'])
        # If page is greater than 1, then skip x records
        if pagination['page'] > 1:
            query_args['skip'] = (pagination['page'] - 1) * pagination['limit']

        # Query MongoDB - gets result cursor
        cursor = Collection(self.slug).find(**query_args)

        # Return JSON dict of records and total
        # Explicity pass through jsonify so it uses custom JSONEncoder
        return jsonify({
            'total': cursor.count(),
            'schema': Schema().load(self.slug),
            'records': list(cursor)
        })
Example #3
0
 def __init__(self, slug):
     """
     Load the schema for the record
     :param slug: Schema slog
     """
     self.slug = slug
     self.schema = Schema().load(self.slug)
     self.collection = Collection(slug)
Example #4
0
 def get(self, identifier):
     """
     :param identifier:
     :return:
      ---
      description: Returns a {slug} record
      ---
     """
     return jsonify({
         'schema': Schema().load(self.slug),
         'record': self.doc.read(identifier)
     })
Example #5
0
    def get(self):
        """
        Get one record that has not yet been transcribed
        :return:
         ---
         description: List {slug} records
         ---
        """

        # Prevent multiple people working on same doc
        limit = 10
        skip = random.randint(1, limit)

        # Query MongoDB - gets result cursor
        record = Collection(self.slug).find().sort('transcription_count', pymongo.DESCENDING).limit(limit).skip(skip)

        # Return JSON dict of records and total
        # Explicity pass through jsonify so it uses custom JSONEncoder
        return jsonify({
            'total': 1 if record else 0,
            'schema': Schema().load(self.slug),
            'record': record[0]
        })
Example #6
0
 def get():
     schemas = Schema().load_all()
     return jsonify(schemas)
Example #7
0
 def get(self):
     schema = Schema().load(self.slug)
     return jsonify(schema)
Example #8
0
 def before_first_request():
     # On start up, parse, validate and load schemas
     app.logger.info('Before first request: initiating schema')
     Schema().build()