-
Notifications
You must be signed in to change notification settings - Fork 0
/
wombat.py
355 lines (257 loc) · 10.9 KB
/
wombat.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# -*- coding: utf-8 -*-
"""
Wombat
~~~~~~
An Asset Managment System written using Flask with SQLAlchemy
Extensions used:
Flask-SQLAlchemy
Flask-WTF
Flask-Uploads
Flask-Mail
:Author : Pronoy Chopra
:Project: Worldforge
:program: GSoC 2011
:Mentor : Kai Blin
"""
#-----------------------------imports-------------------------------------------
#bunch of imports
from __future__ import with_statement
from sqlite3 import dbapi2 as sqlite3
#from contextlib import closing
from flask import Flask, request, session, g, redirect, url_for, abort, \
render_template, flash
from wombat_config import config_file
from wombat_config.config_file import LOCAL_REPO, FIRST_VIEW
from wombatdb import db
from wombatdb import User, UserData
from backend.svnfunctions import SVNfunctions
from backend.functions import Base
from flaskext.bcrypt import bcrypt_init, generate_password_hash, \
check_password_hash
from flaskext.wtf import Form, TextField, TextAreaField, PasswordField, \
SubmitField, Required, ValidationError, validators
from flaskext.mail import Mail
import os
#-------------------------------------------------------------------------------
# create our application :)
app = Flask(__name__)
# take the configuration from the config file
app.config.from_object(config_file)
#use py-bcrypt for hashing password
bcrypt_init(app)
mail = Mail(app)
#all functions need to have to be imported from Base class or SVNfunctions() class
func = Base()
svn = SVNfunctions(app.config['LOCAL_REPO'])
#-----------------------All forms are defined here------------------------------
class LoginForm (Form):
username = TextField("Email")
password = PasswordField("Password")
submit = SubmitField("Login")
def validate_username(self,username):
access_user = User.query.filter_by(email = username.data).first()
if access_user is None:
raise ValidationError, "Invalid Username"
def validate_password(self,password):
access_user = User.query.filter_by(email = self.username.data).first()
if access_user is None:
raise ValidationError, "Invalid Username"
else:
condition = check_password_hash(access_user.password, password.data)
if not condition:
raise ValidationError, "Invalid Password"
def validate_user(self,username):
access_user = User.query.filter_by(email = username.data).first()
if not access_user.active:
raise ValidationError, "User not Active"
#this is the registeration form
class RegisterationForm (Form):
#email field: Mandatory
email = TextField("Email Address", [validators.Length(min=6)])
#password field: Mandatory
password = PasswordField("New Password", [
validators.Required(),
validators.EqualTo('confirm', message='Passwords must match')
])
#confirm password field: Mandatory
confirm = PasswordField('Repeat Password')
#name of the user: Non Mandatory
Name = TextField("Your full name")
#nick name of the user: Non Mandatory
Nickname = TextField("Your nick name")
#vcs_username of the user: Non Mandatory
VCS_Username = TextField("Your VCS Username")
#vcs_password of the user: Non mandatory
VCS_Password = PasswordField("Your VCS Password")
submit = SubmitField("Register")
#querying for known entries for the given email
def validate_username(self,email):
unidentified = User.query.filter_by(email=email.data).first()
if unidentified is not None:
raise ValidationError, "Username already exists"
class UpdateForm (Form):
#username = TextField("Your new email")
password = PasswordField("New Password", [
validators.Required(),
validators.EqualTo('confirm',
message='Passwords must match')
])
confirm = PasswordField("Your new password again")
Name = TextField("Your name")
Nickname = TextField("Your nickname")
VCS_Username = TextField("Your VCS Username")
VCS_Password = PasswordField("Your VCS Password")
Update = SubmitField("Update Information")
class ForgotPassword (Form):
username = TextField("Your Email/Username ")
submit = SubmitField("Check for Username")
def validate_email(self,username):
forgetful_user = User.query.filter_by(email=username.data).first()
if forgetful_user is None:
raise ValidationError, "Username doesn't exist"
#-----------------------------database related actions--------------------------
def connect_db():
"""Returns a new connection to the database."""
return sqlite3.connect(app.config['DATABASE'])
@app.before_request
def before_request():
"""Make sure we are connected to the database each request."""
g.db = connect_db()
@app.after_request
def after_request(response):
"""Closes the database again at the end of the request."""
g.db.close()
return response
#----------------------------decorators start here------------------------------
#index page
@app.route('/')
def server_status():
#The repository information belongs to the updated copy of a
#checked out version on the local system
url_out = svn.get_url()
#returns the URL of the repository not the checked out version
svn.update_copy()
#call this function before fetching any other information
revision = svn.get_revision_no()
#gets the revision number
fileSize,fileLength,folderCount = func.get_info(LOCAL_REPO)
#unwind a touple
fileSize = func.convert_bytes(fileSize)
#convert fileSize in human readable form
serverdict = (dict(url_out=url_out,revision=revision,fileSize=fileSize, \
fileLength=fileLength,folderCount=folderCount))
#create a dict out of the information that needs to be passed to the template
return render_template('server_status.html',serverdict=serverdict)
#pass serverdict as the context
#add a new user
@app.route('/register', methods=['GET', 'POST'])
def add_user():
#render registeration form
form = RegisterationForm(request.form)
#if posted and email is non empty and password is non empty
if request.method == 'POST' and form.validate():
#need to hash password
password_hash = generate_password_hash(form.password.data)
new_user = User(form.email.data,password_hash)
db.session.add(new_user)
db.session.commit()
#bug fixed: if VCS_Password is not empty then hash otherwise not
if not (form.VCS_Password.data == ''):
#need to hash VCS password
VCS_Password_hash = generate_password_hash(form.VCS_Password.data)
new_user_data = UserData(form.Name.data, form.Nickname.data, \
form.VCS_Username.data,VCS_Password_hash)
else:
new_user_data= UserData(form.Name.data, form.Nickname.data, \
form.VCS_Username.data,form.VCS_Password.data)
db.session.add(new_user_data)
db.session.commit()
#simple check to see if the user has been registered
access_user = User.query.filter_by(email = form.email.data).first()
if not access_user is None:
flash('The User details have been registered. ')
#func.send_activation(form.email.data)
#status of registeration
registeration_status="Sent email to user for activation"
return redirect(url_for('login'), registered=registeration_status)
else:
flash("The User doesn't seem to be in our Database yet, please try again")
return render_template('add_user.html',form=form)
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm(request.form)
if form.validate_on_submit():
session['logged_in'] = True
session['username'] = form.username.data
if session['logged_in']:
flash('You were logged in as '+ session['username'])
else:
flash('oops you were not logged in')
return redirect(url_for('server_status'))
return render_template('login.html', form=form)
@app.route('/logout')
def logout():
session.pop('logged_in', None)
return redirect(url_for('server_status'))
@app.route('/navigator', methods=['GET', 'POST'])
def navigator():
path = request.args['path']
repo_path = app.config['LOCAL_REPO']
path = path[1:]
requested_path = os.path.join(repo_path, path)
if not os.path.exists(requested_path):
return render_template("navigator.html",
listType="PATHNOTFOUND")
elif not os.path.isdir(requested_path):
return render_template("navigator.html",
listType="SHOWFILE",
path=path)
else:
return render_template('navigator.html',
listType = "SHOWDIRECTORY",
path="/" + path,
listing=svn.get_dir_info(path))
#---------------------not so important pages------------------------------------
@app.route('/activate/<username>/<hash>')
def activate_username():
@app.route('/forgot_password')
def forgot_password():
form = ForgotPassword(request.form)
if form.validate_on_submit():
return render_template('forgot_password.html')
@app.route('/workbench')
def workbench():
return render_template('workbench.html')
@app.route('/settings')
def settings():
return render_template('settings.html')
@app.route('/settings/account')
def account_settings():
form = UpdateForm(request.form)
if request.method == 'POST' and form.validate():
logged_user = session['username']
request_user = User.query.filter_by(username = logged_user).first()
return render_template('account_settings.html')
@app.route('/status')
def status_view():
return render_template('status.html')
@app.route('/mail')
def mail():
return render_template('mail.html')
@app.route('/mail/inbox')
def mail_inbox():
return render_template('mail_inbox.html')
@app.route('/docs')
def show_docs():
return render_template('docs.html')
@app.route('/docs/know_more')
def know_more():
return render_template('know_more.html')
@app.route('/docs/why')
def why():
return render_template('why.html')
@app.route('/docs/license')
def license():
return render_template('license.html')
if __name__ == '__main__':
app.run()