-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
95 lines (76 loc) · 2.72 KB
/
models.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from app import app
from flask import flash
from flask.ext.bcrypt import generate_password_hash
from flask.ext.sqlalchemy import SQLAlchemy
from os import listdir, makedirs
from os.path import isfile, join, exists
from subprocess import call
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:909090@localhost/wsr'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True)
password = db.Column(db.String(120))
role = db.Column(db.String(10))
def __init__(self, name, password, role = 'user'):
self.name = name
self.password = generate_password_hash(password)
self.role = role
def __repr__(self):
return '<User %r>' % self.name
def save(self):
db.session.add(self)
db.session.commit()
return self
def run(self, script):
try:
path = self.name + '/' + script.name
call(['chmod', '+x', path])
call(['./' + path] + script.arguments.split())
except OSError:
return flash('Incorrect script name or arguments')
def clear_scripts(self):
directory = self.name
if not exists(directory):
makedirs(directory)
arr = [ f for f in listdir(self.name) if isfile(join(self.name,f)) ]
for script in self.scripts:
if script.name not in arr:
db.session.delete(script)
@staticmethod
def find_by_name(username):
user = User.query.filter_by(name=username).first()
return user
def is_active(self):
return True
def get_id(self):
return self.id
def is_authenticated(self):
return True
def is_anonymous(self):
return False
class Script(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80))
arguments = db.Column(db.String(80))
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
user = db.relationship('User', backref='scripts', lazy='joined')
def __init__(self, name, arguments, user):
self.name = name
self.arguments = arguments
self.user = user
def save(self):
db.session.add(self)
db.session.commit()
self.create_data()
return self
def create_data(self):
directory = self.user.name
if not exists(directory):
makedirs(directory)
open(directory + '/%s' % self.name, 'w+')
open(directory + '/%s.output' % self.name, 'w+')
def check_arguments(self):
result = [False for e in self.arguments if not e.isalnum() and e not in [' ', '-']]
return False if False in result else True