-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
user preferences: use JSON type for 'value' column, no need for _valu…
…e column+properties. Added 'preferences' backref to User model
- Loading branch information
Bertrand Mathieu
committed
Jun 17, 2013
1 parent
3784805
commit ed1a9cb
Showing
3 changed files
with
18 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,31 @@ | ||
""" | ||
Models for user preferences. | ||
""" | ||
import json | ||
from __future__ import absolute_import | ||
|
||
from sqlalchemy import Column, Integer, String, ForeignKey, Unicode | ||
from sqlalchemy.orm import relation | ||
from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint | ||
from sqlalchemy.orm import relation, backref | ||
|
||
from abilian.core.extensions import db | ||
from abilian.core.subjects import User | ||
|
||
from abilian.core.sqlalchemy import JSON | ||
|
||
class UserPreference(db.Model): | ||
"""An atom of user preference.""" | ||
|
||
__tablename__ = 'user_preference' | ||
__table_args__ = (UniqueConstraint('user_id', 'key'),) | ||
|
||
#: Unique id for this preference. | ||
id = Column(Integer, primary_key=True, autoincrement=True) | ||
|
||
#: The user who set this preference. | ||
user = relation(User) | ||
user = relation( | ||
User, backref=backref('preferences', cascade="all, delete, delete-orphan") | ||
) | ||
user_id = Column(ForeignKey(User.id)) | ||
|
||
#: The key | ||
key = Column(String, nullable=False) | ||
|
||
#: The value | ||
_value = Column(Unicode, nullable=False) | ||
|
||
@property | ||
def value(self): | ||
return json.loads(self._value) | ||
|
||
@value.setter | ||
def value(self, value): | ||
self._value = unicode(json.dumps(value)) | ||
|
||
# TODO: use JSON to serialize values? | ||
value = Column(JSON, nullable=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters