-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
55 lines (46 loc) · 2.48 KB
/
Copy pathmodels.py
File metadata and controls
55 lines (46 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash
from datetime import datetime
db = SQLAlchemy()
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), unique=True, index=True)
email = db.Column(db.String(120), unique=True, index=True)
password_hash = db.Column(db.String(128))
resources = db.relationship('Resource', backref='owner', lazy='dynamic')
requests = db.relationship('Request', backref='requester', lazy='dynamic')
def set_password(self, password):
self.password_hash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password_hash, password)
class Resource(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
description = db.Column(db.Text)
category = db.Column(db.String(50))
available_from = db.Column(db.DateTime, nullable=False)
available_to = db.Column(db.DateTime, nullable=False)
owner_id = db.Column(db.Integer, db.ForeignKey('user.id'))
created_at = db.Column(db.DateTime, default=datetime.utcnow)
# A resource can have many allocations
allocations = db.relationship('Allocation', backref='resource', lazy='dynamic')
class Request(db.Model):
id = db.Column(db.Integer, primary_key=True)
resource_type = db.Column(db.String(50))
needed_from = db.Column(db.DateTime, nullable=False)
needed_to = db.Column(db.DateTime, nullable=False)
priority = db.Column(db.Integer, default=5) # 1-10, higher is more important
requester_id = db.Column(db.Integer, db.ForeignKey('user.id'))
status = db.Column(db.String(20), default='pending') # pending, allocated, denied
created_at = db.Column(db.DateTime, default=datetime.utcnow)
# A request can have one allocation
allocation = db.relationship('Allocation', backref='request', uselist=False)
class Allocation(db.Model):
id = db.Column(db.Integer, primary_key=True)
resource_id = db.Column(db.Integer, db.ForeignKey('resource.id'))
request_id = db.Column(db.Integer, db.ForeignKey('request.id'))
allocated_from = db.Column(db.DateTime, nullable=False)
allocated_to = db.Column(db.DateTime, nullable=False)
status = db.Column(db.String(20), default='active') # active, completed, cancelled
created_at = db.Column(db.DateTime, default=datetime.utcnow)