# Path: extrapypi/models/package.py
# class Package(db.Model):
#     """Represent a simple package
#     """
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(255, convert_unicode=True), nullable=False, unique=True)
#     summary = db.Column(db.String(255, convert_unicode=True))
#     created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)
#     updated_at = db.Column(db.DateTime, onupdate=datetime.datetime.utcnow)
# 
#     maintainers = db.relationship('User', secondary=package_maintainers, lazy='dynamic', backref='packages')
# 
#     @property
#     def sorted_releases(self):
#         releases = self.releases.all()
#         return sorted(
#             releases,
#             key=lambda r: parse_version(r.version),
#             reverse=True
#         )
# 
#     @property
#     def latest_release(self):
#         return next(iter(self.sorted_releases), None)
# 
#     def __repr__(self):
#         return "<Package {0.name}>".format(self)
# 
# Path: extrapypi/commons/permissions.py

# 
# Path: extrapypi/commons/packages.py
# def create_release(data, config, files):
#     """Register and save a new release
# 
#     Since pypi itself don't support pre-registration anymore, we don't
# 
#     .. note::
# 
#         Installers cannot create a new release
# 
#     If a release with same version number and package exists, we return it
#     :param dict data: request data for registering package
#     :param dict config: current app config
#     :raises: PermissionDenied
#     """
#     maintainer_permission.test()
# 
#     store = get_store(config.get('STORAGE'), config.get('STORAGE_PARAMS'))
# 
#     try:
#         package = Package.query.filter_by(name=data['name']).one()
#     except NoResultFound:
#         package = create_package(data['name'], data['summary'], store)
# 
#     if current_user not in package.maintainers:
#         package.maintainers.append(current_user)
# 
#     release = Release.query.filter_by(version=data['version'], package=package)\
#                            .first()
#     if release is None:
#         release = Release(
#             description=data.get('description', 'UNKNOWN'),
#             download_url=data.get('download_url', 'UNKNOW'),
#             home_page=data.get('home_page', 'UNKNOWN'),
#             version=data['version'],
#             keywords=data.get('keywords'),
#             package=package
#         )
# 
#     try:
#         for name, f in six.iteritems(files):
#             store.create_release(package, f)
#         db.session.add(release)
#         db.session.commit()
#     except Exception:
#         db.session.rollback()
#         raise
#     return release
# 
# def get_store(name, params):
#     """Utility function to get correct storage class based
#     on its name
# 
#     :param str name: name of the storage
#     :param dict params: storage params from application config
#     :return: Correct storage class instance, passing params to constructor
#     :rtype: BaseStorage
#     :raises: AttributeError
#     """
#     storage_classes = [getattr(storage, c) for c in storage.__all__]
#     storage_classes = list(filter(lambda x: issubclass(x, BaseStorage),
#                                   storage_classes))
# 
#     store = next((c for c in storage_classes if c.NAME == name), None)
#     if store is None:
#         log.error("Storage {} does not exists".format(name))
#         raise AttributeError("Storage {} does not exists".format(name))
#     return store(**params)


import logging
from flask_login import login_required
from sqlalchemy.orm.exc import NoResultFound
from flask_principal import PermissionDenied
from flask import (
    request,
    render_template,
    Blueprint,
    abort,
    send_file,
    current_app
)
from extrapypi.models import Package
from extrapypi.commons.permissions import installer_permission
from extrapypi.commons.packages import create_release, get_store
            log.exception("Cannot upload release")
            abort(400)

    packages = Package.query.all()
    return render_template('simple/simple.html', packages=packages)


@blueprint.route('/<string:package>/', methods=['GET'])
@login_required
@installer_permission.require()
def package_view(package):
    """List all files avaible for a package
    """
    try:
        package_obj = Package.query.filter_by(name=package).one()
    except NoResultFound:
        abort(404, "package %s does not exists" % package)

    store = get_store(
        current_app.config['STORAGE'],
        current_app.config['STORAGE_PARAMS']
    )

    files = store.get_files(package_obj)
    return render_template("simple/package.html",
                           files=files, package=package_obj)


@blueprint.route('/<string:package>/<path:source>', methods=['GET'])
@login_required

