summaryrefslogtreecommitdiff
path: root/service/pixelated/controllers
diff options
context:
space:
mode:
authorDuda Dornelles <ddornell@thoughtworks.com>2014-10-15 13:57:13 +0200
committerDuda Dornelles <ddornell@thoughtworks.com>2014-10-15 14:21:26 +0200
commitac3ea5d3838b98b07d182d71f2f3d9f270b5d274 (patch)
tree2aee9e4138d5732ff6890dd07a8a62de7f90d583 /service/pixelated/controllers
parent89d26f3cd389f41b83d27ea225944a0ff8bc2e95 (diff)
Extracting controller classes
Diffstat (limited to 'service/pixelated/controllers')
-rw-r--r--service/pixelated/controllers/__init__.py29
-rw-r--r--service/pixelated/controllers/home_controller.py26
-rw-r--r--service/pixelated/controllers/mails_controller.py111
-rw-r--r--service/pixelated/controllers/tags_controller.py30
4 files changed, 196 insertions, 0 deletions
diff --git a/service/pixelated/controllers/__init__.py b/service/pixelated/controllers/__init__.py
new file mode 100644
index 00000000..84d7bf4d
--- /dev/null
+++ b/service/pixelated/controllers/__init__.py
@@ -0,0 +1,29 @@
+#
+# Copyright (c) 2014 ThoughtWorks, Inc.
+#
+# Pixelated is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Pixelated is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
+
+
+def respond_json(entity, status_code=200):
+ json_response = json.dumps(entity)
+ response = Response(response=json_response, mimetype="application/json")
+ response.status_code = status_code
+ return response
+
+
+import json
+from flask import Response
+from home_controller import HomeController
+from mails_controller import MailsController
+from tags_controller import TagsController
diff --git a/service/pixelated/controllers/home_controller.py b/service/pixelated/controllers/home_controller.py
new file mode 100644
index 00000000..69ecd52f
--- /dev/null
+++ b/service/pixelated/controllers/home_controller.py
@@ -0,0 +1,26 @@
+#
+# Copyright (c) 2014 ThoughtWorks, Inc.
+#
+# Pixelated is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Pixelated is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
+
+from flask import current_app
+
+
+class HomeController:
+
+ def __init__(self):
+ pass
+
+ def home(self):
+ return current_app.send_static_file('index.html')
diff --git a/service/pixelated/controllers/mails_controller.py b/service/pixelated/controllers/mails_controller.py
new file mode 100644
index 00000000..26b35831
--- /dev/null
+++ b/service/pixelated/controllers/mails_controller.py
@@ -0,0 +1,111 @@
+#
+# Copyright (c) 2014 ThoughtWorks, Inc.
+#
+# Pixelated is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Pixelated is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
+
+import json
+from pixelated.adapter.pixelated_mail import InputMail
+from pixelated.controllers import respond_json
+from flask import request
+import dateutil.parser as dateparser
+
+
+class MailsController:
+
+ def __init__(self, mail_service, draft_service, search_engine):
+ self._mail_service = mail_service
+ self._draft_service = draft_service
+ self._search_engine = search_engine
+
+ def mails(self, _request=request):
+ mail_ids = self._search_engine.search(_request.args.get('q'))
+ mails = self._mail_service.mails(mail_ids)
+ mails = sorted(mails, key=lambda mail: dateparser.parse(mail.get_date()), reverse=True)
+
+ response = {
+ "stats": {
+ "total": len(mails),
+ "read": 0,
+ "starred": 0,
+ "replied": 0
+ },
+ "mails": [mail.as_dict() for mail in mails]
+ }
+
+ return respond_json(response)
+
+ def mail(self, mail_id):
+ mail = self._mail_service.mail(mail_id)
+ return respond_json(mail.as_dict())
+
+ def mark_mail_as_read(self, mail_id):
+ mail = self._mail_service.mark_as_read(mail_id)
+ self._search_engine.index_mail(mail)
+ return ""
+
+ def mark_mail_as_unread(self, mail_id):
+ mail = self._mail_service.mark_as_unread(mail_id)
+ self._search_engine.index_mail(mail)
+ return ""
+
+ def mark_many_mail_unread(self):
+ idents = json.loads(request.form['idents'])
+ for ident in idents:
+ self._mail_service.mark_as_unread(ident)
+ return ""
+
+ def delete_mail(self, mail_id):
+ trashed_mail = self._mail_service.delete_mail(mail_id)
+ self._search_engine.index_mail(trashed_mail)
+ return respond_json(None)
+
+ def delete_mails(self):
+ idents = json.loads(request.form['idents'])
+ for ident in idents:
+ self._mail_service.delete_mail(ident)
+ return respond_json(None)
+
+ def send_mail(self, _request=request):
+ try:
+ _mail = InputMail.from_dict(_request.json)
+ draft_id = _request.json.get('ident')
+ if draft_id:
+ self._search_engine.remove_from_index(draft_id)
+ _mail = self._mail_service.send(draft_id, _mail)
+ self._search_engine.index_mail(_mail)
+
+ return respond_json(_mail.as_dict())
+ except Exception as error:
+ return respond_json({'message': '\n'.join(list(error.args))}, status_code=500)
+
+ def mail_tags(self, mail_id):
+ new_tags = map(lambda tag: tag.lower(), request.get_json()['newtags'])
+ try:
+ tags = self._mail_service.update_tags(mail_id, new_tags)
+ self._search_engine.index_mail(self._mail_service.mail(mail_id))
+ except ValueError as ve:
+ return respond_json(ve.message, 403)
+ return respond_json(list(tags))
+
+ def update_draft(self):
+ _mail = InputMail.from_dict(request.json)
+ draft_id = request.json.get('ident')
+ if draft_id:
+ ident = self._draft_service.update_draft(draft_id, _mail).ident
+ self._search_engine.remove_from_index(draft_id)
+ else:
+ ident = self._draft_service.create_draft(_mail).ident
+
+ self._search_engine.index_mail(self._mail_service.mail(ident))
+ return respond_json({'ident': ident})
diff --git a/service/pixelated/controllers/tags_controller.py b/service/pixelated/controllers/tags_controller.py
new file mode 100644
index 00000000..52ed762a
--- /dev/null
+++ b/service/pixelated/controllers/tags_controller.py
@@ -0,0 +1,30 @@
+#
+# Copyright (c) 2014 ThoughtWorks, Inc.
+#
+# Pixelated is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Pixelated is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
+
+from flask import request
+from pixelated.controllers import respond_json
+
+
+class TagsController:
+
+ def __init__(self, search_engine):
+ self._search_engine = search_engine
+
+ def tags(self):
+ query = request.args.get('q')
+ skip_default_tags = request.args.get('skipDefaultTags')
+ tags = self._search_engine.tags(query=query, skip_default_tags=skip_default_tags)
+ return respond_json(tags)