Exemplo n.º 1
0
class EventEntity(db.Model, CRUDMixin):

    """ Stores a list of redcap events """
    __tablename__ = 'Event'

    id = db.Column("evtID", db.Integer, primary_key=True)
    redcap_arm = db.Column("evtRedcapArm", db.String(255), nullable=False)
    redcap_event = db.Column("evtRedcapEvent", db.String(255), nullable=False)
    day_offset = db.Column("evtDayOffset", db.Float, nullable=False,
                           server_default='0')
    added_at = db.Column("evtAddedAt", db.DateTime(), nullable=False,
                         server_default='0000-00-00 00:00:00')

    def get_unique_event_name(self):
        """ Helper for generating the unique string `event_arm` """
        uniq = "{}_{}".format(self.redcap_event, self.redcap_arm)
        return uniq.lower().replace(" ", "_")

    def __repr__(self):
        return """<EventEntity (evtID: {0.id},
        evtRedcapArm: {0.redcap_arm}, evtRedcapEvent: {0.redcap_event},
        evtDayOffset: {0.day_offset}, evtAddedAt:{0.added_at})>""".format(self)

    def serialize(self):
        """Return object data for jsonification """

        return {
            'id': self.id,
            'redcap_arm': self.redcap_arm,
            'redcap_event': self.redcap_event,
            'day_offset': self.day_offset,
            'unique_event_name': self.get_unique_event_name(),
            'added_at': utils.localize_est_date(self.added_at),
        }
class EventEntity(db.Model, CRUDMixin):
    """ Stores a list of redcap events """
    __tablename__ = 'Event'

    id = db.Column("evtID", db.Integer, primary_key=True)
    redcap_arm = db.Column("evtRedcapArm", db.String(255), nullable=False)
    redcap_event = db.Column("evtRedcapEvent", db.String(255), nullable=False)
    added_at = db.Column("evtAddedAt",
                         db.DateTime(),
                         nullable=False,
                         server_default='0000-00-00 00:00:00')

    def get_unique_event_name(self):
        """ Helper for generating the unique string `event_arm` """
        uniq = "{}_{}".format(self.redcap_event, self.redcap_arm)
        return uniq.lower().replace(" ", "_")

    def serialize(self):
        """Return object data for jsonification """

        return {
            'id': self.id,
            'redcap_arm': self.redcap_arm,
            'redcap_event': self.redcap_event,
            'unique_event_name': self.get_unique_event_name(),
            'added_at': dump_datetime(self.added_at),
        }
Exemplo n.º 3
0
class RoleEntity(db.Model, CRUDMixin):  # RoleMixin
    """ Stores possible user roles """
    __tablename__ = 'Role'

    id = db.Column("rolID", db.Integer, primary_key=True)
    name = db.Column("rolName", db.String(255), nullable=False, unique=True)
    description = db.Column("rolDescription", db.String(255), nullable=False)

    def __repr__(self):
        """ implements friendly representation """
        return "<RoleEntity (rolID: {0.id}, rolName: {0.name})>" \
            .format(self)
Exemplo n.º 4
0
class SubjectFileEntity(db.Model, CRUDMixin):

    """ Stores the file metadata """
    __tablename__ = 'SubjectFile'

    id = db.Column("sfID", db.Integer, primary_key=True)
    subject_id = db.Column("sbjID", db.Integer, db.ForeignKey('Subject.sbjID'),
                           nullable=False)
    event_id = db.Column("evtID", db.Integer, db.ForeignKey('Event.evtID'),
                         nullable=False)
    file_name = db.Column("sfFileName", db.String(255), nullable=False)
    file_check_sum = db.Column("sfFileCheckSum", db.String(32), nullable=False)
    file_size = db.Column("sfFileSize", db.String(255), nullable=False)
    uploaded_at = db.Column("sfUploadedAt", db.DateTime(), nullable=False,
                            server_default='0000-00-00 00:00:00')
    user_id = db.Column("usrID", db.Integer, db.ForeignKey('User.usrID'),
                        nullable=False)

    # @OneToOne
    subject = db.relationship('SubjectEntity', uselist=False, lazy='joined')
    event = db.relationship('EventEntity', uselist=False, lazy='joined')
    user = db.relationship('UserEntity', uselist=False, lazy='joined')

    def get_full_path(self, prefix):
        """
        Build the full path using the database info and the prefix
        @TODO: implement the naming convention
        """
        return os.path.join(prefix, self.file_name)

    def __repr__(self):
        return "<SubjectFileEntity (sfID: {0.id}, sbjID: {0.subject_id})>" \
            "usrID: {0.user_id}".format(self)

    def serialize(self):
        """Return object data for jsonification """

        return {
            'id': self.id,
            'file_name': self.file_name,
            'file_check_sum': self.file_check_sum,
            'file_size': self.file_size,
            'uploaded_at': dump_datetime(self.uploaded_at),
            'subject_id': self.subject_id,
            'event_id': self.event_id,
            'user_id': self.user_id,
            'user_name': self.user.get_name(),
            # 'subject': self.subject.serialize(),
            # 'event': self.event.serialize(),
            # 'user': self.user.serialize(),
        }