# Path: extrapypi/models/package.py
# class Package(db.Model):
#     """Represent a simple package
#     """
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(255, convert_unicode=True), nullable=False, unique=True)
#     summary = db.Column(db.String(255, convert_unicode=True))
#     created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)
#     updated_at = db.Column(db.DateTime, onupdate=datetime.datetime.utcnow)
# 
#     maintainers = db.relationship('User', secondary=package_maintainers, lazy='dynamic', backref='packages')
# 
#     @property
#     def sorted_releases(self):
#         releases = self.releases.all()
#         return sorted(
#             releases,
#             key=lambda r: parse_version(r.version),
#             reverse=True
#         )
# 
#     @property
#     def latest_release(self):
#         return next(iter(self.sorted_releases), None)
# 
#     def __repr__(self):
#         return "<Package {0.name}>".format(self)
# 
# Path: extrapypi/commons/permissions.py

# 
# Path: extrapypi/commons/packages.py
# def create_release(data, config, files):
#     """Register and save a new release
# 
#     Since pypi itself don't support pre-registration anymore, we don't
# 
#     .. note::
# 
#         Installers cannot create a new release
# 
#     If a release with same version number and package exists, we return it
#     :param dict data: request data for registering package
#     :param dict config: current app config
#     :raises: PermissionDenied
#     """
#     maintainer_permission.test()
# 
#     store = get_store(config.get('STORAGE'), config.get('STORAGE_PARAMS'))
# 
#     try:
#         package = Package.query.filter_by(name=data['name']).one()
#     except NoResultFound:
#         package = create_package(data['name'], data['summary'], store)
# 
#     if current_user not in package.maintainers:
#         package.maintainers.append(current_user)
# 
#     release = Release.query.filter_by(version=data['version'], package=package)\
#                            .first()
#     if release is None:
#         release = Release(
#             description=data.get('description', 'UNKNOWN'),
#             download_url=data.get('download_url', 'UNKNOW'),
#             home_page=data.get('home_page', 'UNKNOWN'),
#             version=data['version'],
#             keywords=data.get('keywords'),
#             package=package
#         )
# 
#     try:
#         for name, f in six.iteritems(files):
#             store.create_release(package, f)
#         db.session.add(release)
#         db.session.commit()
#     except Exception:
#         db.session.rollback()
#         raise
#     return release
# 
# def get_store(name, params):
#     """Utility function to get correct storage class based
#     on its name
# 
#     :param str name: name of the storage
#     :param dict params: storage params from application config
#     :return: Correct storage class instance, passing params to constructor
#     :rtype: BaseStorage
#     :raises: AttributeError
#     """
#     storage_classes = [getattr(storage, c) for c in storage.__all__]
#     storage_classes = list(filter(lambda x: issubclass(x, BaseStorage),
#                                   storage_classes))
# 
#     store = next((c for c in storage_classes if c.NAME == name), None)
#     if store is None:
#         log.error("Storage {} does not exists".format(name))
#         raise AttributeError("Storage {} does not exists".format(name))
#     return store(**params)


# Path: extrapypi/simple/views.py
import logging
from flask_login import login_required
from sqlalchemy.orm.exc import NoResultFound
from flask_principal import PermissionDenied
from flask import (
    request,
    render_template,
    Blueprint,
    abort,
    send_file,
    current_app
)
from extrapypi.models import Package
from extrapypi.commons.permissions import installer_permission
from extrapypi.commons.packages import create_release, get_store

            log.exception("Cannot upload release")
            abort(400)

    packages = Package.query.all()
    return render_template('simple/simple.html', packages=packages)


@blueprint.route('/<string:package>/', methods=['GET'])
@login_required
@installer_permission.require()
def package_view(package):
    """List all files avaible for a package
    """
    try:
        package_obj = Package.query.filter_by(name=package).one()
    except NoResultFound:
        abort(404, "package %s does not exists" % package)

    store = get_store(
        current_app.config['STORAGE'],
        current_app.config['STORAGE_PARAMS']
    )

    files = store.get_files(package_obj)
    return render_template("simple/package.html",
                           files=files, package=package_obj)


@blueprint.route('/<string:package>/<path:source>', methods=['GET'])
@login_required

@installer_permission.require()