Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
website
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Container registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Video AG Infrastruktur
website
Commits
63893c5e
Commit
63893c5e
authored
8 years ago
by
Julian Rother
Browse files
Options
Downloads
Patches
Plain Diff
Commit changes to server.py after moving parts to db.py
parent
bb104174
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
server.py
+7
-103
7 additions, 103 deletions
server.py
with
7 additions
and
103 deletions
server.py
+
7
−
103
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
'
]
=
[]
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment