Clean up & Add support to update objects in feed API

This commit is contained in:
Jessica Tallon 2014-07-08 21:27:43 +01:00
parent c3b89febc0
commit 6781ff3cb1
2 changed files with 93 additions and 28 deletions

View File

@ -64,7 +64,7 @@ def uploads(request):
file_data = FileStorage( file_data = FileStorage(
stream=io.BytesIO(request.data), stream=io.BytesIO(request.data),
filename=filename, filename=filename,
content_type=request.headers.get("Content-Type", "application/octal-stream") content_type=mimetype
) )
# Find media manager # Find media manager
@ -90,9 +90,12 @@ def feed(request):
return json_response({"error": error}, status=404) return json_response({"error": error}, status=404)
request.user = requested_user[0] request.user = requested_user[0]
if request.data:
if request.method == "POST":
data = json.loads(request.data) data = json.loads(request.data)
else:
data = {"verb": None, "object": {}}
if request.method == "POST" and data["verb"] == "post":
obj = data.get("object", None) obj = data.get("object", None)
if obj is None: if obj is None:
error = {"error": "Could not find 'object' element."} error = {"error": "Could not find 'object' element."}
@ -139,12 +142,74 @@ def feed(request):
error = {"error": error_message} error = {"error": error_message}
return json_response(error, status=400) return json_response(error, status=400)
elif request.method in ["PUT", "POST"] and data["verb"] == "update":
# Check we've got a valid object
obj = data.get("object", None)
if obj is None:
error = {"error": "Could not find 'object' element."}
return json_response(error, status=400)
if "objectType" not in obj:
error = {"error": "No objectType specified."}
return json_response(error, status=400)
if "id" not in obj:
error = {"error": "Object ID has not been specified."}
return json_response(error, status=400)
obj_id = obj["id"]
# Now try and find object
if obj["objectType"] == "comment":
comment = MediaComment.query.filter_by(id=obj_id)
if comment is None:
error = {"error": "No such 'comment' with id '{0}'.".format(obj_id)}
return json_response(error, status=400)
comment = comment[0]
# TODO: refactor this out to update/setting method on MediaComment
if obj.get("content", None) is not None:
comment.content = obj["content"]
comment.save()
activity = {
"verb": "update",
"object": comment.serialize(request),
}
return json_response(activity)
elif obj["objectType"] == "image":
image = MediaEntry.query.filter_by(id=obj_id)
if image is None:
error = {"error": "No such 'image' with the id '{0}'.".format(obj_id)}
return json_response(error, status=400)
image = image[0]
# TODO: refactor this out to update/setting method on MediaEntry
if obj.get("displayName", None) is not None:
image.title = obj["displayName"]
if obj.get("content", None) is not None:
image.description = obj["content"]
if obj.get("license", None) is not None:
# I think we might need some validation here
image.license = obj["license"]
image.save()
activity = {
"verb": "update",
"object": image.serialize(request),
}
return json_response(activity)
feed_url = request.urlgen( feed_url = request.urlgen(
"mediagoblin.federation.feed", "mediagoblin.federation.feed",
username=request.user.username, username=request.user.username,
qualified=True qualified=True
) )
feed = { feed = {
"displayName": "Activities by {user}@{host}".format( "displayName": "Activities by {user}@{host}".format(
@ -191,17 +256,17 @@ def feed(request):
@oauth_required @oauth_required
def object(request, raw_obj=False): def object(request, raw_obj=False):
""" Lookup for a object type """ """ Lookup for a object type """
objectType = request.matchdict["objectType"] object_type = request.matchdict["objectType"]
uuid = request.matchdict["uuid"] uuid = request.matchdict["uuid"]
if objectType not in ["image"]: if object_type not in ["image"]:
error = "Unknown type: {0}".format(objectType) error = "Unknown type: {0}".format(object_type)
# not sure why this is 404, maybe ask evan. Maybe 400? # not sure why this is 404, maybe ask evan. Maybe 400?
return json_response({"error": error}, status=404) return json_response({"error": error}, status=404)
media = MediaEntry.query.filter_by(slug=uuid).first() media = MediaEntry.query.filter_by(slug=uuid).first()
if media is None: if media is None:
# no media found with that uuid # no media found with that uuid
error = "Can't find a {0} with ID = {1}".format(objectType, uuid) error = "Can't find a {0} with ID = {1}".format(object_type, uuid)
return json_response({"error": error}, status=404) return json_response({"error": error}, status=404)
if raw_obj: if raw_obj:
@ -217,14 +282,16 @@ def object_comments(request):
if isinstance(response, MediaEntry): if isinstance(response, MediaEntry):
comments = response.serialize(request) comments = response.serialize(request)
comments = comments.get("replies", { comments = comments.get("replies", {
"totalItems": 0, "totalItems": 0,
"items": [], "items": [],
"url": request.urlgen( "url": request.urlgen(
"mediagoblin.federation.object.comments", "mediagoblin.federation.object.comments",
objectType=media.objectType, objectType=media.objectType,
uuid=media.slug, uuid=media.slug,
qualified=True) qualified=True
}) )
})
comments["displayName"] = "Replies to {0}".format(comments["url"]) comments["displayName"] = "Replies to {0}".format(comments["url"])
comments["links"] = { comments["links"] = {
"first": comments["url"], "first": comments["url"],

View File

@ -15,12 +15,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from oauthlib.common import Request from oauthlib.common import Request
from oauthlib.oauth1 import RequestValidator from oauthlib.oauth1 import RequestValidator
from mediagoblin.db.models import NonceTimestamp, Client, RequestToken, AccessToken from mediagoblin.db.models import NonceTimestamp, Client, RequestToken, AccessToken
class GMGRequestValidator(RequestValidator): class GMGRequestValidator(RequestValidator):
enforce_ssl = False enforce_ssl = False
@ -63,14 +61,14 @@ class GMGRequestValidator(RequestValidator):
""" Currently a stub - called when making AccessTokens """ """ Currently a stub - called when making AccessTokens """
return list() return list()
def validate_timestamp_and_nonce(self, client_key, timestamp, def validate_timestamp_and_nonce(self, client_key, timestamp,
nonce, request, request_token=None, nonce, request, request_token=None,
access_token=None): access_token=None):
nc = NonceTimestamp.query.filter_by(timestamp=timestamp, nonce=nonce) nc = NonceTimestamp.query.filter_by(timestamp=timestamp, nonce=nonce)
nc = nc.first() nc = nc.first()
if nc is None: if nc is None:
return True return True
return False return False
def validate_client_key(self, client_key, request): def validate_client_key(self, client_key, request):
@ -78,7 +76,7 @@ class GMGRequestValidator(RequestValidator):
client = Client.query.filter_by(id=client_key).first() client = Client.query.filter_by(id=client_key).first()
if client is None: if client is None:
return False return False
return True return True
def validate_access_token(self, client_key, token, request): def validate_access_token(self, client_key, token, request):
@ -119,9 +117,9 @@ class GMGRequest(Request):
""" """
def __init__(self, request, *args, **kwargs): def __init__(self, request, *args, **kwargs):
""" """
:param request: werkzeug request object :param request: werkzeug request object
any extra params are passed to oauthlib.common.Request object any extra params are passed to oauthlib.common.Request object
""" """
kwargs["uri"] = kwargs.get("uri", request.url) kwargs["uri"] = kwargs.get("uri", request.url)