From cbfdcad6d222a01b5b1216a7e849d1e23a8f5cbb Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Oct 14 2016 10:05:57 +0000 Subject: Add a _settings field in the user table This will be used to store global user's settings --- diff --git a/alembic/versions/5083efccac7_create_the__settings_fields_for_user.py b/alembic/versions/5083efccac7_create_the__settings_fields_for_user.py new file mode 100644 index 0000000..ed77f87 --- /dev/null +++ b/alembic/versions/5083efccac7_create_the__settings_fields_for_user.py @@ -0,0 +1,29 @@ +"""Create the _settings fields for user + +Revision ID: 5083efccac7 +Revises: 26af5c3602a0 +Create Date: 2016-10-13 16:21:08.716951 + +""" + +# revision identifiers, used by Alembic. +revision = '5083efccac7' +down_revision = '26af5c3602a0' + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + ''' Add the column notifications to the table users. + ''' + op.add_column( + 'users', + sa.Column('_settings', sa.Text, nullable=True) + ) + + +def downgrade(): + ''' Add the column notifications to the table users. + ''' + op.drop_column('users', '_settings') diff --git a/pagure/lib/model.py b/pagure/lib/model.py index ba58a11..08851fa 100644 --- a/pagure/lib/model.py +++ b/pagure/lib/model.py @@ -177,6 +177,7 @@ class User(BASE): fullname = sa.Column(sa.String(255), nullable=False, index=True) public_ssh_key = sa.Column(sa.Text, nullable=True) default_email = sa.Column(sa.Text, nullable=False) + _settings = sa.Column(sa.Text, nullable=True) password = sa.Column(sa.Text, nullable=True) token = sa.Column(sa.String(50), nullable=True) @@ -210,6 +211,32 @@ class User(BASE): ''' Return the list of Group.group_name in which the user is. ''' return [group.group_name for group in self.group_objs] + @property + def settings(self): + """ Return the dict stored as string in the database as an actual + dict object. + """ + default = { + 'cc_me_to_my_actions': False, + } + + if self._settings: + current = json.loads(self._settings) + # Update the current dict with the new keys + for key in default: + if key not in current: + current[key] = default[key] + elif str(current[key]).lower() in ['true', 'y']: + current[key] = True + return current + else: + return default + + @settings.setter + def settings(self, settings): + ''' Ensures the settings are properly saved. ''' + self._settings = json.dumps(settings) + def __repr__(self): ''' Return a string representation of this object. '''