<s> import yaml <EOL> from datetime import datetime <EOL> from uuid import uuid4 <EOL> from collections import defaultdict <EOL> from sqlalchemy import Column , String , DateTime , ForeignKey , Integer , Text <EOL> from sqlalchemy . ext . associationproxy import association_proxy <EOL> from sqlalchemy . orm import relationship , joinedload <EOL> from sqlalchemy . schema import UniqueConstraint <EOL> from changes . config import db <EOL> from changes . constants import ProjectStatus <EOL> from changes . db . types . guid import GUID <EOL> from changes . db . types . enum import Enum <EOL> from changes . utils . slugs import slugify <EOL> class ProjectConfigError ( Exception ) : <EOL> pass <EOL> class Project ( db . Model ) : <EOL> """<STR_LIT>""" <EOL> __tablename__ = '<STR_LIT>' <EOL> id = Column ( GUID , primary_key = True , default = uuid4 ) <EOL> slug = Column ( String ( <NUM_LIT:64> ) , unique = True , nullable = False ) <EOL> repository_id = Column ( GUID , ForeignKey ( '<STR_LIT>' , ondelete = "<STR_LIT>" ) , nullable = False ) <EOL> name = Column ( String ( <NUM_LIT:64> ) ) <EOL> date_created = Column ( DateTime , default = datetime . utcnow ) <EOL> avg_build_time = Column ( Integer ) <EOL> status = Column ( Enum ( ProjectStatus ) , default = ProjectStatus . active , <EOL> server_default = '<STR_LIT:1>' ) <EOL> repository = relationship ( '<STR_LIT>' ) <EOL> plans = association_proxy ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( Project , self ) . __init__ ( ** kwargs ) <EOL> if not self . id : <EOL> self . id = uuid4 ( ) <EOL> if not self . slug : <EOL> self . slug = slugify ( self . name ) <EOL> @ classmethod <EOL> def get ( cls , id ) : <EOL> project = cls . query . options ( <EOL> joinedload ( cls . repository , innerjoin = True ) , <EOL> ) . filter_by ( slug = id ) . first ( ) <EOL> if project is None and len ( id ) == <NUM_LIT:32> : <EOL> project = cls . query . options ( <EOL> joinedload ( cls . repository ) , <EOL> ) . get ( id ) <EOL> return project <EOL> _default_config = { <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> def get_config_path ( self ) : <EOL> return '<STR_LIT>' . format ( self . slug ) <EOL> def get_config ( self , revision_sha , diff = None , config_path = None ) : <EOL> '''<STR_LIT>''' <EOL> from changes . vcs . base import CommandError , ContentReadError <EOL> if config_path is None : <EOL> config_path = self . get_config_path ( ) <EOL> vcs = self . repository . get_vcs ( ) <EOL> if vcs is None : <EOL> raise NotImplementedError <EOL> else : <EOL> try : <EOL> config_content = vcs . read_file ( <EOL> revision_sha , config_path , diff = diff ) <EOL> except ( CommandError , ContentReadError ) : <EOL> config_content = '<STR_LIT:{}>' <EOL> try : <EOL> config = yaml . safe_load ( config_content ) <EOL> if not isinstance ( config , dict ) : <EOL> raise ProjectConfigError ( <EOL> '<STR_LIT>' . format ( config_path ) ) <EOL> except yaml . YAMLError : <EOL> raise ProjectConfigError ( <EOL> '<STR_LIT>' . format ( config_path ) ) <EOL> for k , v in self . _default_config . iteritems ( ) : <EOL> config . setdefault ( k , v ) <EOL> return config <EOL> class ProjectOption ( db . Model ) : <EOL> """<STR_LIT>""" <EOL> __tablename__ = '<STR_LIT>' <EOL> __table_args__ = ( <EOL> UniqueConstraint ( '<STR_LIT>' , '<STR_LIT:name>' , name = '<STR_LIT>' ) , <EOL> ) <EOL> id = Column ( GUID , primary_key = True , default = uuid4 ) <EOL> project_id = Column ( GUID , ForeignKey ( '<STR_LIT>' , ondelete = "<STR_LIT>" ) , nullable = False ) <EOL> name = Column ( String ( <NUM_LIT:64> ) , nullable = False ) <EOL> value = Column ( Text , nullable = False ) <EOL> date_created = Column ( DateTime , default = datetime . utcnow , nullable = False ) <EOL> project = relationship ( '<STR_LIT>' ) <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( ProjectOption , 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> class ProjectOptionsHelper : <EOL> @ staticmethod <EOL> def get_options ( project_list , options_list ) : <EOL> options_query = db . session . query ( <EOL> ProjectOption . project_id , ProjectOption . name , ProjectOption . value <EOL> ) . filter ( <EOL> ProjectOption . project_id . in_ ( p . id for p in project_list ) , <EOL> ProjectOption . name . in_ ( options_list ) <EOL> ) <EOL> options = defaultdict ( dict ) <EOL> for project_id , option_name , option_value in options_query : <EOL> options [ project_id ] [ option_name ] = option_value <EOL> return options </s>
