# Path: invenio_files_rest/models.py
# class Location(db.Model, Timestamp):
#     """Model defining base locations."""
# 
#     __tablename__ = 'files_location'
# 
#     id = db.Column(db.Integer, primary_key=True)
#     """Internal identifier for locations.
# 
#     The internal identifier is used only used as foreign key for buckets in
#     order to decrease storage requirements per row for buckets.
#     """
# 
#     name = db.Column(db.String(20), unique=True, nullable=False)
#     """External identifier of the location."""
# 
#     uri = db.Column(db.String(255), nullable=False)
#     """URI of the location."""
# 
#     default = db.Column(db.Boolean(name='default'),
#                         nullable=False,
#                         default=False)
#     """True if the location is the default location.
# 
#     At least one location should be the default location.
#     """
# 
#     @validates('name')
#     def validate_name(self, key, name):
#         """Validate name."""
#         if not slug_pattern.match(name) or len(name) > 20:
#             raise ValueError(
#                 'Invalid location name (lower-case alphanumeric + dashes).')
#         return name
# 
#     @classmethod
#     def get_by_name(cls, name):
#         """Fetch a specific location object."""
#         return cls.query.filter_by(
#             name=name,
#         ).one_or_none()
# 
#     @classmethod
#     def get_default(cls):
#         """Fetch the default location object."""
#         try:
#             return cls.query.filter_by(default=True).one_or_none()
#         except MultipleResultsFound:
#             return None
# 
#     @classmethod
#     def all(cls):
#         """Return query that fetches all locations."""
#         return Location.query.all()
# 
#     def __repr__(self):
#         """Return representation of location."""
#         return self.name


import click
from click_default_group import DefaultGroup
from flask.cli import with_appcontext
from invenio_db import db
from .models import Location
    from .models import Bucket
    from .helpers import populate_from_path
    from .models import Bucket

    else:
        if default:
            _unset_default_location()

        location = Location(name=name, uri=uri, default=default)
        db.session.add(location)
        db.session.commit()
        click.secho('Location {} {} as default {} stored in database'.format(
            location.name, location.uri, str(location.default)), fg='green')


@location.command()
@with_appcontext
def list():
    """Return the list of locations."""
    locations = Location.all()
    for location in locations:
        click.secho('{} {} as default {}'.format(
            location.name, location.uri, str(location.default)), fg='green')


@location.command()
@click.argument('name')
@with_appcontext
def set_default(name):
    """Set default a location as default. The location must already exist.

    If another location was marked as default it unsets it.
    """

# Path: invenio_files_rest/models.py
# class Location(db.Model, Timestamp):
#     """Model defining base locations."""
# 
#     __tablename__ = 'files_location'
# 
#     id = db.Column(db.Integer, primary_key=True)
#     """Internal identifier for locations.
# 
#     The internal identifier is used only used as foreign key for buckets in
#     order to decrease storage requirements per row for buckets.
#     """
# 
#     name = db.Column(db.String(20), unique=True, nullable=False)
#     """External identifier of the location."""
# 
#     uri = db.Column(db.String(255), nullable=False)
#     """URI of the location."""
# 
#     default = db.Column(db.Boolean(name='default'),
#                         nullable=False,
#                         default=False)
#     """True if the location is the default location.
# 
#     At least one location should be the default location.
#     """
# 
#     @validates('name')
#     def validate_name(self, key, name):
#         """Validate name."""
#         if not slug_pattern.match(name) or len(name) > 20:
#             raise ValueError(
#                 'Invalid location name (lower-case alphanumeric + dashes).')
#         return name
# 
#     @classmethod
#     def get_by_name(cls, name):
#         """Fetch a specific location object."""
#         return cls.query.filter_by(
#             name=name,
#         ).one_or_none()
# 
#     @classmethod
#     def get_default(cls):
#         """Fetch the default location object."""
#         try:
#             return cls.query.filter_by(default=True).one_or_none()
#         except MultipleResultsFound:
#             return None
# 
#     @classmethod
#     def all(cls):
#         """Return query that fetches all locations."""
#         return Location.query.all()
# 
#     def __repr__(self):
#         """Return representation of location."""
#         return self.name


# Path: invenio_files_rest/cli.py
import click
from click_default_group import DefaultGroup
from flask.cli import with_appcontext
from invenio_db import db
from .models import Location
    from .models import Bucket
    from .helpers import populate_from_path
    from .models import Bucket


    else:
        if default:
            _unset_default_location()

        location = Location(name=name, uri=uri, default=default)
        db.session.add(location)
        db.session.commit()
        click.secho('Location {} {} as default {} stored in database'.format(
            location.name, location.uri, str(location.default)), fg='green')


@location.command()
@with_appcontext
def list():
    """Return the list of locations."""
    locations = Location.all()
    for location in locations:
        click.secho('{} {} as default {}'.format(
            location.name, location.uri, str(location.default)), fg='green')


@location.command()
@click.argument('name')
@with_appcontext
def set_default(name):
    """Set default a location as default. The location must already exist.

    If another location was marked as default it unsets it.
    """

    location = Location.get_by_name(name)