Exemple #1
0
class Receipt(db.Model):
    __tablename__ = 'receipts'
    id = db.Column(db.Integer, primary_key=True)
    description = db.Column(db.Text, nullable=False)
    filename = db.Column(db.String(128), nullable=False)
    amount = db.Column(db.Float, nullable=False)
    bill_id = db.Column(db.Integer, db.ForeignKey('bills.id'), nullable=False)
    bill = db.relationship('Bill', backref=db.backref('receipts', lazy='dynamic'))

    @staticmethod
    def check(description, amount, content):

        if (not description) or (not amount) or (not content):
            raise ProcessingException(description='Tositteista puuttuu tietoja.')

    @staticmethod
    def preprocess(description, amount, content):
        header, data = content.split(',')
        data = a2b_base64(data)
        type = header.split('/')[1].split(';')[0]
        filename = sha512(data).hexdigest() + '.pdf'

        with open(app.config['RECEIPTS_FOLDER'] + filename, 'wb') as f:
            f.write(data)

        return {
                'filename': filename,
                'description': description,
                'amount': amount
                }
Exemple #2
0
class Authienticat(db.Model):
    ID = db.Column(
        db.Integer,
        primary_key=True,
    )
    NationalID = db.Column(db.String(250), nullable=False)
    Token = db.Column(db.String(250), nullable=False)
Exemple #3
0
class Environments(db.Model):
    __tablename__ = 'environments'
    #primary_key,设置为true,这列就是表对主键
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(1024), nullable=False)
    create_time = db.Column(db.DateTime, nullable=True)

    def __init__(self, name):
        self.name = name
Exemple #4
0
class Entity_list_user(db.Model):
    ID = db.Column(db.Integer, primary_key=True)
    NationalID = db.Column(db.String(250), nullable=False, unique=True)
    FirstName = db.Column(db.String(250), nullable=False)
    LastName = db.Column(db.String(250), nullable=False)
    Email = db.Column(db.String(250), nullable=False)
    Password = db.Column(db.String(250), nullable=False)
    FacultyID = db.Column(db.String(250), unique=True)
    Faculty = db.Column(db.String(250))
    Dept = db.Column(db.String(250))
    UserType = db.Column(db.String(250), nullable=False)
Exemple #5
0
class Variable(db.Model):
    __tablename__ = 'variable1'
    #primary_key,设置为true,这列就是表对主键
    id = db.Column(db.Integer, primary_key=True)
    #nullable,如果设为 True ,这列允许使用空值;如果设为False ,这列不允许使用空值
    env_id = db.Column(db.Integer,
                       db.ForeignKey('environments.id'),
                       nullable=False)
    key = db.Column(db.String(1024), nullable=False)
    value = db.Column(db.String(1024), nullable=False)
    #nullable,如果设为 True ,这列允许使用空值;如果设为True ,这列允许使用空值
    create_time = db.Column(db.DateTime, nullable=True)

    def __init__(self, env_id, key, value):
        self.env_id = env_id
        self.key = key
        self.value = value
Exemple #6
0
class User(db.Model):
    __tablename__ = 'users'
    username = db.Column(db.String(100), primary_key=True)
    email = db.Column(db.String(100), nullable=False)
    password_hash = db.Column(db.Text, nullable=False)
    admin = db.Column(db.Boolean, nullable=False)
    restore_id = db.Column(db.Text, unique=True)
    restore_valid = db.Column(db.DateTime)

    def change_password(self, password):
        if not self.restore_valid or datetime.now() > self.restore_valid:
            return "", 400
        self.password_hash = pw.hash(password)
        self.restore_id = None
        self.restore_valid = None
        db.session.add(self)
        db.session.commit()
        return "", 200

    def request_password_change(self):
        self.restore_id = str(uuid4())
        self.restore_valid = datetime.now() + timedelta(days=1)

        Mailer.send_password_restore(self)

        db.session.add(self)
        db.session.commit()

    @staticmethod
    def preprocess(**kw):
        kw['data']['password_hash'] = pw.hash(kw['data']['password'])
        del kw['data']['password']
Exemple #7
0
class Entity_list_Attendance(db.Model):
    ID = db.Column(
        db.Integer,
        primary_key=True,
    )
    FacultyID = db.Column(db.String(250), nullable=False)
    Name = db.Column(db.String(250), nullable=False)
    Time = db.Column(db.String(250), nullable=False)
    InOut = db.Column(db.String(250), nullable=False)
    Date = db.Column(db.String(250), nullable=False)
    db.ForeignKeyConstraint(['FacultyID'], ['Entity_list_user.FacultyID'],
                            name='fk_FacultyID')
Exemple #8
0
class Bill(db.Model):
    __tablename__ = 'bills'
    id = db.Column(db.Integer, primary_key=True)
    submitter = db.Column(db.String(40), nullable=False)
    iban = db.Column(db.String(80), nullable=False)
    description = db.Column(db.Text, nullable=False)
    date = db.Column(db.DateTime, nullable=False)
    accepted = db.Column(db.Boolean, default=False)
    accepted_at = db.Column(db.Unicode, nullable=True)
    paid = db.Column(db.Unicode, nullable=True)
    hidden = db.Column(db.Boolean, default=False)

    @staticmethod
    def preprocess_update(**kw):
        print(kw)
        for key in kw:
            if key not in ['id', 'paid', 'accepted', 'accepted_at']:
                del kw[key]

    # Preprocessor for posting new bill
    @staticmethod
    def preprocess_post(**kw):

        submitter = kw['data']['submitter']
        iban = kw['data']['iban']
        description = kw['data']['description']
        receipts = kw['data']['receipts']

        kw['data']['date'] = str(datetime.now())

        errors = []

        if len(submitter or '\0') == 0:
            errors.append('Nimi on pakollinen kenttä.')

        try:
            IBAN(iban or '\0')
        except ValueError:
            errors.append('IBAN ei ole validi.')

        if len(description or '\0') == 0:
            errors.append('Maksun peruste tulee antaa.')

        if len(receipts) == 0:
            errors.append('Tositteita ei löytynyt.')

        if len(errors) > 0:
            raise ProcessingException(description='\n'.join(errors))

        for r in receipts:
            Receipt.check(**r)

        nrs = []
        for r in receipts:
            nrs.append(Receipt.preprocess(**r))
        kw['data']['receipts'] = nrs

    @staticmethod
    def accept(id, accepted_at, paid):
        bill = Bill.query().get(id)
        bill.accepted = True
        bill.accepted_at = accepted_at
        bill.paid = paid
        db.session.add(bill)
        db.session.commit()

    @staticmethod
    def toggle_hidden(id):
        bill = Bill.query().get(id)
        bill.hidden = not bill.hidden
        db.session.add(bill)
        db.session.commit()

    @staticmethod
    def postprocess_get(**kw):
        pass

    @classmethod
    def query(cls):
        orig = db.session.query(cls)
        return orig.order_by(Bill.date.desc())

    @staticmethod
    def render(id):
        fn = latexify(Bill.query().get(id))
        if not fn:
            return "Oops...", 404

        return send_from_directory(app.config['TMP_FOLDER'],
                                   fn,
                                   as_attachment=True,
                                   attachment_filename=Bill.pretty_name(id))

    @staticmethod
    def pretty_name(id):
        bill = Bill.query().get(id)
        return "{}-{}.pdf".format(bill.submitter.replace(" ", "_"),
                                  bill.date.strftime('%d-%m-%Y'))