diff options
Diffstat (limited to 'mediagoblin/db')
-rw-r--r-- | mediagoblin/db/__init__.py | 2 | ||||
-rw-r--r-- | mediagoblin/db/indexes.py | 5 | ||||
-rw-r--r-- | mediagoblin/db/migrations.py | 41 | ||||
-rw-r--r-- | mediagoblin/db/models.py | 63 | ||||
-rw-r--r-- | mediagoblin/db/open.py | 2 | ||||
-rw-r--r-- | mediagoblin/db/util.py | 7 |
6 files changed, 61 insertions, 59 deletions
diff --git a/mediagoblin/db/__init__.py b/mediagoblin/db/__init__.py index c5124b1a..27e8a90f 100644 --- a/mediagoblin/db/__init__.py +++ b/mediagoblin/db/__init__.py @@ -23,7 +23,7 @@ Database Abstraction/Wrapper Layer pymongo. Read beow for why, but note that nobody is actually doing this and there's no proof that we'll ever support more than MongoDB... it would be a huge amount of work to do so. - + If you really want to prove that possible, jump on IRC and talk to us about making such a branch. In the meanwhile, it doesn't hurt to have things as they are... if it ever makes it hard for us to diff --git a/mediagoblin/db/indexes.py b/mediagoblin/db/indexes.py index 75394a31..1dd73f2b 100644 --- a/mediagoblin/db/indexes.py +++ b/mediagoblin/db/indexes.py @@ -93,8 +93,9 @@ MEDIAENTRY_INDEXES = { ('created', DESCENDING)]}, 'state_uploader_tags_created': { - # Indexing on processed?, media uploader, associated tags, and timestamp - # Used for showing media items matching a tag search, most recent first. + # Indexing on processed?, media uploader, associated tags, and + # timestamp Used for showing media items matching a tag + # search, most recent first. 'index': [('state', ASCENDING), ('uploader', ASCENDING), ('tags.slug', DESCENDING), diff --git a/mediagoblin/db/migrations.py b/mediagoblin/db/migrations.py index 01df7208..cfc01287 100644 --- a/mediagoblin/db/migrations.py +++ b/mediagoblin/db/migrations.py @@ -15,7 +15,18 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. from mediagoblin.db.util import RegisterMigration -from mediagoblin.util import cleaned_markdown_conversion +from mediagoblin.tools.text import cleaned_markdown_conversion + + +def add_table_field(db, table_name, field_name, default_value): + """ + Add a new field to the table/collection named table_name. + The field will have the name field_name and the value default_value + """ + db[table_name].update( + {field_name: {'$exists': False}}, + {'$set': {field_name: default_value}}, + multi=True) # Please see mediagoblin/tests/test_migrations.py for some examples of @@ -70,11 +81,7 @@ def mediaentry_add_queued_task_id(database): """ Add the 'queued_task_id' field for entries that don't have it. """ - collection = database['media_entries'] - collection.update( - {'queued_task_id': {'$exists': False}}, - {'$set': {'queued_task_id': None}}, - multi=True) + add_table_field(database, 'media_entries', 'queued_task_id', None) @RegisterMigration(5) @@ -82,16 +89,8 @@ def mediaentry_add_fail_error_and_metadata(database): """ Add 'fail_error' and 'fail_metadata' fields to media entries """ - collection = database['media_entries'] - collection.update( - {'fail_error': {'$exists': False}}, - {'$set': {'fail_error': None}}, - multi=True) - - collection.update( - {'fail_metadata': {'$exists': False}}, - {'$set': {'fail_metadata': {}}}, - multi=True) + add_table_field(database, 'media_entries', 'fail_error', None) + add_table_field(database, 'media_entries', 'fail_metadata', {}) @RegisterMigration(6) @@ -99,14 +98,8 @@ def user_add_forgot_password_token_and_expires(database): """ Add token and expiration fields to help recover forgotten passwords """ - database['users'].update( - {'fp_verification_key': {'$exists': False}}, - {'$set': {'fp_verification_key': None}}, - multi=True) - database['users'].update( - {'fp_token_expire': {'$exists': False}}, - {'$set': {'fp_token_expire': None}}, - multi=True) + add_table_field(database, 'users', 'fp_verification_key', None) + add_table_field(database, 'users', 'fp_token_expire', None) @RegisterMigration(7) diff --git a/mediagoblin/db/models.py b/mediagoblin/db/models.py index bbddada6..f13a4457 100644 --- a/mediagoblin/db/models.py +++ b/mediagoblin/db/models.py @@ -14,18 +14,17 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. -import datetime, uuid +import datetime +import uuid from mongokit import Document -from mediagoblin import util from mediagoblin.auth import lib as auth_lib from mediagoblin import mg_globals from mediagoblin.db import migrations from mediagoblin.db.util import ASCENDING, DESCENDING, ObjectId -from mediagoblin.util import Pagination -from mediagoblin.util import DISPLAY_IMAGE_FETCHING_ORDER - +from mediagoblin.tools.pagination import Pagination +from mediagoblin.tools import url, common ################### # Custom validators @@ -64,22 +63,23 @@ class User(Document): - bio_html: biography of the user converted to proper HTML. """ __collection__ = 'users' + use_dot_notation = True structure = { 'username': unicode, 'email': unicode, 'created': datetime.datetime, - 'plugin_data': dict, # plugins can dump stuff here. + 'plugin_data': dict, # plugins can dump stuff here. 'pw_hash': unicode, 'email_verified': bool, 'status': unicode, 'verification_key': unicode, 'is_admin': bool, - 'url' : unicode, - 'bio' : unicode, # May contain markdown - 'bio_html': unicode, # May contain plaintext, or HTML - 'fp_verification_key': unicode, # forgotten password verification key - 'fp_token_expire': datetime.datetime + 'url': unicode, + 'bio': unicode, # May contain markdown + 'bio_html': unicode, # May contain plaintext, or HTML + 'fp_verification_key': unicode, # forgotten password verification key + 'fp_token_expire': datetime.datetime, } required_fields = ['username', 'created', 'pw_hash', 'email'] @@ -174,21 +174,22 @@ class MediaEntry(Document): critical to this piece of media but may be usefully relevant to people viewing the work. (currently unused.) - - fail_error: path to the exception raised - - fail_metadata: + - fail_error: path to the exception raised + - fail_metadata: """ __collection__ = 'media_entries' + use_dot_notation = True structure = { 'uploader': ObjectId, 'title': unicode, 'slug': unicode, 'created': datetime.datetime, - 'description': unicode, # May contain markdown/up - 'description_html': unicode, # May contain plaintext, or HTML + 'description': unicode, # May contain markdown/up + 'description_html': unicode, # May contain plaintext, or HTML 'media_type': unicode, - 'media_data': dict, # extra data relevant to this media_type - 'plugin_data': dict, # plugins can dump stuff here. + 'media_data': dict, # extra data relevant to this media_type + 'plugin_data': dict, # plugins can dump stuff here. 'tags': [dict], 'state': unicode, @@ -216,11 +217,17 @@ class MediaEntry(Document): 'created': datetime.datetime.utcnow, 'state': u'unprocessed'} - def get_comments(self): + def get_comments(self, ascending=False): + if ascending: + order = ASCENDING + else: + order = DESCENDING + return self.db.MediaComment.find({ - 'media_entry': self['_id']}).sort('created', DESCENDING) + 'media_entry': self._id}).sort('created', order) - def get_display_media(self, media_map, fetch_order=DISPLAY_IMAGE_FETCHING_ORDER): + def get_display_media(self, media_map, + fetch_order=common.DISPLAY_IMAGE_FETCHING_ORDER): """ Find the best media for display. @@ -234,7 +241,7 @@ class MediaEntry(Document): """ media_sizes = media_map.keys() - for media_size in DISPLAY_IMAGE_FETCHING_ORDER: + for media_size in common.DISPLAY_IMAGE_FETCHING_ORDER: if media_size in media_sizes: return media_map[media_size] @@ -242,13 +249,13 @@ class MediaEntry(Document): pass def generate_slug(self): - self['slug'] = util.slugify(self['title']) + self['slug'] = url.slugify(self['title']) duplicate = mg_globals.database.media_entries.find_one( {'slug': self['slug']}) if duplicate: - self['slug'] = "%s-%s" % (self['_id'], self['slug']) + self['slug'] = "%s-%s" % (self._id, self['slug']) def url_for_self(self, urlgen): """ @@ -267,13 +274,13 @@ class MediaEntry(Document): return urlgen( 'mediagoblin.user_pages.media_home', user=uploader['username'], - media=unicode(self['_id'])) + media=unicode(self._id)) def url_to_prev(self, urlgen): """ Provide a url to the previous entry from this user, if there is one """ - cursor = self.db.MediaEntry.find({'_id' : {"$gt": self['_id']}, + cursor = self.db.MediaEntry.find({'_id': {"$gt": self._id}, 'uploader': self['uploader'], 'state': 'processed'}).sort( '_id', ASCENDING).limit(1) @@ -286,7 +293,7 @@ class MediaEntry(Document): """ Provide a url to the next entry from this user, if there is one """ - cursor = self.db.MediaEntry.find({'_id' : {"$lt": self['_id']}, + cursor = self.db.MediaEntry.find({'_id': {"$lt": self._id}, 'uploader': self['uploader'], 'state': 'processed'}).sort( '_id', DESCENDING).limit(1) @@ -304,7 +311,7 @@ class MediaEntry(Document): Get the exception that's appropriate for this error """ if self['fail_error']: - return util.import_component(self['fail_error']) + return common.import_component(self['fail_error']) class MediaComment(Document): @@ -321,6 +328,7 @@ class MediaComment(Document): """ __collection__ = 'media_comments' + use_dot_notation = True structure = { 'media_entry': ObjectId, @@ -353,4 +361,3 @@ def register_models(connection): Register all models in REGISTER_MODELS with this connection. """ connection.register(REGISTER_MODELS) - diff --git a/mediagoblin/db/open.py b/mediagoblin/db/open.py index e73b6258..e677ba12 100644 --- a/mediagoblin/db/open.py +++ b/mediagoblin/db/open.py @@ -29,7 +29,7 @@ def connect_database_from_config(app_config, use_pymongo=False): port = app_config.get('db_port') if port: port = asint(port) - + if use_pymongo: connection = pymongo.Connection( app_config.get('db_host'), port) diff --git a/mediagoblin/db/util.py b/mediagoblin/db/util.py index 84a6cbce..52e97f6d 100644 --- a/mediagoblin/db/util.py +++ b/mediagoblin/db/util.py @@ -118,11 +118,12 @@ def remove_deprecated_indexes(database, deprecated_indexes=DEPRECATED_INDEXES): ################# # The default migration registry... -# +# # Don't set this yourself! RegisterMigration will automatically fill # this with stuff via decorating methods in migrations.py -class MissingCurrentMigration(Exception): pass +class MissingCurrentMigration(Exception): + pass MIGRATIONS = {} @@ -147,7 +148,7 @@ class RegisterMigration(object): """ def __init__(self, migration_number, migration_registry=MIGRATIONS): assert migration_number > 0, "Migration number must be > 0!" - assert not migration_registry.has_key(migration_number), \ + assert migration_number not in migration_registry, \ "Duplicate migration numbers detected! That's not allowed!" self.migration_number = migration_number |