aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/db
diff options
context:
space:
mode:
authortilly-Q <nattilypigeonfowl@gmail.com>2013-06-27 14:13:42 -0700
committertilly-Q <nattilypigeonfowl@gmail.com>2013-06-27 14:13:42 -0700
commit9b8ef022ef874304fb3d5aead612ec3b8fb23e9a (patch)
tree72ffe12bae2f975aa19c1cbe4e721bd0b22d5058 /mediagoblin/db
parent30a9fe7c1cf128fdf413797a2b2edac2d5439bc2 (diff)
downloadmediagoblin-9b8ef022ef874304fb3d5aead612ec3b8fb23e9a.tar.lz
mediagoblin-9b8ef022ef874304fb3d5aead612ec3b8fb23e9a.tar.xz
mediagoblin-9b8ef022ef874304fb3d5aead612ec3b8fb23e9a.zip
In this commit, I have made a few changes and tightened up some of my models
code. I added in two major pieces of functionality: table foundations and a decorator to confirm whether or not a user is a member of a certain group. Table Foundations are default rows that should be present in a given table as soon as the database is initialized. For example, I am using these to populate the core__groups table with all of the necessary groups ('moderator', 'com- menter', etc). Right now, this is achieved by adding a dictionary of parameters (with the parameters as lists) to the constant FOUNDATIONS in mediagoblin.db.models. The keys to this dictionary are uninstantiated classes. The classes which require foundations also have must have a constructor so that the list of parameters can be passed appropriately like so: Model(*parameters) In order to implement these foundations, I added the method populate_table_fou- -ndations to MigrationManager in mediagoblin.db.migration_tools. The decorator, called user_in_group, accepts as a parameter a unicode string, and then decides whether to redirect to 403 or let the user access the page. The identifier is the Group.group_name string, because I believe that will allow for the most readable code. I also added in the simple decorator require_admin_login. In terms of tightening up my code, I made many minor changes to my use of white space and made a few small documentation additions. I removed a vestigial class (ReportForm) from mediagoblin.user_pages.forms. I moved all of my migrations in- to one registered Migration. Setting up Foundations ============================== --\ mediagoblin/db/migration_tools.py --| created: MigrationManager.populate_table_foundations --| modified: MigrationManager.init_or_migrate to run | self.populate_table_foundations on init --\ mediagoblin/db/models.py --| created: FOUNDATIONS ----| created: group_foundations Working With Permissions ============================== --\ mediagoblin/decorators.py --| created: user_in_group --| created: require_admin_login --\ mediagoblin/user_pages/views.py --| modified: added decorator user_in_group to file_a_report --\ mediagoblin/admin/views.py --| modified: added decorator require_admin_login to all views functions General Code Tidying ============================= --/ mediagoblin/admin/views.py --/ mediagoblin/user_pages/forms.py --/ mediagoblin/db/models.py --/ mediagoblin/user_pages/lib.py --/ mediagoblin/user_pages/views.py --/ mediagoblin/db/migrations.py
Diffstat (limited to 'mediagoblin/db')
-rw-r--r--mediagoblin/db/migration_tools.py17
-rw-r--r--mediagoblin/db/migrations.py37
-rw-r--r--mediagoblin/db/models.py85
3 files changed, 94 insertions, 45 deletions
diff --git a/mediagoblin/db/migration_tools.py b/mediagoblin/db/migration_tools.py
index c0c7e998..f100f47e 100644
--- a/mediagoblin/db/migration_tools.py
+++ b/mediagoblin/db/migration_tools.py
@@ -140,6 +140,17 @@ class MigrationManager(object):
self.session.bind,
tables=[model.__table__ for model in self.models])
+ def populate_table_foundations(self):
+ """
+ Create the table foundations (default rows) as layed out in FOUNDATIONS
+ in mediagoblin.db.models
+ """
+ from mediagoblin.db.models import FOUNDATIONS as MAIN_FOUNDATIONS
+ for Model in MAIN_FOUNDATIONS.keys():
+ for parameters in MAIN_FOUNDATIONS[Model]:
+ row = Model(*parameters)
+ row.save()
+
def create_new_migration_record(self):
"""
Create a new migration record for this migration set
@@ -203,8 +214,10 @@ class MigrationManager(object):
self.init_tables()
# auto-set at latest migration number
- self.create_new_migration_record()
-
+ self.create_new_migration_record()
+ if self.name==u'__main__':
+ self.populate_table_foundations()
+
self.printer(u"done.\n")
self.set_current_migration()
return u'inited'
diff --git a/mediagoblin/db/migrations.py b/mediagoblin/db/migrations.py
index 110a48d4..5e9a71d4 100644
--- a/mediagoblin/db/migrations.py
+++ b/mediagoblin/db/migrations.py
@@ -314,16 +314,13 @@ class MediaReport_v0(ReportBase_v0):
__tablename__ = 'core__reports_on_media'
__mapper_args__ = {'polymorphic_identity': 'media_report'}
- id = Column('id',Integer, ForeignKey('core__reports.id'),
- primary_key=True)
+ id = Column(
+ 'id',
+ Integer,
+ ForeignKey('core__reports.id'),
+ primary_key=True)
media_entry_id = Column(Integer, ForeignKey(MediaEntry.id), nullable=False)
-@RegisterMigration(11, MIGRATIONS)
-def create_report_tables(db):
- ReportBase_v0.__table__.create(db.bind)
- CommentReport_v0.__table__.create(db.bind)
- MediaReport_v0.__table__.create(db.bind)
- db.commit()
class UserBan_v0(declarative_base()):
__tablename__ = 'core__user_bans'
@@ -334,23 +331,31 @@ class UserBan_v0(declarative_base()):
class Group_v0(declarative_base()):
__tablename__ = 'core__groups'
- id = Column(Integer, nullable=False, primary_key=True)
+ id = Column(Integer, nullable=False, primary_key=True, unique=True)
group_name = Column(Unicode, nullable=False)
class GroupUserAssociation_v0(declarative_base()):
__tablename__ = 'core__group_user_associations'
- group_id = Column('core__group_id', Integer, ForeignKey(User.id), primary_key=True)
- user_id = Column('core__user_id', Integer, ForeignKey(Group.id), primary_key=True)
+ group_id = Column(
+ 'core__group_id',
+ Integer,
+ ForeignKey(User.id),
+ primary_key=True)
+ user_id = Column(
+ 'core__user_id',
+ Integer,
+ ForeignKey(Group.id),
+ primary_key=True)
-
-
-@RegisterMigration(12, MIGRATIONS)
-def create_banned_and_group_tables(db):
+@RegisterMigration(11, MIGRATIONS)
+def create_moderation_tables(db):
+ ReportBase_v0.__table__.create(db.bind)
+ CommentReport_v0.__table__.create(db.bind)
+ MediaReport_v0.__table__.create(db.bind)
UserBan_v0.__table__.create(db.bind)
Group_v0.__table__.create(db.bind)
GroupUserAssociation_v0.__table__.create(db.bind)
db.commit()
-
diff --git a/mediagoblin/db/models.py b/mediagoblin/db/models.py
index f524b220..28e01a85 100644
--- a/mediagoblin/db/models.py
+++ b/mediagoblin/db/models.py
@@ -492,11 +492,13 @@ class ReportBase(Base):
__tablename__ = 'core__reports'
id = Column(Integer, primary_key=True)
reporter_id = Column(Integer, ForeignKey(User.id), nullable=False)
- reporter = relationship(User, backref=backref("reports_filed_by",
- lazy="dynamic",
- cascade="all, delete-orphan"))
+ reporter = relationship(
+ User,
+ backref=backref("reports_filed_by",
+ lazy="dynamic",
+ cascade="all, delete-orphan"))
report_content = Column(UnicodeText)
- created = Column(DateTime, nullable=False, default=datetime.datetime.now())
+ created = Column(DateTime, nullable=False, default=datetime.datetime.now())
resolved = Column(DateTime)
discriminator = Column('type', Unicode(50))
__mapper_args__ = {'polymorphic_on': discriminator}
@@ -512,9 +514,10 @@ class CommentReport(ReportBase):
id = Column('id',Integer, ForeignKey('core__reports.id'),
primary_key=True)
comment_id = Column(Integer, ForeignKey(MediaComment.id), nullable=False)
- comment = relationship(MediaComment, backref=backref("reports_filed_on",
- lazy="dynamic",
- cascade="all, delete-orphan"))
+ comment = relationship(
+ MediaComment, backref=backref("reports_filed_on",
+ lazy="dynamic",
+ cascade="all, delete-orphan"))
class MediaReport(ReportBase):
"""
@@ -526,27 +529,32 @@ class MediaReport(ReportBase):
id = Column('id',Integer, ForeignKey('core__reports.id'),
primary_key=True)
media_entry_id = Column(Integer, ForeignKey(MediaEntry.id), nullable=False)
- media_entry = relationship(MediaEntry, backref=backref("reports_filed_on",
- lazy="dynamic",
- cascade="all, delete-orphan"))
+ media_entry = relationship(
+ MediaEntry,
+ backref=backref("reports_filed_on",
+ lazy="dynamic",
+ cascade="all, delete-orphan"))
class UserBan(Base):
"""
- Holds the information on a specific user's ban-state. As long as one of these
- is attached to a user, they are banned from accessing mediagoblin. When they
- try to log in, they are greeted with a page that tells them the reason why
- they are banned and when (if ever) the ban will be lifted
- :param user_id Holds the id of the user this object is attached to.
- This should be a one-to-one relationship.
- :param expiration_date Holds the date that the ban will be lifted. If this
- is null, the ban is permanent unless a moderator
- manually lifts it.
+ Holds the information on a specific user's ban-state. As long as one of
+ these is attached to a user, they are banned from accessing mediagoblin.
+ When they try to log in, they are greeted with a page that tells them
+ the reason why they are banned and when (if ever) the ban will be
+ lifted
+
+ :param user_id Holds the id of the user this object is
+ attached to. This is a one-to-one
+ relationship.
+ :param expiration_date Holds the date that the ban will be lifted.
+ If this is null, the ban is permanent
+ unless a moderator manually lifts it.
:param reason Holds the reason why the user was banned.
"""
__tablename__ = 'core__user_bans'
- user_id = Column('id',Integer, ForeignKey(User.id), nullable=False,
- primary_key=True)
+ user_id = Column(Integer, ForeignKey(User.id), nullable=False,
+ primary_key=True)
expiration_date = Column(DateTime)
reason = Column(UnicodeText, nullable=False)
@@ -555,8 +563,14 @@ class Group(Base):
__tablename__ = 'core__groups'
id = Column(Integer, nullable=False, primary_key=True)
- group_name = Column(Unicode, nullable=False)
- all_users = relationship(User, backref='all_groups', secondary="core__group_user_associations")
+ group_name = Column(Unicode, nullable=False, unique=True)
+ all_users = relationship(
+ User,
+ backref='all_groups',
+ secondary="core__group_user_associations")
+
+ def __init__(self, group_name):
+ self.group_name = group_name
def __repr__(self):
return "<Group %s>" % (self.group_name)
@@ -564,14 +578,31 @@ class Group(Base):
class GroupUserAssociation(Base):
__tablename__ = 'core__group_user_associations'
- group_id = Column('core__group_id', Integer, ForeignKey(User.id), primary_key=True)
- user_id = Column('core__user_id', Integer, ForeignKey(Group.id), primary_key=True)
+ group_id = Column(
+ 'core__group_id',
+ Integer,
+ ForeignKey(User.id),
+ primary_key=True)
+ user_id = Column(
+ 'core__user_id',
+ Integer,
+ ForeignKey(Group.id),
+ primary_key=True)
+group_foundations = [[u'admin'], [u'moderator'], [u'commenter'], [u'uploader'],[u'reporter'],[u'active']]
MODELS = [
- User, MediaEntry, Tag, MediaTag, MediaComment, Collection, CollectionItem, MediaFile, FileKeynames,
- MediaAttachmentFile, ProcessingMetaData, CommentReport, MediaReport, UserBan, Group, GroupUserAssociation]
+ User, MediaEntry, Tag, MediaTag, MediaComment, Collection, CollectionItem,
+ MediaFile, FileKeynames, MediaAttachmentFile, ProcessingMetaData, ReportBase,
+ CommentReport, MediaReport, UserBan, Group, GroupUserAssociation]
+
+# Foundations are the default rows that are created immediately after the tables are initialized. Each entry to
+# this dictionary should be in the format of
+# ModelObject:List of Rows
+# (Each Row must be a list of parameters that can create and instance of the ModelObject)
+#
+FOUNDATIONS = {Group:group_foundations}
######################################################
# Special, migrations-tracking table