Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Jannik Hellenkamp
website
Commits
63893c5e
Commit
63893c5e
authored
Aug 30, 2016
by
Julian Rother
Browse files
Commit changes to server.py after moving parts to db.py
parent
bb104174
Changes
1
Hide whitespace changes
Inline
Side-by-side
server.py
View file @
63893c5e
#!/bin/python
from
flask
import
*
from
flask
import
Flask
,
g
,
request
,
url_for
,
redirect
,
session
,
render_template
,
flash
from
functools
import
wraps
import
datetime
import
sqlite3
import
os
import
re
app
=
Flask
(
__name__
)
config
=
app
.
config
config
[
'DB_SCHEMA'
]
=
'db_schema.sql'
config
[
'DB_DATA'
]
=
'db_example.sql'
...
...
@@ -17,110 +15,17 @@ config['SQLITE_INIT_SCHEMA'] = True
config
[
'SQLITE_INIT_DATA'
]
=
False
config
[
'DEBUG'
]
=
False
config
[
'VIDEOPREFIX'
]
=
'https://videoag.fsmpi.rwth-aachen.de'
if
__name__
==
'__main__'
:
config
[
'SQLITE_INIT_DATA'
]
=
True
config
[
'DEBUG'
]
=
True
config
.
from_pyfile
(
'config.py'
,
silent
=
True
)
app
.
jinja_env
.
globals
[
'videoprefix'
]
=
config
[
'VIDEOPREFIX'
]
mod_endpoints
=
[]
if
config
[
'DB_ENGINE'
]
==
'sqlite'
:
created
=
not
os
.
path
.
exists
(
config
[
'SQLITE_DB'
])
db
=
sqlite3
.
connect
(
config
[
'SQLITE_DB'
])
cur
=
db
.
cursor
()
if
config
[
'SQLITE_INIT_SCHEMA'
]:
cur
.
executescript
(
open
(
config
[
'DB_SCHEMA'
]).
read
())
if
config
[
'SQLITE_INIT_DATA'
]
and
created
:
cur
.
executescript
(
open
(
config
[
'DB_DATA'
]).
read
())
db
.
commit
()
db
.
close
()
# Row wrapper for sqlite
def
dict_factory
(
cursor
,
row
):
d
=
{}
for
idx
,
col
in
enumerate
(
cursor
.
description
):
if
type
(
row
[
idx
])
==
str
:
d
[
col
[
0
].
split
(
'.'
)[
-
1
]]
=
row
[
idx
].
replace
(
'
\\
n'
,
'
\n
'
).
replace
(
'
\\
r'
,
'
\r
'
)
else
:
d
[
col
[
0
].
split
(
'.'
)[
-
1
]]
=
row
[
idx
]
return
d
def
query
(
operation
,
*
params
):
if
config
[
'DB_ENGINE'
]
==
'mysql'
:
import
mysql.connector
if
'db'
not
in
g
or
not
g
.
db
.
is_connected
():
g
.
db
=
mysql
.
connector
.
connect
(
user
=
config
[
'MYSQL_USER'
],
password
=
config
[
'MYSQL_PASSWD'
],
host
=
config
[
'MYSQL_HOST'
],
database
=
config
[
'MYSQL_DB'
])
if
not
hasattr
(
request
,
'db'
):
request
.
db
=
g
.
db
.
cursor
(
dictionary
=
True
)
request
.
db
.
execute
(
operation
.
replace
(
'?'
,
'%s'
),
params
)
elif
config
[
'DB_ENGINE'
]
==
'sqlite'
:
if
'db'
not
in
g
:
g
.
db
=
sqlite3
.
connect
(
config
[
'SQLITE_DB'
])
g
.
db
.
row_factory
=
dict_factory
g
.
db
.
isolation_level
=
None
if
not
hasattr
(
request
,
'db'
):
request
.
db
=
g
.
db
.
cursor
()
request
.
db
.
execute
(
operation
,
params
)
else
:
return
[]
return
request
.
db
.
fetchall
()
@
app
.
teardown_request
def
commit_db
(
*
args
):
if
hasattr
(
request
,
'db'
):
request
.
db
.
close
()
g
.
db
.
commit
()
def
searchquery
(
text
,
columns
,
match
,
tables
,
suffix
,
*
suffixparams
):
params
=
[]
subexprs
=
[]
words
=
text
.
split
(
' '
)
prio
=
len
(
words
)
+
1
for
word
in
words
:
if
word
==
''
or
word
.
isspace
():
continue
matchexpr
=
' OR '
.
join
([
'%s LIKE ?'
%
column
for
column
in
match
])
subexprs
.
append
(
'SELECT %s, %s AS _prio FROM %s WHERE %s'
%
(
columns
,
str
(
prio
),
tables
,
matchexpr
))
params
+=
[
'%'
+
word
+
'%'
]
*
len
(
match
)
prio
-=
1
if
subexprs
==
[]:
return
[]
expr
=
'SELECT *,SUM(_prio) AS _score FROM (%s) AS _tmp %s'
%
(
' UNION '
.
join
(
subexprs
),
suffix
)
return
query
(
expr
,
*
params
,
*
suffixparams
)
LDAP_USERRE
=
re
.
compile
(
r
'[^a-z0-9]'
)
notldap
=
{
'videoag'
:(
'videoag'
,
[
'users'
,
'videoag'
],
{
'uid'
:
'videoag'
,
'givenName'
:
'Video'
,
'sn'
:
'Geier'
}),
'gustav'
:(
'passwort'
,
[
'users'
],
{
'uid'
:
'gustav'
,
'givenName'
:
'Gustav'
,
'sn'
:
'Geier'
})
}
config
.
from_pyfile
(
'config.py'
,
silent
=
True
)
def
ldapauth
(
user
,
password
):
user
=
LDAP_USERRE
.
sub
(
r
''
,
user
.
lower
())
if
'LDAP_HOST'
in
config
:
import
ldap3
try
:
conn
=
ldap3
.
Connection
(
config
[
'LDAP_HOST'
],
'uid=%s,ou=users,dc=fsmpi,dc=rwth-aachen,dc=de'
%
user
,
password
,
auto_bind
=
True
)
if
conn
.
search
(
"ou=groups,dc=fsmpi,dc=rwth-aachen,dc=de"
,
"(&(cn=*)(memberUid=%s))"
%
user
,
attributes
=
[
'cn'
]):
groups
=
[
e
.
cn
.
value
for
e
in
conn
.
entries
]
conn
.
unbind
()
return
user
,
groups
except
ldap3
.
core
.
exceptions
.
LDAPBindError
:
pass
elif
config
.
get
(
'DEBUG'
)
and
user
in
notldap
and
password
==
notldap
[
user
][
0
]:
return
user
,
notldap
[
user
][
1
]
return
None
,
[]
from
db
import
query
,
searchquery
,
ldapauth
,
ldapget
def
ldapget
(
user
):
user
=
LDAP_USERRE
.
sub
(
r
''
,
user
.
lower
())
if
'LDAP_HOST'
in
config
:
import
ldap3
conn
=
ldap3
.
Connection
(
'ldaps://rumo.fsmpi.rwth-aachen.de'
,
auto_bind
=
True
)
conn
.
search
(
"ou=users,dc=fsmpi,dc=rwth-aachen,dc=de"
,
"(uid=%s)"
%
user
,
attributes
=
ldap3
.
ALL_ATTRIBUTES
)
e
=
conn
.
entries
[
0
]
return
{
'uid'
:
user
,
'givenName'
:
e
.
givenName
.
value
,
'sn'
:
e
.
sn
.
value
}
else
:
return
notldap
[
user
][
2
]
app
.
jinja_env
.
globals
[
'videoprefix'
]
=
config
[
'VIDEOPREFIX'
]
mod_endpoints
=
[]
def
ismod
(
*
args
):
return
(
'user'
in
session
)
...
...
@@ -136,7 +41,6 @@ def mod_required(func):
return
redirect
(
url_for
(
'login'
,
ref
=
request
.
url
))
else
:
return
func
(
*
args
,
**
kwargs
)
print
(
decorator
.
__name__
)
return
decorator
app
.
jinja_env
.
globals
[
'navbar'
]
=
[]
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment