# Path: app/models/user.py
# class User(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     type = db.Column(db.String(32), nullable=False, default='User')
#     username = db.Column(db.String(80), unique=True)
#     email = db.Column(db.String(120), unique=True)
#     name = db.Column(db.String(80))
#     bio = db.Column(db.String(180))
#     points = db.Column(db.Integer, default=0)
#     site_admin = db.Column(db.Boolean, default=False)
#     avatar_url = db.Column(db.String(256))
#     owner = db.Column(db.String(32), nullable=False, default='user')
#     github_id = db.Column(db.Integer, unique=True)
#     github_username = db.Column(db.String(64), unique=True)
#     github_token = db.Column(db.String(300), unique=True)
#     password = db.Column(db.String(300))
#     location = db.Column(db.String(255))
#     followers_count = db.Column(db.Integer, default=0)
#     public_repos_count = db.Column(db.Integer, default=0)
#     created_at = db.Column(db.DateTime, default=datetime.utcnow)
#     last_active_at = db.Column(db.DateTime, default=datetime.utcnow)
#     last_login_reward_at = db.Column(db.DateTime, default=datetime.utcnow)
#     following_topics = db.relationship('Topic', secondary=topics,
#         backref=db.backref('user', lazy='dynamic'))
# 
#     def __init__(self, username, email, password, name=None):
#         self.email = email
#         self.username = username
#         self.password = password
#         if name is None:
#             self.name = username
#         else:
#             self.name = name
# 
#     is_authenticated = True
#     is_anonymous =  False
#     is_active = True
# 
#     def get_id(self):
#         return unicode(self.id)
# 
#     def __repr__(self):
#         return '<User %r>' % self.username
# 
# Path: app/models/repository.py
# class Repository(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     origin_id = db.Column(db.Integer, nullable=False)
#     name = db.Column(db.String(128), nullable=False)
#     description = db.Column(db.String(1024))
#     logo_url = db.Column(db.String(256))
#     html_url = db.Column(db.String(256))
#     homepage = db.Column(db.String(256))
#     language = db.Column(db.String(32))
#     last_snippet_number = db.Column(db.Integer, nullable=False, default=0)
#     pushed_at = db.Column(db.DateTime)
#     created_at = db.Column(db.DateTime)
#     updated_at = db.Column(db.DateTime)
#     imported_at = db.Column(db.DateTime, default=datetime.utcnow)
#     imported_from = db.Column(db.String(16))
#     owner_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
#     owner = db.relationship('User', uselist=False)
#     topics = db.relationship('Topic', secondary=topics,
#         backref=db.backref('repository', lazy='dynamic'))
# 
#     def __init__(self, owner, data):
#         self.name = data['name']
#         self.origin_id = data['id']
#         self.homepage = data['homepage']
#         self.language = data['language']
#         self.html_url = data['html_url']
#         self.description = data['description']
#         self.created_at = datetime_from_utc(data['created_at'])
#         self.updated_at = datetime_from_utc(data['updated_at'])
#         self.pushed_at = datetime_from_utc(data['pushed_at'])
#         self.owner_id = owner.id
#         self.owner = owner
#         language = data['language']
#         has_language_topic = False
#         for name in data['topics']: 
#             topic = Topic.query.filter_by(name=name).first()
#             if topic is None:
#                 topic = Topic(name)
#                 if language and name == language:
#                     topic.group = 'language'
#                     has_language_topic = True
#                 topic.repositories_count = 1
#             self.topics.append(topic)
#         if language and not has_language_topic:
#             topic = Topic.query.filter_by(name=language).first()
#             if topic is None:
#                 topic = Topic(language, 'language')
#                 topic.repositories_count = 1
#             self.topics.append(topic)
# 
#     def __repr__(self):
#         return '<Repository %r>' % self.name


from app.models.user import User
from app.models.repository import Repository

def get(username, name):
    user = User.query.filter_by(username=username).first()
    if user is None:
        return None
    repo = Repository.query.filter_by(owner_id=user.id, name=name).first()
    if repo is None:
        return None
    return repo

def get_by_origin_id(origin_id, imported_from='GitHub'):

