From 5b1a7bae3c8e56ea9b512dcbba6b8a512304a956 Mon Sep 17 00:00:00 2001 From: Michele Azzolari Date: Wed, 11 Jan 2012 15:48:37 +0100 Subject: [PATCH 1/7] Added PuSH capability --- mediagoblin/config_spec.ini | 3 +++ mediagoblin/submit/views.py | 14 ++++++++++++++ mediagoblin/user_pages/views.py | 20 ++++++++++++++------ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/mediagoblin/config_spec.ini b/mediagoblin/config_spec.ini index eb22bc1b..226356d9 100644 --- a/mediagoblin/config_spec.ini +++ b/mediagoblin/config_spec.ini @@ -50,6 +50,9 @@ allow_attachments = boolean(default=False) # Cookie stuff csrf_cookie_name = string(default='mediagoblin_csrftoken') +# Push stuff +push_enabled = boolean(default=True) +push_url = string(default='https://pubsubhubbub.appspot.com/') [storage:publicstore] storage_class = string(default="mediagoblin.storage.filestorage:BasicFileStorage") diff --git a/mediagoblin/submit/views.py b/mediagoblin/submit/views.py index dd273c7f..d5aa60fa 100644 --- a/mediagoblin/submit/views.py +++ b/mediagoblin/submit/views.py @@ -20,6 +20,7 @@ from os.path import splitext from cgi import FieldStorage from celery import registry +import urllib,urllib2 from werkzeug.utils import secure_filename @@ -125,6 +126,19 @@ def submit_start(request): # re-raise the exception raise + if mg_globals.app_config["push_enabled"]: + feed_url=request.urlgen( + 'mediagoblin.user_pages.atom_feed', + qualified=True,user=request.user.username) + hubparameters = { + 'hub.mode': 'publish', + 'hub.url': feed_url} + huburl = mg_globals.app_config["push_url"] + hubdata = urllib.urlencode(hubparameters) + hubheaders = {"Content-type": "application/x-www-form-urlencoded"} + hubrequest = urllib2.Request(huburl, hubdata,hubheaders) + hubresponse = urllib2.urlopen(hubrequest) + add_message(request, SUCCESS, _('Woohoo! Submitted!')) return redirect(request, "mediagoblin.user_pages.user_home", diff --git a/mediagoblin/user_pages/views.py b/mediagoblin/user_pages/views.py index a234722f..ee7cfe0f 100644 --- a/mediagoblin/user_pages/views.py +++ b/mediagoblin/user_pages/views.py @@ -228,16 +228,24 @@ def atom_feed(request): """ ATOM feed id is a tag URI (see http://en.wikipedia.org/wiki/Tag_URI) """ + atomlinks = [{ + 'href': request.urlgen( + 'mediagoblin.user_pages.user_home', + qualified=True,user=request.matchdict['user']), + 'rel': 'alternate', + 'type': 'text/html' + }]; + if mg_globals.app_config["push_enabled"]: + atomlinks.append({ + 'rel': 'hub', + 'href': mg_globals.app_config["push_url"]}) + feed = AtomFeed( "MediaGoblin: Feed for user '%s'" % request.matchdict['user'], feed_url=request.url, id='tag:'+request.host+',2011:gallery.user-'+request.matchdict['user'], - links=[{ - 'href': request.urlgen( - 'mediagoblin.user_pages.user_home', - qualified=True,user=request.matchdict['user']), - 'rel': 'alternate', - 'type': 'text/html'}]) + links=atomlinks) + for entry in cursor: feed.add(entry.get('title'), From f502a89b6dad993a44088a84c9f441fdc74189f8 Mon Sep 17 00:00:00 2001 From: Michele Azzolari Date: Wed, 11 Jan 2012 16:11:23 +0100 Subject: [PATCH 2/7] Default is now PuSH disabled --- mediagoblin/config_spec.ini | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mediagoblin/config_spec.ini b/mediagoblin/config_spec.ini index 226356d9..d692f205 100644 --- a/mediagoblin/config_spec.ini +++ b/mediagoblin/config_spec.ini @@ -51,8 +51,8 @@ allow_attachments = boolean(default=False) csrf_cookie_name = string(default='mediagoblin_csrftoken') # Push stuff -push_enabled = boolean(default=True) -push_url = string(default='https://pubsubhubbub.appspot.com/') +push_enabled = boolean(default=False) +push_url = string(default='') [storage:publicstore] storage_class = string(default="mediagoblin.storage.filestorage:BasicFileStorage") From 7f251b037bd5207652ca73f556e90b9633786a3c Mon Sep 17 00:00:00 2001 From: Michele Azzolari Date: Thu, 12 Jan 2012 00:00:28 +0100 Subject: [PATCH 3/7] As suggested by Elrond, we use only one setting --- mediagoblin/config_spec.ini | 1 - mediagoblin/submit/views.py | 2 +- mediagoblin/user_pages/views.py | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/mediagoblin/config_spec.ini b/mediagoblin/config_spec.ini index d692f205..b8e7b193 100644 --- a/mediagoblin/config_spec.ini +++ b/mediagoblin/config_spec.ini @@ -51,7 +51,6 @@ allow_attachments = boolean(default=False) csrf_cookie_name = string(default='mediagoblin_csrftoken') # Push stuff -push_enabled = boolean(default=False) push_url = string(default='') [storage:publicstore] diff --git a/mediagoblin/submit/views.py b/mediagoblin/submit/views.py index d5aa60fa..de280422 100644 --- a/mediagoblin/submit/views.py +++ b/mediagoblin/submit/views.py @@ -126,7 +126,7 @@ def submit_start(request): # re-raise the exception raise - if mg_globals.app_config["push_enabled"]: + if mg_globals.app_config["push_url"]: feed_url=request.urlgen( 'mediagoblin.user_pages.atom_feed', qualified=True,user=request.user.username) diff --git a/mediagoblin/user_pages/views.py b/mediagoblin/user_pages/views.py index ee7cfe0f..2d4eac69 100644 --- a/mediagoblin/user_pages/views.py +++ b/mediagoblin/user_pages/views.py @@ -235,7 +235,7 @@ def atom_feed(request): 'rel': 'alternate', 'type': 'text/html' }]; - if mg_globals.app_config["push_enabled"]: + if mg_globals.app_config["push_url"]: atomlinks.append({ 'rel': 'hub', 'href': mg_globals.app_config["push_url"]}) From bb025ebda14297b721f8816d13980a477f62bca6 Mon Sep 17 00:00:00 2001 From: Michele Azzolari Date: Thu, 12 Jan 2012 11:05:05 +0100 Subject: [PATCH 4/7] As per spec, we permit to have more then 1 hub --- mediagoblin/config_spec.ini | 2 +- mediagoblin/submit/views.py | 12 +++++++----- mediagoblin/user_pages/views.py | 9 +++++---- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/mediagoblin/config_spec.ini b/mediagoblin/config_spec.ini index b8e7b193..dc286a27 100644 --- a/mediagoblin/config_spec.ini +++ b/mediagoblin/config_spec.ini @@ -51,7 +51,7 @@ allow_attachments = boolean(default=False) csrf_cookie_name = string(default='mediagoblin_csrftoken') # Push stuff -push_url = string(default='') +push_urls = string_list(default=list()) [storage:publicstore] storage_class = string(default="mediagoblin.storage.filestorage:BasicFileStorage") diff --git a/mediagoblin/submit/views.py b/mediagoblin/submit/views.py index de280422..65243ca1 100644 --- a/mediagoblin/submit/views.py +++ b/mediagoblin/submit/views.py @@ -126,18 +126,20 @@ def submit_start(request): # re-raise the exception raise - if mg_globals.app_config["push_url"]: + if mg_globals.app_config["push_urls"]: feed_url=request.urlgen( 'mediagoblin.user_pages.atom_feed', qualified=True,user=request.user.username) hubparameters = { 'hub.mode': 'publish', 'hub.url': feed_url} - huburl = mg_globals.app_config["push_url"] hubdata = urllib.urlencode(hubparameters) - hubheaders = {"Content-type": "application/x-www-form-urlencoded"} - hubrequest = urllib2.Request(huburl, hubdata,hubheaders) - hubresponse = urllib2.urlopen(hubrequest) + hubheaders = { + "Content-type": "application/x-www-form-urlencoded", + "Connection": "close"} + for huburl in mg_globals.app_config["push_urls"]: + hubrequest = urllib2.Request(huburl, hubdata,hubheaders) + hubresponse = urllib2.urlopen(hubrequest) add_message(request, SUCCESS, _('Woohoo! Submitted!')) diff --git a/mediagoblin/user_pages/views.py b/mediagoblin/user_pages/views.py index 2d4eac69..29360e23 100644 --- a/mediagoblin/user_pages/views.py +++ b/mediagoblin/user_pages/views.py @@ -235,10 +235,11 @@ def atom_feed(request): 'rel': 'alternate', 'type': 'text/html' }]; - if mg_globals.app_config["push_url"]: - atomlinks.append({ - 'rel': 'hub', - 'href': mg_globals.app_config["push_url"]}) + if mg_globals.app_config["push_urls"]: + for push_url in mg_globals.app_config["push_urls"]: + atomlinks.append({ + 'rel': 'hub', + 'href': push_url}) feed = AtomFeed( "MediaGoblin: Feed for user '%s'" % request.matchdict['user'], From 77b91efcc260cf5f4e7d2b544a02a12c51f45ad4 Mon Sep 17 00:00:00 2001 From: Michele Azzolari Date: Tue, 17 Jan 2012 22:42:36 +0100 Subject: [PATCH 5/7] We handle exceptions if PuSH fails --- mediagoblin/submit/views.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/mediagoblin/submit/views.py b/mediagoblin/submit/views.py index 65243ca1..91498b09 100644 --- a/mediagoblin/submit/views.py +++ b/mediagoblin/submit/views.py @@ -21,6 +21,7 @@ from cgi import FieldStorage from celery import registry import urllib,urllib2 +import logging from werkzeug.utils import secure_filename @@ -131,15 +132,24 @@ def submit_start(request): 'mediagoblin.user_pages.atom_feed', qualified=True,user=request.user.username) hubparameters = { - 'hub.mode': 'publish', - 'hub.url': feed_url} + 'hub.mode': 'publish', + 'hub.url': feed_url} hubdata = urllib.urlencode(hubparameters) hubheaders = { "Content-type": "application/x-www-form-urlencoded", "Connection": "close"} for huburl in mg_globals.app_config["push_urls"]: - hubrequest = urllib2.Request(huburl, hubdata,hubheaders) - hubresponse = urllib2.urlopen(hubrequest) + hubrequest = urllib2.Request(huburl, hubdata, hubheaders) + try: + hubresponse = urllib2.urlopen(hubrequest) + except urllib2.HTTPError as exc: + # This is not a big issue, the item will be fetched + # by the PuSH server next time we hit it + logging.getLogger(__name__).warning( + "push url %r gave error %r", huburl, exc.code) + except urllib2.URLError as exc: + logging.getLogger(__name__).warning( + "push url %r is unreachable %r", huburl, exc.reason) add_message(request, SUCCESS, _('Woohoo! Submitted!')) From c03d13cd791ac41db1be72e8a5d4d2eaa6cc6087 Mon Sep 17 00:00:00 2001 From: Michele Azzolari Date: Tue, 17 Jan 2012 23:15:47 +0100 Subject: [PATCH 6/7] Cleaned the code --- mediagoblin/submit/views.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mediagoblin/submit/views.py b/mediagoblin/submit/views.py index 91498b09..33868785 100644 --- a/mediagoblin/submit/views.py +++ b/mediagoblin/submit/views.py @@ -23,6 +23,8 @@ from celery import registry import urllib,urllib2 import logging +_log = logging.getLogger(__name__) + from werkzeug.utils import secure_filename from mediagoblin.db.util import ObjectId @@ -145,10 +147,10 @@ def submit_start(request): except urllib2.HTTPError as exc: # This is not a big issue, the item will be fetched # by the PuSH server next time we hit it - logging.getLogger(__name__).warning( + _log.warning( "push url %r gave error %r", huburl, exc.code) except urllib2.URLError as exc: - logging.getLogger(__name__).warning( + _log.warning( "push url %r is unreachable %r", huburl, exc.reason) add_message(request, SUCCESS, _('Woohoo! Submitted!')) From 6fc8af3278173a0d1113dc0cf5525c2249f1d0bc Mon Sep 17 00:00:00 2001 From: Elrond Date: Thu, 29 Dec 2011 11:07:58 +0100 Subject: [PATCH 7/7] sql/fake.py: Some fake objects/methods to calm the code sql/fake.py contains some fake classes and functions to calm the rest of the code base. Or provide super minimal implementations. Currently: - ObjectId "class": It's a function mostly doing int(first_arg) to convert string primary keys into integer primary keys. - InvalidId exception - DESCENING "constant" --- mediagoblin/db/sql/fake.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 mediagoblin/db/sql/fake.py diff --git a/mediagoblin/db/sql/fake.py b/mediagoblin/db/sql/fake.py new file mode 100644 index 00000000..ba11bfee --- /dev/null +++ b/mediagoblin/db/sql/fake.py @@ -0,0 +1,28 @@ +""" +This module contains some fake classes and functions to +calm the rest of the code base. Or provide super minimal +implementations. + +Currently: +- ObjectId "class": It's a function mostly doing + int(init_arg) to convert string primary keys into + integer primary keys. +- InvalidId exception +- DESCENDING "constant" +""" + + +DESCENDING = object() # a unique object for this "constant" + + +class InvalidId(Exception): + pass + + +def ObjectId(value=None): + if value is None: + return None + try: + return int(value) + except ValueError: + raise InvalidId("%r is an invalid id" % value)