Beispiel #1
0
class Token(BaseModel):
    __tablename__ = "token"
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    token = db.Column(db.String(250))
    token1 = db.Column(db.String(250))

    @staticmethod
    async def generate_key():
        return binascii.hexlify(os.urandom(20)).decode()

    @staticmethod
    async def get_or_create(user_id):
        check = await Token.query.where(Token.user_id == user_id).gino.first()
        if check:
            return check.token
        key = await Token.generate_key()
        token = await Token.create(user_id=user_id, token=key)
        return token.token

    @staticmethod
    async def check_token(token):
        return await Token.query.where(Token.token == token).gino.first()
Beispiel #2
0
class Opportunity(db.Model):
    __tablename__ = 'opportunity'

    # table columns
    id = db.Column(db.String, primary_key=True)
    program_id = db.Column(db.Integer, db.ForeignKey('program.id'), nullable=False)
    title = db.Column(db.String(200), nullable=False)
    short_description = db.Column(db.String(2000), nullable=False)
    gdoc_id = db.Column(db.String(200))
    gdoc_link = db.Column(db.String(200), nullable=False)
    card_id = db.Column(db.String)
    program_name = db.Column(db.String)
    stage = db.Column(db.Integer, default=1)
    org_name = db.Column(db.String(255), nullable=False)
    is_active = db.Column(db.Boolean, default=True, nullable=False)

    # relationships
    applications = db.relationship('OpportunityApp', back_populates='opportunity',
                                   cascade='all, delete, delete-orphan')
    program = db.relationship('Program', back_populates='opportunities')

    @hybrid_property
    def status(self):
        return OpportunityStage(self.stage)
Beispiel #3
0
class Recipe(TimestampMixin, DB.Model):
    """ Encapsulates the business logic of a recipe in the yummy recipes
    system.
    A recipe belongs to a User
    """
    __tablename__ = 'recipes'

    id = DB.Column(DB.Integer, primary_key=True)
    user_id = DB.Column(DB.Integer, DB.ForeignKey('users.id'), nullable=False)
    title = DB.Column(DB.String, unique=True)
    description = DB.Column(DB.String)
    fulfilled = DB.Column(DB.Boolean)

    instructions = DB.relationship('Instruction', backref='recipe', lazy=True)
    ingredients = DB.relationship('Ingredient', backref='recipe', lazy=True)

    def __repr__(self):
        return "<Recipe(user_id='{}', title='{}', description='{}')>".format(
            self.user_id, self.title, self.description)

    def save(self):
        """This method persists the recipe object in the database"""
        DB.session.add(self)
        DB.session.commit()
Beispiel #4
0
class Profile(db.Model):
    __tablename__ = 'profile'

    # table columns
    id = db.Column(db.Integer, primary_key=True)
    contact_id = db.Column(db.Integer,
                           db.ForeignKey('contact.id'),
                           nullable=False)
    gender = db.Column(db.String)
    gender_other = db.Column(db.String)
    pronoun = db.Column(db.String)
    pronoun_other = db.Column(db.String)
    years_exp = db.Column(db.String)
    job_search_status = db.Column(db.String)
    current_job_status = db.Column(db.String)
    current_edu_status = db.Column(db.String)
    previous_bcorps_program = db.Column(db.String)
    needs_help_programs = db.Column(db.Boolean)
    hear_about_us = db.Column(db.String)
    hear_about_us_other = db.Column(db.String)
    value_question1 = db.Column(db.String)
    value_question2 = db.Column(db.String)

    #relationships
    race = db.relationship('Race',
                           back_populates='profile',
                           cascade='all, delete, delete-orphan',
                           uselist=False)
    address_primary = db.relationship('ContactAddress',
                                      primaryjoin=db.and_(
                                          id == ContactAddress.profile_id,
                                          ContactAddress.is_primary == True),
                                      back_populates='profile',
                                      uselist=False)
    addresses = db.relationship('ContactAddress',
                                back_populates='profile',
                                cascade='all, delete, delete-orphan')
    roles = db.relationship('RoleChoice',
                            back_populates='profile',
                            cascade='all, delete, delete-orphan',
                            uselist=False)
    programs_completed = db.relationship('ProgramsCompleted',
                                         back_populates='profile',
                                         cascade='all, delete, delete-orphan',
                                         uselist=False)
    contact = db.relationship('Contact', back_populates='profile')

    #methods
    def update(self, **update_dict):
        UPDATE_FIELDS = [
            'gender',
            'gender_other',
            'pronoun',
            'pronoun_other',
            'years_exp',
            'job_search_status',
            'current_job_status',
            'current_edu_status',
            'previous_bcorps_program',
            'needs_help_programs',
            'hear_about_us',
            'hear_about_us_other',
            'value_question1',
            'value_question2',
        ]

        address_data = update_dict.pop('address_primary', None)
        race_data = update_dict.pop('race', None)
        role_data = update_dict.pop('roles', None)
        programs_data = update_dict.pop('programs_completed', None)

        for field, value in update_dict.items():
            if field in UPDATE_FIELDS:
                setattr(self, field, value)

        if address_data:
            self.address_primary.update(**address_data)
        if race_data:
            self.race.update(**race_data)
        if role_data:
            self.roles.update(**role_data)
        if programs_data:
            self.programs_completed.update(**programs_data)
class Experience(db.Model):
    __tablename__ = 'experience'

    # table columns
    id = db.Column(db.Integer, primary_key=True)
    description = db.Column(db.String)
    host = db.Column(db.String, nullable=False)
    title = db.Column(db.String, nullable=False)
    degree_other = db.Column(db.String(100))
    degree = db.Column(db.String(100))
    link = db.Column(db.String)
    link_name = db.Column(db.String)
    start_month = db.Column(db.Enum(Month, name='MonthType'), nullable=False)
    start_year = db.Column(db.Integer, nullable=False)
    end_month = db.Column(db.Enum(Month, name='MonthType'), nullable=False)
    end_year = db.Column(db.Integer, nullable=False)
    type = db.Column(db.Enum(Type, name='ExperienceType'))
    contact_id = db.Column(db.Integer,
                           db.ForeignKey('contact.id'),
                           nullable=False)
    location = db.Column(db.String(255))

    # relationships
    contact = db.relationship('Contact')
    achievements = db.relationship('Achievement',
                                   back_populates='experience',
                                   cascade='all, delete, delete-orphan')

    skill_items = db.relationship('ExperienceSkill',
                                  cascade='all, delete, delete-orphan')

    def add_skill(self, skill):
        contact_skill = self.contact.add_skill(skill)
        exp_skill = (ExperienceSkill.query.filter_by(
            experience_id=self.id, parent_id=contact_skill.id).first())
        if not exp_skill:
            exp_skill = ExperienceSkill(contact_skill, self)
            self.skill_items.append(exp_skill)
        return exp_skill

    # calculated fields
    @hybrid_property
    def skills(self):
        skills = []
        for skill_item in self.skill_items:
            if not skill_item.deleted:
                skills.append(skill_item.skill)
        return sorted(skills, key=lambda skill: skill.name)

    @hybrid_property
    def date_end(self):
        if self.end_month == Month.none or self.end_year == 0:
            return dt.datetime.today()
        else:
            end_str = f'1 {self.end_month.value}, {self.end_year}'
            return dt.datetime.strptime(end_str, '%d %B, %Y')

    @hybrid_property
    def date_start(self):
        if self.start_month == Month.none or self.start_year == 0:
            return dt.datetime.today()
        else:
            start_str = f'1 {self.start_month.value}, {self.start_year}'
            return dt.datetime.strptime(start_str, '%d %B, %Y')

    @hybrid_property
    def date_length(self):
        end = self.date_end
        start = self.date_start
        return (end.year - start.year) * 12 + end.month - start.month

    @hybrid_property
    def length_year(self):
        return math.floor(self.date_length / 12)

    @hybrid_property
    def length_month(self):
        return self.date_length % 12

    @hybrid_property
    def is_current(self):
        if self.end_month == Month.none or self.end_year == 0:
            return True
        else:
            return False
