aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/db
diff options
context:
space:
mode:
Diffstat (limited to 'mediagoblin/db')
-rw-r--r--mediagoblin/db/mixin.py47
-rw-r--r--mediagoblin/db/models.py20
-rw-r--r--mediagoblin/db/util.py2
3 files changed, 39 insertions, 30 deletions
diff --git a/mediagoblin/db/mixin.py b/mediagoblin/db/mixin.py
index 0832c21a..0dc3bc85 100644
--- a/mediagoblin/db/mixin.py
+++ b/mediagoblin/db/mixin.py
@@ -52,10 +52,18 @@ class UserMixin(object):
return cleaned_markdown_conversion(self.bio)
-class MediaEntryMixin(object):
+class GenerateSlugMixin(object):
+ """
+ Mixin to add a generate_slug method to objects.
+
+ Depends on:
+ - self.slug
+ - self.title
+ - self.check_slug_used(new_slug)
+ """
def generate_slug(self):
"""
- Generate a unique slug for this MediaEntry.
+ Generate a unique slug for this object.
This one does not *force* slugs, but usually it will probably result
in a niceish one.
@@ -76,10 +84,6 @@ class MediaEntryMixin(object):
generated bits until it's unique. That'll be a little bit of junk,
but at least it has the basis of a nice slug.
"""
- # import this here due to a cyclic import issue
- # (db.models -> db.mixin -> db.util -> db.models)
- from mediagoblin.db.util import check_media_slug_used
-
#Is already a slug assigned? Check if it is valid
if self.slug:
self.slug = slugify(self.slug)
@@ -100,14 +104,13 @@ class MediaEntryMixin(object):
return # giving up!
# Otherwise, let's see if this is unique.
- if check_media_slug_used(self.uploader, self.slug, self.id):
+ if self.check_slug_used(self.slug):
# It looks like it's being used... lame.
# Can we just append the object's id to the end?
if self.id:
slug_with_id = u"%s-%s" % (self.slug, self.id)
- if not check_media_slug_used(self.uploader,
- slug_with_id, self.id):
+ if not self.check_slug_used(slug_with_id):
self.slug = slug_with_id
return # success!
@@ -115,9 +118,18 @@ class MediaEntryMixin(object):
# let's whack junk on there till it's unique.
self.slug += '-' + uuid.uuid4().hex[:4]
# keep going if necessary!
- while check_media_slug_used(self.uploader, self.slug, self.id):
+ while self.check_slug_used(self.slug):
self.slug += uuid.uuid4().hex[:4]
+
+class MediaEntryMixin(GenerateSlugMixin):
+ def check_slug_used(self, slug):
+ # import this here due to a cyclic import issue
+ # (db.models -> db.mixin -> db.util -> db.models)
+ from mediagoblin.db.util import check_media_slug_used
+
+ return check_media_slug_used(self.uploader, slug, self.id)
+
@property
def description_html(self):
"""
@@ -238,22 +250,13 @@ class MediaCommentMixin(object):
return cleaned_markdown_conversion(self.content)
-class CollectionMixin(object):
- def generate_slug(self):
+class CollectionMixin(GenerateSlugMixin):
+ def check_slug_used(self, slug):
# import this here due to a cyclic import issue
# (db.models -> db.mixin -> db.util -> db.models)
from mediagoblin.db.util import check_collection_slug_used
- self.slug = slugify(self.title)
-
- duplicate = check_collection_slug_used(mg_globals.database,
- self.creator, self.slug, self.id)
-
- if duplicate:
- if self.id is not None:
- self.slug = u"%s-%s" % (self.id, self.slug)
- else:
- self.slug = None
+ return check_collection_slug_used(self.creator, slug, self.id)
@property
def description_html(self):
diff --git a/mediagoblin/db/models.py b/mediagoblin/db/models.py
index 2f58503f..fcfd0f61 100644
--- a/mediagoblin/db/models.py
+++ b/mediagoblin/db/models.py
@@ -172,8 +172,7 @@ class MediaEntry(Base, MediaEntryMixin):
order_col = MediaComment.created
if not ascending:
order_col = desc(order_col)
- return MediaComment.query.filter_by(
- media_entry=self.id).order_by(order_col)
+ return self.all_comments.order_by(order_col)
def url_to_prev(self, urlgen):
"""get the next 'newer' entry by this user"""
@@ -238,9 +237,7 @@ class MediaEntry(Base, MediaEntryMixin):
:param del_orphan_tags: True/false if we delete unused Tags too
:param commit: True/False if this should end the db transaction"""
# User's CollectionItems are automatically deleted via "cascade".
- # Delete all the associated comments
- for comment in self.get_comments():
- comment.delete(commit=False)
+ # Comments on this Media are deleted by cascade, hopefully.
# Delete all related files/attachments
try:
@@ -385,13 +382,22 @@ class MediaComment(Base, MediaCommentMixin):
content = Column(UnicodeText, nullable=False)
# Cascade: Comments are owned by their creator. So do the full thing.
- # lazy=dynamic: People might post a *lot* of comments, so make
- # the "posted_comments" a query-like thing.
+ # lazy=dynamic: People might post a *lot* of comments,
+ # so make the "posted_comments" a query-like thing.
get_author = relationship(User,
backref=backref("posted_comments",
lazy="dynamic",
cascade="all, delete-orphan"))
+ # Cascade: Comments are somewhat owned by their MediaEntry.
+ # So do the full thing.
+ # lazy=dynamic: MediaEntries might have many comments,
+ # so make the "all_comments" a query-like thing.
+ get_media_entry = relationship(MediaEntry,
+ backref=backref("all_comments",
+ lazy="dynamic",
+ cascade="all, delete-orphan"))
+
class Collection(Base, CollectionMixin):
"""An 'album' or 'set' of media by a user.
diff --git a/mediagoblin/db/util.py b/mediagoblin/db/util.py
index 529ef8b9..6ffec44d 100644
--- a/mediagoblin/db/util.py
+++ b/mediagoblin/db/util.py
@@ -59,7 +59,7 @@ def clean_orphan_tags(commit=True):
Session.commit()
-def check_collection_slug_used(dummy_db, creator_id, slug, ignore_c_id):
+def check_collection_slug_used(creator_id, slug, ignore_c_id):
filt = (Collection.creator == creator_id) \
& (Collection.slug == slug)
if ignore_c_id is not None: