Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F97911810
websession_model.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Tue, Jan 7, 11:52
Size
6 KB
Mime Type
text/x-python
Expires
Thu, Jan 9, 11:52 (2 d)
Engine
blob
Format
Raw Data
Handle
23438537
Attached To
R3600 invenio-infoscience
websession_model.py
View Options
# -*- coding: utf-8 -*-
#
## This file is part of Invenio.
## Copyright (C) 2011, 2012 CERN.
##
## Invenio is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 2 of the
## License, or (at your option) any later version.
##
## Invenio is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with Invenio; if not, write to the Free Software Foundation, Inc.,
## 59 Temple Place, Suite 330, Boston, MA 02D111-1307, USA.
"""
WebSession database models.
"""
# General imports.
from
invenio.sqlalchemyutils
import
db
from
sqlalchemy.ext.hybrid
import
hybrid_property
from
invenio.dbquery
import
serialize_via_marshal
,
deserialize_via_marshal
# Create your models here.
#from invenio.webuser import get_default_user_preferences
from
invenio.access_control_config
import
CFG_EXTERNAL_AUTHENTICATION
,
\
CFG_WEBACCESS_MSGS
,
CFG_WEBACCESS_WARNING_MSGS
,
CFG_EXTERNAL_AUTH_DEFAULT
def
get_default_user_preferences
():
user_preference
=
{
'login_method'
:
''
}
if
CFG_EXTERNAL_AUTH_DEFAULT
in
CFG_EXTERNAL_AUTHENTICATION
:
user_preference
[
'login_method'
]
=
CFG_EXTERNAL_AUTH_DEFAULT
return
user_preference
#TODO Consider using standard cPickle or pickle
# (then just remove pickler=MarshalPickle()).
class
MarshalPickle
(
object
):
def
__init__
(
self
,
set_empty
=
False
,
default
=
None
):
self
.
set_empty
=
set_empty
self
.
default
=
default
or
{}
def
dumps
(
self
,
obj
,
protocol
=
None
):
if
obj
is
not
None
:
obj
=
serialize_via_marshal
(
obj
)
elif
self
.
set_empty
:
obj
=
serialize_via_marshal
(
self
.
default
)
return
obj
def
loads
(
self
,
obj
):
try
:
obj
=
deserialize_via_marshal
(
obj
)
except
:
obj
=
self
.
default
return
obj
class
User
(
db
.
Model
):
"""Represents a User record."""
def
__str__
(
self
):
return
"
%s
<
%s
>"
%
(
self
.
nickname
,
self
.
email
)
__tablename__
=
'user'
id
=
db
.
Column
(
db
.
Integer
(
15
,
unsigned
=
True
),
primary_key
=
True
,
autoincrement
=
True
)
email
=
db
.
Column
(
db
.
String
(
255
),
nullable
=
False
,
server_default
=
''
)
_password
=
db
.
Column
(
db
.
LargeBinary
,
name
=
"password"
,
nullable
=
False
)
note
=
db
.
Column
(
db
.
String
(
255
),
nullable
=
True
)
settings
=
db
.
Column
(
db
.
PickleType
(
mutable
=
True
,
pickler
=
MarshalPickle
(
set_empty
=
True
,
default
=
get_default_user_preferences
())),
nullable
=
True
)
nickname
=
db
.
Column
(
db
.
String
(
255
),
nullable
=
False
,
server_default
=
''
)
last_login
=
db
.
Column
(
db
.
DateTime
,
nullable
=
False
,
server_default
=
'0001-01-01 00:00:00'
)
#TODO re_invalid_nickname = re.compile(""".*[,'@]+.*""")
_password_comparator
=
db
.
PasswordComparator
(
_password
)
@hybrid_property
def
password
(
self
):
return
self
.
_password
@password.setter
def
password
(
self
,
password
):
self
.
_password
=
self
.
_password_comparator
.
hash
(
password
)
@password.comparator
def
password
(
self
):
return
self
.
_password_comparator
@property
def
guest
(
self
):
return
False
if
self
.
email
else
True
#
# Basic functions for user authentification.
#
def
get_id
(
self
):
return
self
.
id
def
is_guest
(
self
):
return
self
.
guest
def
is_authenticated
(
self
):
return
True
if
self
.
email
else
False
def
is_active
(
self
):
return
True
class
Usergroup
(
db
.
Model
):
"""Represents a Usergroup record."""
def
__str__
(
self
):
return
"
%s
<
%s
>"
%
(
self
.
name
,
self
.
description
)
__tablename__
=
'usergroup'
id
=
db
.
Column
(
db
.
Integer
(
15
,
unsigned
=
True
),
nullable
=
False
,
primary_key
=
True
,
autoincrement
=
True
)
name
=
db
.
Column
(
db
.
String
(
255
),
nullable
=
False
,
server_default
=
''
)
description
=
db
.
Column
(
db
.
Text
,
nullable
=
True
)
join_policy
=
db
.
Column
(
db
.
CHAR
(
2
),
nullable
=
False
,
server_default
=
''
)
login_method
=
db
.
Column
(
db
.
String
(
255
),
nullable
=
False
,
server_default
=
'INTERNAL'
)
#all_users = db.relationship(User, secondary=lambda: UserUsergroup.__table__,
# collection_class=set)
class
UserUsergroup
(
db
.
Model
):
"""Represents a UserUsergroup record."""
def
__str__
(
self
):
return
"
%s
:
%s
"
%
(
self
.
user
.
nickname
,
self
.
usergroup
.
name
)
__tablename__
=
'user_usergroup'
id_user
=
db
.
Column
(
db
.
Integer
(
15
,
unsigned
=
True
),
db
.
ForeignKey
(
User
.
id
),
nullable
=
False
,
server_default
=
'0'
,
primary_key
=
True
)
id_usergroup
=
db
.
Column
(
db
.
Integer
(
15
,
unsigned
=
True
),
db
.
ForeignKey
(
Usergroup
.
id
),
nullable
=
False
,
server_default
=
'0'
,
primary_key
=
True
)
user_status
=
db
.
Column
(
db
.
CHAR
(
1
),
nullable
=
False
,
server_default
=
''
)
user_status_date
=
db
.
Column
(
db
.
DateTime
,
nullable
=
False
,
server_default
=
'0001-01-01 00:00:00'
)
user
=
db
.
relationship
(
User
,
backref
=
'usergroups'
)
usergroup
=
db
.
relationship
(
Usergroup
,
backref
=
'users'
)
class
Session
(
db
.
Model
):
"""Represents a Session record."""
def
__init__
(
self
):
pass
__tablename__
=
'session'
session_key
=
db
.
Column
(
db
.
String
(
32
),
nullable
=
False
,
server_default
=
''
,
primary_key
=
True
)
session_expiry
=
db
.
Column
(
db
.
DateTime
,
nullable
=
False
,
server_default
=
'0001-01-01 00:00:00'
)
# Use standard binary column.
session_object
=
db
.
Column
(
db
.
LargeBinary
,
nullable
=
True
)
uid
=
db
.
Column
(
db
.
Integer
(
15
,
unsigned
=
True
),
nullable
=
False
)
__all__
=
[
'User'
,
'Usergroup'
,
'UserUsergroup'
,
'Session'
]
Event Timeline
Log In to Comment