Exemplo n.º 5
0
class LogTypeEntity(db.Model, CRUDMixin):
    """ Stores types of logs """
    __tablename__ = 'LogType'

    id = db.Column('logtID', db.Integer, primary_key=True)
    type = db.Column('logtType', db.String(255), nullable=False)
    description = db.Column('logtDescription', db.Text, nullable=False)

    def __repr__(self):
        """ Return a friendly object representation """
        return ("<LogTypeEntity(id: {0.id}, "
                "logtType: {0.type}, "
                "logtDescription: {0.description})>".format(self))
class WebSessionEntity(db.Model, CRUDMixin):
    """Store web session details"""
    __tablename__ = 'WebSession'

    id = db.Column('webID', db.Integer, primary_key=True)
    session_id = db.Column('webSessID',
                           db.String(255),
                           nullable=False,
                           default='')
    user_id = db.Column('usrID',
                        db.Integer,
                        db.ForeignKey('User.usrID'),
                        nullable=False,
                        default=0)
    ip = db.Column('webIP', db.String(15), nullable=False, default='')
    date_time = db.Column('webDateTime',
                          db.DateTime,
                          nullable=False,
                          default=datetime.datetime(datetime.MINYEAR, 1, 1))
    user_agent_id = db.Column('uaID',
                              db.Integer,
                              db.ForeignKey('UserAgent.uaID'),
                              nullable=False)
    # @OneToMany
    user_agent = db.relationship(UserAgentEntity, lazy='joined')
    user = db.relationship(UserEntity, lazy='joined')

    @staticmethod
    def get_by_session_id(session_id):
        """ Search helper: WHERE webSessID = ???"""
        return WebSessionEntity.query.filter_by(session_id=session_id).first()

    def __repr__(self):
        """ Return a friendly object representation """
        return "<WebSessionEntity (webID: '{0.id}', webSessID: {0.session_id},"\
               " usrID: {0.user_id}, webIP: {0.ip})>".format(self)
class SubjectEntity(db.Model, CRUDMixin):
    """ Stores the REDCap subject data """
    __tablename__ = 'Subject'

    id = db.Column("sbjID", db.Integer, primary_key=True)
    redcap_id = db.Column("sbjRedcapID",
                          db.String(255),
                          nullable=False,
                          unique=True)
    added_at = db.Column("sbjAddedAt",
                         db.DateTime(),
                         nullable=False,
                         server_default='0000-00-00 00:00:00')
    last_checked_at = db.Column("sbjLastCheckedAt",
                                db.DateTime(),
                                nullable=False,
                                server_default='0000-00-00 00:00:00')
    was_deleted = db.Column("sbjWasDeleted",
                            db.Boolean(),
                            nullable=False,
                            server_default='0')

    def __repr__(self):
        return """<SubjectEntity (sbjID: {0.id},
        sbjRedcapID: {0.redcap_id}, sbjAddedAt: {0.added_at})>""".format(self)

    @staticmethod
    def get_by_redcap_id(redcap_id):
        """ Search helper: WHERE redcap_id = 123"""
        subject = SubjectEntity.query.filter_by(redcap_id=redcap_id).first()
        print subject
        return subject

    def serialize(self):
        """Return object data for jsonification

        Note: There is some `residual jsx code` that expects the
            `events` array to be sent
        """
        return {
            'id': self.id,
            'redcap_id': self.redcap_id,
            'events': [],
            'added_at': utils.localize_est_datetime(self.added_at),
            'last_checked_at':
            utils.localize_est_datetime(self.last_checked_at),
            'was_deleted': self.was_deleted
        }
class SubjectEntity(db.Model, CRUDMixin):
    """ Stores the REDCap subject data """
    __tablename__ = 'Subject'

    id = db.Column("sbjID", db.Integer, primary_key=True)
    redcap_id = db.Column("sbjRedcapID",
                          db.String(255),
                          nullable=False,
                          unique=True)
    added_at = db.Column("sbjAddedAt",
                         db.DateTime(),
                         nullable=False,
                         server_default='0000-00-00 00:00:00')
    last_checked_at = db.Column("sbjLastCheckedAt",
                                db.DateTime(),
                                nullable=False,
                                server_default='0000-00-00 00:00:00')
    was_deleted = db.Column("sbjWasDeleted",
                            db.Boolean(),
                            nullable=False,
                            server_default='0')

    # @OneToOne
    # user = db.relationship('UserEntity', uselist=False, lazy='joined')

    def __repr__(self):
        return """<SubjectEntity (sbjID: {0.id},
        sbjRedcapID: {0.redcap_id}, sbjAddedAt: {0.added_at})>""".format(self)

    def serialize(self):
        """Return object data for jsonification

        Note: There is some `residual jsx code` that expects the
            `events` array to be sent
        """

        return {
            'id': self.id,
            'events': [],
            # 'events': [{'event_id': '1', 'event_files': '1'}],
            # 'files': [f.name for f in self.files],
            'added_at': dump_datetime(self.added_at),
            'last_checked_at': dump_datetime(self.last_checked_at),
            'was_deleted': self.was_deleted
        }
Exemplo n.º 9
0
class SubjectFileEntity(db.Model, CRUDMixin):
    """ Stores the uploaded file metadata """
    __tablename__ = 'SubjectFile'

    id = db.Column("sfID", db.Integer, primary_key=True)
    subject_id = db.Column("sbjID",
                           db.Integer,
                           db.ForeignKey('Subject.sbjID'),
                           nullable=False)
    event_id = db.Column("evtID",
                         db.Integer,
                         db.ForeignKey('Event.evtID'),
                         nullable=False)
    file_name = db.Column("sfFileName", db.String(255), nullable=False)
    file_check_sum = db.Column("sfFileCheckSum", db.String(32), nullable=False)
    file_size = db.Column("sfFileSize", db.String(255), nullable=False)
    uploaded_at = db.Column("sfUploadedAt",
                            db.DateTime,
                            nullable=False,
                            server_default='0000-00-00 00:00:00')
    user_id = db.Column("usrID",
                        db.Integer,
                        db.ForeignKey('User.usrID'),
                        nullable=False)

    # @OneToOne
    subject = db.relationship('SubjectEntity', uselist=False, lazy='joined')
    event = db.relationship('EventEntity', uselist=False, lazy='joined')
    user = db.relationship('UserEntity', uselist=False, lazy='joined')

    def create_folder(self, directory):
        """
        Create folder if it does not exist
        """
        success = True

        if not os.path.exists(directory):
            try:
                os.makedirs(directory)
            except Exception as exc:
                print "Failed due: {}".format(exc)
                success = False
        return success

    @classmethod
    def get_convention_file_name(cls, date_and_time, subject_id, file_name):
        """
        Concatenate the pieces to obtain a fiendly file name.

        @TODO: check if we need to need the "site ID" and
            how to obtain it.

        Original convention:
            20120101_0123_SiteID_A_SubjectID_B_Sequence123_xyz.jpg

        Actual implementation (does not keep track of sequences):
            20120101_0123_site_subject_B_xyz.jpg
        """
        date_part = date_and_time.strftime("%Y%m%d")
        time_part = date_and_time.strftime("%H%M")

        file_convention = "{}_{}_site_subject_{}_{}".format(
            date_part, time_part, subject_id, file_name)
        return file_convention

    def get_full_path(self, prefix):
        """
        Build the full path using the database info and the prefix
        @TODO: implement the naming convention

        20120101_0123_SiteIDA_SubjectIDB_Sequence123_xyz.jpg
        """
        subject_dir = os.path.join(prefix,
                                   "subject_{}".format(self.subject.redcap_id))
        success = self.create_folder(subject_dir)
        assert success

        file_convention = SubjectFileEntity.get_convention_file_name(
            self.uploaded_at, self.subject.redcap_id, self.file_name)
        full_path = os.path.join(subject_dir, file_convention)
        return full_path

    def __repr__(self):
        """ Return a friendly object representation """
        return "<SubjectFileEntity (sfID: {0.id}, sbjID: {0.subject_id}, " \
            "usrID: {0.user_id}, sfFileName: {0.file_name}>)".format(self)

    def serialize(self):
        """Return object data for jsonification """
        # @TODO: add information about download counts
        return {
            'id': self.id,
            'file_name': self.file_name,
            'file_check_sum': self.file_check_sum,
            'file_size': self.file_size,
            'uploaded_at': utils.localize_est_datetime(self.uploaded_at),
            'subject_id': self.subject_id,
            'event_id': self.event_id,
            'user_id': self.user_id,
            'user_name': self.user.get_name(),
            # 'subject': self.subject.serialize(),
            # 'event': self.event.serialize(),
            # 'user': self.user.serialize(),
        }