class Profile(db.Model):
    __tablename__ = 'profile'

    # table columns
    id = db.Column(db.Integer, primary_key=True)
    contact_id = db.Column(db.Integer,
                           db.ForeignKey('contact.id'),
                           nullable=False)
    gender = db.Column(db.String)
    gender_other = db.Column(db.String)
    pronoun = db.Column(db.String)
    pronoun_other = db.Column(db.String)
    years_exp = db.Column(db.String)
    job_search_status = db.Column(db.String)
    current_job_status = db.Column(db.String)
    current_edu_status = db.Column(db.String)
    previous_bcorps_program = db.Column(db.String)
    needs_help_programs = db.Column(db.Boolean)
    hear_about_us = db.Column(db.String)
    hear_about_us_other = db.Column(db.String)
    value_question1 = db.Column(db.String)
    value_question2 = db.Column(db.String)

    #relationships
    race = db.relationship('Race',
                           back_populates='profile',
                           cascade='all, delete, delete-orphan',
                           uselist=False)
    address_primary = db.relationship('ContactAddress',
                                      primaryjoin=db.and_(
                                          id == ContactAddress.profile_id,
                                          ContactAddress.is_primary == True),
                                      back_populates='profile',
                                      uselist=False)
    addresses = db.relationship('ContactAddress',
                                back_populates='profile',
                                cascade='all, delete, delete-orphan')
    roles = db.relationship('RoleChoice',
                            back_populates='profile',
                            cascade='all, delete, delete-orphan',
                            uselist=False)
    programs_completed = db.relationship('ProgramsCompleted',
                                         back_populates='profile',
                                         cascade='all, delete, delete-orphan',
                                         uselist=False)
    contact = db.relationship('Contact', back_populates='profile')

    # calculated fields
    @hybrid_property
    def roles_list(self):
        role_dict = {
            'advocacy_public_policy': 'Advocacy and Public Policy',
            'community_engagement_outreach':
            'Community Engagement and Outreach',
            'data_analysis': 'Data Analysis',
            'fundraising_development': 'Fundraising and Development',
            'marketing_public_relations': 'Marketing and Public Relations',
            'program_management': 'Program Management',
        }
        roles_selected = [
            role_dict[k] for k, v in self.roles.__dict__.items()
            if k in role_dict.keys() and v == True
        ]
        return roles_selected

    @hybrid_property
    def candidate_info_complete(self):
        if self.address_primary:
            return (self.address_primary.street1 is not None
                    and self.address_primary.city is not None
                    and self.address_primary.state is not None
                    and self.address_primary.country is not None
                    and self.address_primary.zip_code is not None)
        else:
            return False

    @hybrid_property
    def value_alignment_complete(self):
        return (self.value_question1 is not None
                and self.value_question2 is not None)

    @hybrid_property
    def interests_and_goals_complete(self):
        return (self.years_exp is not None
                and self.job_search_status is not None
                and self.current_job_status is not None
                and self.current_edu_status is not None)

    @hybrid_property
    def programs_and_eligibility_complete(self):
        if self.contact.program_apps:
            programs = [
                p for p in self.contact.program_apps if p.is_interested
            ]
        else:
            programs = []

        return self.needs_help_programs or (len(programs) > 0)

    #methods
    def update(self, **update_dict):
        UPDATE_FIELDS = [
            'gender',
            'gender_other',
            'pronoun',
            'pronoun_other',
            'years_exp',
            'job_search_status',
            'current_job_status',
            'current_edu_status',
            'previous_bcorps_program',
            'needs_help_programs',
            'hear_about_us',
            'hear_about_us_other',
            'value_question1',
            'value_question2',
        ]

        address_data = update_dict.pop('address_primary', None)
        race_data = update_dict.pop('race', None)
        role_data = update_dict.pop('roles', None)
        programs_data = update_dict.pop('programs_completed', None)

        for field, value in update_dict.items():
            if field in UPDATE_FIELDS:
                setattr(self, field, value)

        if address_data:
            self.address_primary.update(**address_data)
        if race_data:
            self.race.update(**race_data)
        if role_data:
            self.roles.update(**role_data)
        if programs_data:
            self.programs_completed.update(**programs_data)
Beispiel #7
0
from models.base_model import db
from marshmallow import Schema, fields, post_dump, EXCLUDE

# Helper table to classify capabilities
capability_skills = db.Table('capability_skills',
    db.Column('capability_id', db.String, db.ForeignKey('capability.id')),
    db.Column('skill_id', db.String, db.ForeignKey('skill.id')),
    db.PrimaryKeyConstraint(
        'capability_id',
        'skill_id',
        name='capability_skills_pk'),
)

class Skill(db.Model):
    __tablename__ = 'skill'

    #table columns
    id = db.Column(db.String, nullable=False, primary_key=True)
    name = db.Column(db.String, nullable=False)

    #relationships
    capabilities = db.relationship('Capability', 
                                   secondary=capability_skills,
                                   lazy='subquery',
                                   back_populates='related_skills'
                                   )

    __table_args__ = (
        db.UniqueConstraint('name', name='skill_name_uniq'),
    )