<s> from datetime import datetime <EOL> from sqlalchemy import Column , DateTime , ForeignKey , String <EOL> from sqlalchemy . orm import relationship <EOL> from sqlalchemy . schema import UniqueConstraint , ForeignKeyConstraint <EOL> from uuid import uuid4 <EOL> from changes . config import db <EOL> from changes . db . types . guid import GUID <EOL> from changes . db . types . json import JSONEncodedDict <EOL> class Source ( db . Model ) : <EOL> """<STR_LIT>""" <EOL> id = Column ( GUID , primary_key = True , default = uuid4 ) <EOL> repository_id = Column ( GUID , ForeignKey ( '<STR_LIT>' ) , nullable = False ) <EOL> patch_id = Column ( GUID , ForeignKey ( '<STR_LIT>' ) , unique = True ) <EOL> revision_sha = Column ( String ( <NUM_LIT> ) ) <EOL> date_created = Column ( DateTime , default = datetime . utcnow ) <EOL> data = Column ( JSONEncodedDict ) <EOL> repository = relationship ( '<STR_LIT>' , innerjoin = False ) <EOL> patch = relationship ( '<STR_LIT>' ) <EOL> revision = relationship ( '<STR_LIT>' , <EOL> foreign_keys = [ repository_id , revision_sha ] ) <EOL> __tablename__ = '<STR_LIT:source>' <EOL> __table_args__ = ( <EOL> ForeignKeyConstraint ( <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> ) , <EOL> UniqueConstraint ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , name = '<STR_LIT>' , <EOL> ) , <EOL> ) <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( Source , self ) . __init__ ( ** kwargs ) <EOL> if self . id is None : <EOL> self . id = uuid4 ( ) <EOL> if self . date_created is None : <EOL> self . date_created = datetime . utcnow ( ) <EOL> def generate_diff ( self ) : <EOL> diff = None <EOL> if self . patch : <EOL> diff = self . patch . diff <EOL> else : <EOL> vcs = self . repository . get_vcs ( ) <EOL> if vcs : <EOL> try : <EOL> diff = vcs . export ( self . revision_sha ) <EOL> except Exception : <EOL> pass <EOL> if isinstance ( diff , bytes ) : <EOL> diff = diff . decode ( '<STR_LIT:utf-8>' ) <EOL> return diff <EOL> def is_commit ( self ) : <EOL> return bool ( self . patch_id is None and self . revision_sha ) </s>
