Exemplo n.º 1
0
class Workorder(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(30), nullable=False)

    create_date = db.Column(db.DateTime,
                            nullable=False,
                            default=datetime.utcnow)
    update_date = db.Column(db.DateTime,
                            nullable=False,
                            default=datetime.utcnow)
    requested_date = db.Column(
        db.DateTime)  # Requested delivery date when the project is ready
    ready_date = db.Column(db.DateTime)  # When the project was actually ready
    status = db.Column(db.String(30), nullable=False, default='created')

    public_chargers = db.Column(db.Integer, nullable=False)
    public_installation_location = db.Column(db.Text, nullable=False)
    public_charging_power = db.Column(db.Float, nullable=False)

    private_chargers = db.Column(db.Integer, nullable=False)
    private_installation_location = db.Column(db.Text, nullable=False)
    private_charging_power = db.Column(db.Float, nullable=False)
    installation_type = db.Column(db.String(10), nullable=False)
    total = db.Column(db.Float, nullable=False, default=0.0)

    # Backrefs
    org = db.relationship('Organization', backref='workorder', lazy=True)
    location = db.relationship('Location', backref='workorder', lazy=True)
    line_items = db.relationship('Lineitem', backref='workorder', lazy=True)
    attachments = db.relationship('Workorderattachment',
                                  backref='workorder',
                                  lazy=True)
    comments = db.relationship('Comment', backref='workorder', lazy=True)

    def order_total(self):
        """ Calculate the order total based on the line_items in the workorder """
        order_total = 0
        for line in self.line_items:
            print(type(line.total))
            order_total += float(line.total)
        return order_total

    def __repr__(self):
        return f'Workorder <{self.id} | {self.title} | Created {self.create_date} | Updated {self.update_date} | {self.status}>'
Exemplo n.º 2
0
class Lineitem(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    discount = db.Column(db.DECIMAL(3, 2), default=0.00)
    quantity = db.Column(db.Integer, nullable=False)
    total = db.Column(db.Float, nullable=False)

    # Foreign keys
    workorder_id = db.Column(db.Integer, db.ForeignKey('workorder.id'))
    product_id = db.Column(db.Integer, db.ForeignKey('product.id'))

    product = db.relationship('Product', backref='lineitem')

    def __repr__(self):
        return f'LineItem <Workorder ID {self.workorder_id} | Product ID {self.product_id} | Quantity {self.quantity} | Total {self.total}>'
Exemplo n.º 3
0
class Survey(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    create_date = db.Column(db.DateTime,
                            nullable=False,
                            default=datetime.utcnow)
    update_date = db.Column(db.DateTime,
                            nullable=False,
                            default=datetime.utcnow)
    status = db.Column(db.String(30), nullable=False, default='created')

    # Installation related information
    installation_method = db.Column(db.String(30), nullable=False)
    concrete_foundation = db.Column(db.Boolean)
    grid_connection = db.Column(db.Integer)
    grid_cable = db.Column(db.String(15))
    max_power = db.Column(db.Float)
    consumption_fuse = db.Column(db.Integer)
    maincabinet_rating = db.Column(db.Integer)
    empty_fuses = db.Column(db.Boolean)
    number_of_slots = db.Column(db.Integer)
    signal_strength = db.Column(db.Float)
    installation_location = db.Column(db.String(255))

    # Foreign keys to User model
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    workorder_id = db.Column(db.Integer, db.ForeignKey('workorder.id'))

    # Table relationships and backrefs
    contact_person = db.relationship('Contactperson',
                                     secondary=survey_contact_rel,
                                     backref='surveys',
                                     lazy=True)
    pictures = db.relationship('Surveypicture', backref='survey', lazy=True)
    location = db.relationship('Location', backref='survey', lazy=True)

    def __repr__(self):
        return f'Survey <Survey ID {self.id} | Workorder ID {self.workorder_id}>'
Exemplo n.º 4
0
class Organization(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    org_name = db.Column(db.String(100), nullable=False)
    org_number = db.Column(db.String(20), nullable=False, unique=True)
    address = db.Column(db.String(100), nullable=False)
    postal_code = db.Column(db.String(10), nullable=False)
    city = db.Column(db.String(30), nullable=False)
    country = db.Column(db.String(30), nullable=False)

    # One-to-many and many-to-many relations
    org_type = db.relationship('Orgtype',
                               secondary=org_type_rel,
                               backref='organizations',
                               lazy=True)
    contact_persons = db.relationship('Contactperson',
                                      backref='organization',
                                      lazy=True)
    users = db.relationship('User', backref='organization', lazy=True)

    # Foreign keys
    workorder_id = db.Column(db.Integer, db.ForeignKey('workorder.id'))

    def __repr__(self):
        return f'Organization <{self.org_name} | {self.org_number} | {self.city} | {self.country}'
Exemplo n.º 5
0
class User(db.Model, UserMixin):
    # When the login token expires in seconds
    expires_sec = 1800

    id = db.Column(db.Integer, primary_key=True)
    first_name = db.Column(db.String(30), nullable=False)
    last_name = db.Column(db.String(30), nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    password = db.Column(db.String(120), nullable=False)

    # Foreign keys
    org_id = db.Column(db.Integer, db.ForeignKey('organization.id'))

    # Backref to show surveys done by user
    surveys = db.relationship('Survey', backref='user', lazy=True)

    def set_password(self, password):
        """Set the password of the user"""
        self.password = bcrypt.generate_password_hash(password).decode('utf-8')

    def check_password(self, password):
        """Check that the submitted password matches to password stored in DB."""
        return bcrypt.check_password_hash(self.password, password)

    def get_password_token(self, expires_in=600):
        """Create JWT token for password reset email which expire time is specified in expises_in variable.
            Returns UTF-8 formatted JWT token to attach reset email"""
        return jwt.encode(
            {
                'reset_password': self.id,
                'exp': time() + expires_in
            },
            app.config['SECRET_KEY'],
            algorithm='HS256').decode('utf-8')

    @staticmethod
    def verify_password_token(token):
        """Check that the submitted token for password reset matches to token created by the application and returns the user information if token is correct"""
        try:
            id = jwt.decode(token,
                            app.config['SECRET_KEY'],
                            algorithms=['HS256'])['reset_password']
        except:
            return
        return User.query.get(id)

    def __repr__(self):
        return f'User <{self.first_name} | {self.last_name} | {self.email} |>'