From bb9a1c8827c02ac71c16b7d5d0b4c4c18b34f24c Mon Sep 17 00:00:00 2001 From: NavaL Date: Mon, 18 Jan 2016 17:27:19 +0100 Subject: migrating AttachmentsResources to use the service factory Issue #576 --- service/pixelated/resources/__init__.py | 19 ++++++++++++++----- service/pixelated/resources/attachments_resource.py | 15 ++++++++------- service/pixelated/resources/root_resource.py | 2 +- 3 files changed, 23 insertions(+), 13 deletions(-) (limited to 'service/pixelated/resources') diff --git a/service/pixelated/resources/__init__.py b/service/pixelated/resources/__init__.py index 556469c1..cddfff97 100644 --- a/service/pixelated/resources/__init__.py +++ b/service/pixelated/resources/__init__.py @@ -47,10 +47,19 @@ class BaseResource(Resource): Resource.__init__(self) self._services_factory = services_factory - def keymanager(self, request): - user_id = self._get_user_id_from_request() - return self._services_factory.services(user_id).keymanager - - def _get_user_id_from_request(self): + def _get_user_id_from_request(self, request): # currently we are faking this return self._services_factory._services_by_user.keys()[0] + + def _services(self, request): + user_id = self._get_user_id_from_request(request) + return self._services_factory.services(user_id) + + def _service(self, request, attribute): + return getattr(self._services(request), attribute) + + def keymanager(self, request): + return self._service(request, 'keymanager') + + def mail_service(self, request): + return self._service(request, 'mail_service') diff --git a/service/pixelated/resources/attachments_resource.py b/service/pixelated/resources/attachments_resource.py index 09715282..249d268a 100644 --- a/service/pixelated/resources/attachments_resource.py +++ b/service/pixelated/resources/attachments_resource.py @@ -25,7 +25,7 @@ from twisted.web import server from twisted.web.resource import Resource from twisted.web.server import NOT_DONE_YET -from pixelated.resources import respond_json_deferred +from pixelated.resources import respond_json_deferred, BaseResource logger = logging.getLogger(__name__) @@ -72,21 +72,22 @@ class AttachmentResource(Resource): return match.group(1) -class AttachmentsResource(Resource): +class AttachmentsResource(BaseResource): BASE_URL = 'attachment' - def __init__(self, mail_service): - Resource.__init__(self) - self.mail_service = mail_service + def __init__(self, services_factory): + BaseResource.__init__(self, services_factory) def getChild(self, attachment_id, request): - return AttachmentResource(self.mail_service, attachment_id) + _mail_service = self.mail_service(request) + return AttachmentResource(_mail_service, attachment_id) def render_POST(self, request): + _mail_service = self.mail_service(request) fields = cgi.FieldStorage(fp=request.content, headers=(request.getAllHeaders()), environ={'REQUEST_METHOD': 'POST'}) _file = fields['attachment'] - deferred = self.mail_service.save_attachment(_file.value, _file.type) + deferred = _mail_service.save_attachment(_file.value, _file.type) def send_location(attachment_id): request.headers['Location'] = '/%s/%s' % (self.BASE_URL, attachment_id) diff --git a/service/pixelated/resources/root_resource.py b/service/pixelated/resources/root_resource.py index fac63bd7..af3c6c98 100644 --- a/service/pixelated/resources/root_resource.py +++ b/service/pixelated/resources/root_resource.py @@ -41,7 +41,7 @@ class RootResource(Resource): self.putChild('assets', File(self._static_folder)) self.putChild('keys', KeysResource(services_factory)) - self.putChild(AttachmentsResource.BASE_URL, AttachmentsResource(mail_service)) + self.putChild(AttachmentsResource.BASE_URL, AttachmentsResource(services_factory)) self.putChild('contacts', ContactsResource(search_engine)) self.putChild('features', FeaturesResource()) self.putChild('tags', TagsResource(search_engine)) -- cgit v1.2.3