Exemplo n.º 10
0
class UserEntity(db.Model, UserMixin, CRUDMixin):
    """ Stores the basic information about the user.
    Implements the functions as required by:
        https://flask-login.readthedocs.org/en/latest/
    """
    __tablename__ = 'User'

    id = db.Column("usrID", db.Integer, primary_key=True)
    email = db.Column("usrEmail", db.String(255), nullable=False, unique=True)
    first = db.Column("usrFirst", db.String(255), nullable=False)
    last = db.Column("usrLast", db.String(255), nullable=False)
    minitial = db.Column("usrMI", db.String(1), nullable=False)
    added_at = db.Column("usrAddedAt",
                         db.DateTime(),
                         nullable=False,
                         server_default='0000-00-00 00:00:00')
    modified_at = db.Column("usrModifiedAt", db.TIMESTAMP(), nullable=False)
    # server_default='CURRENT_TIMESTAMP')
    email_confirmed_at = db.Column("usrEmailConfirmedAt",
                                   db.DateTime(),
                                   nullable=False,
                                   server_default='0000-00-00 00:00:00')
    active = db.Column("usrIsActive",
                       db.Boolean(),
                       nullable=False,
                       server_default='1')

    access_expires_at = db.Column("usrAccessExpiresAt",
                                  db.DateTime(),
                                  nullable=False,
                                  server_default='0000-00-00 00:00:00')
    password_hash = db.Column("usrPasswordHash",
                              db.String(255),
                              nullable=False,
                              server_default='')

    # @OneToMany
    roles = db.relationship(RoleEntity,
                            secondary=UserRoleEntity.__tablename__,
                            backref=db.backref('users'),
                            lazy='dynamic')
    """
    `lazy` defines when SQLAlchemy will load the data from the database:
        'select' (which is the default) means that SQLAlchemy will load the
        data as necessary in one go using a standard select statement.
    'joined' tells SQLAlchemy to load the relationship in the same query as the
        parent using a JOIN statement.
    'subquery' works like 'joined' but instead SQLAlchemy will use a subquery.
    'dynamic' is special and useful if you have may items. Instead of loading
        the items SQLAlchemy will return another query object which you can
        further refine before loading them items. This is usually what you want
        if you expect more than a handful of items for this relationship.
    """
    def is_active(self):
        """ An user can be blocked by setting a flag in the database """
        return self.active

    def is_expired(self):
        """ An user can be blocked by setting expiration date to yesterday"""
        return self.access_expires_at < datetime.today()

    def is_anonymous(self):
        """ Flag instances of valid users """
        return False

    def is_authenticated(self):
        """ Returns True if the user is authenticated, i.e. they have provided
        valid credentials.
        (Only authenticated users will fulfill the criteria of login_required.)
        """
        return True

    def get_id(self):
        """ The id encrypted in the session """
        return unicode(self.id)

    def get_roles(self):
        """ Return text representation of user roles """
        return [role.name for role in self.roles]

    """
    @property
    def to_visible(self):
    visible_props = ['id', 'email', 'first', 'last', 'minitial',
                     'added_at', 'modified_at', 'email_confirmed_at',
                     'is_active', 'access_expires_at']

        return dict([(key, val) for key, val in self.__dict__.items()
                    if key in UserEntity.visible_props])
    """

    def get_name(self):
        """
        :rtype string
        :return concat(first, ' ', last)
        """
        return "{} {}".format(self.first, self.last)

    def get_email_verification_token(self):
        """
        :rtype string
        :return the email verification token stored in the database
        """
        return "token"

    def serialize(self):
        """Return object data for jsonification"""

        return {
            'id': self.id,
            'email': self.email,
            'roles': [r.name for r in self.roles],
            'first': self.first,
            'last': self.last,
            'minitial': self.minitial,
            'is_active': True if self.active else False,
            'is_expired': True if self.is_expired() else False,
            'added_at': dump_datetime(self.added_at),
            'email_confirmed_at': dump_datetime(self.email_confirmed_at),
            'access_expires_at': dump_datetime(self.access_expires_at),
        }

    def __repr__(self):
        return "<UserEntity (usrID: {0.id}, usrEmail: {0.email}, " \
               "usrIsActive: {0.active})>".format(self)