# Path: app/models/user.py
# class User(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     type = db.Column(db.String(32), nullable=False, default='User')
#     username = db.Column(db.String(80), unique=True)
#     email = db.Column(db.String(120), unique=True)
#     name = db.Column(db.String(80))
#     bio = db.Column(db.String(180))
#     points = db.Column(db.Integer, default=0)
#     site_admin = db.Column(db.Boolean, default=False)
#     avatar_url = db.Column(db.String(256))
#     owner = db.Column(db.String(32), nullable=False, default='user')
#     github_id = db.Column(db.Integer, unique=True)
#     github_username = db.Column(db.String(64), unique=True)
#     github_token = db.Column(db.String(300), unique=True)
#     password = db.Column(db.String(300))
#     location = db.Column(db.String(255))
#     followers_count = db.Column(db.Integer, default=0)
#     public_repos_count = db.Column(db.Integer, default=0)
#     created_at = db.Column(db.DateTime, default=datetime.utcnow)
#     last_active_at = db.Column(db.DateTime, default=datetime.utcnow)
#     last_login_reward_at = db.Column(db.DateTime, default=datetime.utcnow)
#     following_topics = db.relationship('Topic', secondary=topics,
#         backref=db.backref('user', lazy='dynamic'))
# 
#     def __init__(self, username, email, password, name=None):
#         self.email = email
#         self.username = username
#         self.password = password
#         if name is None:
#             self.name = username
#         else:
#             self.name = name
# 
#     is_authenticated = True
#     is_anonymous =  False
#     is_active = True
# 
#     def get_id(self):
#         return unicode(self.id)
# 
#     def __repr__(self):
#         return '<User %r>' % self.username
# 
# Path: app/models/repository.py
# class Repository(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     origin_id = db.Column(db.Integer, nullable=False)
#     name = db.Column(db.String(128), nullable=False)
#     description = db.Column(db.String(1024))
#     logo_url = db.Column(db.String(256))
#     html_url = db.Column(db.String(256))
#     homepage = db.Column(db.String(256))
#     language = db.Column(db.String(32))
#     last_snippet_number = db.Column(db.Integer, nullable=False, default=0)
#     pushed_at = db.Column(db.DateTime)
#     created_at = db.Column(db.DateTime)
#     updated_at = db.Column(db.DateTime)
#     imported_at = db.Column(db.DateTime, default=datetime.utcnow)
#     imported_from = db.Column(db.String(16))
#     owner_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
#     owner = db.relationship('User', uselist=False)
#     topics = db.relationship('Topic', secondary=topics,
#         backref=db.backref('repository', lazy='dynamic'))
# 
#     def __init__(self, owner, data):
#         self.name = data['name']
#         self.origin_id = data['id']
#         self.homepage = data['homepage']
#         self.language = data['language']
#         self.html_url = data['html_url']
#         self.description = data['description']
#         self.created_at = datetime_from_utc(data['created_at'])
#         self.updated_at = datetime_from_utc(data['updated_at'])
#         self.pushed_at = datetime_from_utc(data['pushed_at'])
#         self.owner_id = owner.id
#         self.owner = owner
#         language = data['language']
#         has_language_topic = False
#         for name in data['topics']: 
#             topic = Topic.query.filter_by(name=name).first()
#             if topic is None:
#                 topic = Topic(name)
#                 if language and name == language:
#                     topic.group = 'language'
#                     has_language_topic = True
#                 topic.repositories_count = 1
#             self.topics.append(topic)
#         if language and not has_language_topic:
#             topic = Topic.query.filter_by(name=language).first()
#             if topic is None:
#                 topic = Topic(language, 'language')
#                 topic.repositories_count = 1
#             self.topics.append(topic)
# 
#     def __repr__(self):
#         return '<Repository %r>' % self.name


# Path: app/services/repositories_service.py
from app.models.user import User
from app.models.repository import Repository


def get(username, name):
    user = User.query.filter_by(username=username).first()
    if user is None:
        return None
    repo = Repository.query.filter_by(owner_id=user.id, name=name).first()
    if repo is None:
        return None
    return repo

def get_by_origin_id(origin_id, imported_from='GitHub'):

    return Repository.query.filter_by(