Move jsonify methods into the objects instead of separate file.
This commit is contained in:
		
							parent
							
								
									d3f6d6b26a
								
							
						
					
					
						commit
						8ecf594945
					
				
					 11 changed files with 114 additions and 123 deletions
				
			
		|  | @ -2,7 +2,6 @@ from . import constants | |||
| from . import decorators | ||||
| from . import exceptions | ||||
| from . import helpers | ||||
| from . import jsonify | ||||
| from . import objects | ||||
| from . import photodb | ||||
| from . import searchhelpers | ||||
|  |  | |||
|  | @ -35,6 +35,14 @@ class EtiquetteException(Exception, metaclass=ErrorTypeAdder): | |||
|     def __str__(self): | ||||
|         return self.error_type + '\n' + self.error_message | ||||
| 
 | ||||
|     def jsonify(self): | ||||
|         j = { | ||||
|             'type': 'error', | ||||
|             'error_type': self.error_type, | ||||
|             'error_message': self.error_message, | ||||
|         } | ||||
|         return j | ||||
| 
 | ||||
| # NO SUCH ########################################################################################## | ||||
| 
 | ||||
| class NoSuch(EtiquetteException): | ||||
|  |  | |||
|  | @ -1,95 +0,0 @@ | |||
| ''' | ||||
| This file provides functions that convert the Etiquette objects into | ||||
| dictionaries suitable for JSON serializing. | ||||
| ''' | ||||
| 
 | ||||
| def album(a, minimal=False): | ||||
|     j = { | ||||
|         'type': 'album', | ||||
|         'id': a.id, | ||||
|         'description': a.description, | ||||
|         'title': a.title, | ||||
|         'author': user_or_none(a.get_author()), | ||||
|     } | ||||
|     if not minimal: | ||||
|         j['photos'] = [photo(p) for p in a.get_photos()] | ||||
|         j['parents'] = [album(p, minimal=True) for p in a.get_parents()] | ||||
|         j['sub_albums'] = [album(c, minimal=True) for c in a.get_children()] | ||||
| 
 | ||||
|     return j | ||||
| 
 | ||||
| def bookmark(b): | ||||
|     j = { | ||||
|         'type': 'bookmark', | ||||
|         'id': b.id, | ||||
|         'author': user_or_none(b.get_author()), | ||||
|         'url': b.url, | ||||
|         'title': b.title, | ||||
|     } | ||||
|     return j | ||||
| 
 | ||||
| def exception(e): | ||||
|     j = { | ||||
|         'type': 'error', | ||||
|         'error_type': e.error_type, | ||||
|         'error_message': e.error_message, | ||||
|     } | ||||
|     return j | ||||
| 
 | ||||
| def photo(p, include_albums=True, include_tags=True): | ||||
|     j = { | ||||
|         'type': 'photo', | ||||
|         'id': p.id, | ||||
|         'author': user_or_none(p.get_author()), | ||||
|         'extension': p.extension, | ||||
|         'width': p.width, | ||||
|         'height': p.height, | ||||
|         'ratio': p.ratio, | ||||
|         'area': p.area, | ||||
|         'bytes': p.bytes, | ||||
|         'duration_str': p.duration_string, | ||||
|         'duration': p.duration, | ||||
|         'bytes_str': p.bytestring, | ||||
|         'has_thumbnail': bool(p.thumbnail), | ||||
|         'created': p.created, | ||||
|         'filename': p.basename, | ||||
|         'mimetype': p.mimetype, | ||||
|         'searchhidden': bool(p.searchhidden), | ||||
|     } | ||||
|     if include_albums: | ||||
|         j['albums'] = [album(a, minimal=True) for a in p.get_containing_albums()] | ||||
| 
 | ||||
|     if include_tags: | ||||
|         j['tags'] = [tag(t, minimal=True) for t in p.get_tags()] | ||||
| 
 | ||||
|     return j | ||||
| 
 | ||||
| def tag(t, include_synonyms=False, minimal=False): | ||||
|     j = { | ||||
|         'type': 'tag', | ||||
|         'id': t.id, | ||||
|         'name': t.name, | ||||
|     } | ||||
|     if not minimal: | ||||
|         j['author'] = user_or_none(t.get_author()) | ||||
|         j['description'] = t.description | ||||
|         j['children'] = [tag(c, minimal=True) for c in t.get_children()] | ||||
| 
 | ||||
|     if include_synonyms: | ||||
|         j['synonyms'] = list(t.get_synonyms()) | ||||
|     return j | ||||
| 
 | ||||
| def user(u): | ||||
|     j = { | ||||
|         'type': 'user', | ||||
|         'id': u.id, | ||||
|         'username': u.username, | ||||
|         'created': u.created, | ||||
|         'display_name': u.display_name, | ||||
|     } | ||||
|     return j | ||||
| 
 | ||||
| def user_or_none(u): | ||||
|     if u is None: | ||||
|         return None | ||||
|     return user(u) | ||||
|  | @ -514,6 +514,21 @@ class Album(ObjectBase, GroupableMixin): | |||
|         ) | ||||
|         return row is not None | ||||
| 
 | ||||
|     def jsonify(self, minimal=False): | ||||
|         j = { | ||||
|             'type': 'album', | ||||
|             'id': self.id, | ||||
|             'description': self.description, | ||||
|             'title': self.title, | ||||
|             'author': self.get_author().jsonify() if self.author_id else None, | ||||
|         } | ||||
|         if not minimal: | ||||
|             j['photos'] = [photo.jsonify(include_albums=False) for photo in self.get_photos()] | ||||
|             j['parents'] = [parent.jsonify(minimal=True) for parent in self.get_parents()] | ||||
|             j['sub_albums'] = [child.jsonify(minimal=True) for child in self.get_children()] | ||||
| 
 | ||||
|         return j | ||||
| 
 | ||||
|     @decorators.required_feature('album.edit') | ||||
|     @decorators.transaction | ||||
|     def remove_child(self, *args, **kwargs): | ||||
|  | @ -674,6 +689,16 @@ class Bookmark(ObjectBase): | |||
|         self.title = title | ||||
|         self.url = url | ||||
| 
 | ||||
|     def jsonify(self): | ||||
|         j = { | ||||
|             'type': 'bookmark', | ||||
|             'id': self.id, | ||||
|             'author': self.get_author().jsonify() if self.author_id else None, | ||||
|             'url': self.url, | ||||
|             'title': self.title, | ||||
|         } | ||||
|         return j | ||||
| 
 | ||||
| class Photo(ObjectBase): | ||||
|     ''' | ||||
|     A PhotoDB entry containing information about an image file. | ||||
|  | @ -959,6 +984,34 @@ class Photo(ObjectBase): | |||
| 
 | ||||
|         return tag_by_id[rel_row[0]] | ||||
| 
 | ||||
|     def jsonify(self, include_albums=True, include_tags=True): | ||||
|         j = { | ||||
|             'type': 'photo', | ||||
|             'id': self.id, | ||||
|             'author': self.get_author().jsonify() if self.author_id else None, | ||||
|             'extension': self.extension, | ||||
|             'width': self.width, | ||||
|             'height': self.height, | ||||
|             'ratio': self.ratio, | ||||
|             'area': self.area, | ||||
|             'bytes': self.bytes, | ||||
|             'duration_str': self.duration_string, | ||||
|             'duration': self.duration, | ||||
|             'bytes_str': self.bytestring, | ||||
|             'has_thumbnail': bool(self.thumbnail), | ||||
|             'created': self.created, | ||||
|             'filename': self.basename, | ||||
|             'mimetype': self.mimetype, | ||||
|             'searchhidden': bool(self.searchhidden), | ||||
|         } | ||||
|         if include_albums: | ||||
|             j['albums'] = [album.jsonify(minimal=True) for album in self.get_containing_albums()] | ||||
| 
 | ||||
|         if include_tags: | ||||
|             j['tags'] = [tag.jsonify(minimal=True) for tag in self.get_tags()] | ||||
| 
 | ||||
|         return j | ||||
| 
 | ||||
|     def make_thumbnail_filepath(self): | ||||
|         ''' | ||||
|         Create the filepath that should be the location of our thumbnail. | ||||
|  | @ -1468,6 +1521,22 @@ class Tag(ObjectBase, GroupableMixin): | |||
|         synonyms = set(name for (name,) in syn_rows) | ||||
|         return synonyms | ||||
| 
 | ||||
|     def jsonify(self, include_synonyms=False, minimal=False): | ||||
|         j = { | ||||
|             'type': 'tag', | ||||
|             'id': self.id, | ||||
|             'name': self.name, | ||||
|         } | ||||
|         if not minimal: | ||||
|             j['author'] = self.get_author().jsonify() if self.author_id else None, | ||||
|             j['description'] = self.description | ||||
|             j['children'] = [child.jsonify(minimal=True) for child in self.get_children()] | ||||
| 
 | ||||
|         if include_synonyms: | ||||
|             j['synonyms'] = list(self.get_synonyms()) | ||||
| 
 | ||||
|         return j | ||||
| 
 | ||||
|     @decorators.required_feature('tag.edit') | ||||
|     @decorators.transaction | ||||
|     def remove_child(self, *args, **kwargs): | ||||
|  | @ -1592,6 +1661,16 @@ class User(ObjectBase): | |||
|         else: | ||||
|             return self._display_name | ||||
| 
 | ||||
|     def jsonify(self): | ||||
|         j = { | ||||
|             'type': 'user', | ||||
|             'id': self.id, | ||||
|             'username': self.username, | ||||
|             'created': self.created, | ||||
|             'display_name': self.display_name, | ||||
|         } | ||||
|         return j | ||||
| 
 | ||||
|     @decorators.required_feature('user.edit') | ||||
|     @decorators.transaction | ||||
|     def set_display_name(self, display_name): | ||||
|  |  | |||
|  | @ -132,7 +132,7 @@ def P_wrapper(function): | |||
|             if response_type == 'html': | ||||
|                 flask.abort(status, exc.error_message) | ||||
|             else: | ||||
|                 response = etiquette.jsonify.exception(exc) | ||||
|                 response = exc.jsonify() | ||||
|                 response = jsonify.make_json_response(response, status=status) | ||||
|                 flask.abort(response) | ||||
| 
 | ||||
|  |  | |||
|  | @ -20,7 +20,7 @@ def catch_etiquette_exception(function): | |||
|                 status = 404 | ||||
|             else: | ||||
|                 status = 400 | ||||
|             response = etiquette.jsonify.exception(exc) | ||||
|             response = exc.jsonify() | ||||
|             response = jsonify.make_json_response(response, status=status) | ||||
|             flask.abort(response) | ||||
|     return wrapped | ||||
|  |  | |||
|  | @ -30,7 +30,7 @@ def get_album_html(album_id): | |||
| @site.route('/album/<album_id>.json') | ||||
| def get_album_json(album_id): | ||||
|     album = common.P_album(album_id, response_type='json') | ||||
|     album = etiquette.jsonify.album(album) | ||||
|     album = album.jsonify() | ||||
|     return jsonify.make_json_response(album) | ||||
| 
 | ||||
| @site.route('/album/<album_id>.zip') | ||||
|  | @ -65,7 +65,7 @@ def post_album_add_child(album_id): | |||
|     children = list(common.P_albums(child_ids, response_type='json')) | ||||
|     print(children) | ||||
|     album.add_children(children, commit=True) | ||||
|     response = etiquette.jsonify.album(album) | ||||
|     response = album.jsonify() | ||||
|     return jsonify.make_json_response(response) | ||||
| 
 | ||||
| @site.route('/album/<album_id>/remove_child', methods=['POST']) | ||||
|  | @ -76,7 +76,7 @@ def post_album_remove_child(album_id): | |||
|     child_ids = stringtools.comma_space_split(request.form['child_id']) | ||||
|     children = list(common.P_albums(child_ids, response_type='json')) | ||||
|     album.remove_children(children, commit=True) | ||||
|     response = etiquette.jsonify.album(album) | ||||
|     response = album.jsonify() | ||||
|     return jsonify.make_json_response(response) | ||||
| 
 | ||||
| @site.route('/album/<album_id>/refresh_directories', methods=['POST']) | ||||
|  | @ -101,7 +101,7 @@ def post_album_add_photo(album_id): | |||
|     photo_ids = stringtools.comma_space_split(request.form['photo_id']) | ||||
|     photos = list(common.P_photos(photo_ids, response_type='json')) | ||||
|     album.add_photos(photos, commit=True) | ||||
|     response = etiquette.jsonify.album(album) | ||||
|     response = album.jsonify() | ||||
|     return jsonify.make_json_response(response) | ||||
| 
 | ||||
| @site.route('/album/<album_id>/remove_photo', methods=['POST']) | ||||
|  | @ -115,7 +115,7 @@ def post_album_remove_photo(album_id): | |||
|     photo_ids = stringtools.comma_space_split(request.form['photo_id']) | ||||
|     photos = list(common.P_photos(photo_ids, response_type='json')) | ||||
|     album.remove_photos(photos, commit=True) | ||||
|     response = etiquette.jsonify.album(album) | ||||
|     response = album.jsonify() | ||||
|     return jsonify.make_json_response(response) | ||||
| 
 | ||||
| # Album tag operations ############################################################################# | ||||
|  | @ -132,7 +132,7 @@ def post_album_add_tag(album_id): | |||
|     try: | ||||
|         tag = common.P_tag(tag, response_type='json') | ||||
|     except etiquette.exceptions.NoSuchTag as exc: | ||||
|         response = etiquette.jsonify.exception(exc) | ||||
|         response = exc.jsonify() | ||||
|         return jsonify.make_json_response(response, status=404) | ||||
|     recursive = request.form.get('recursive', False) | ||||
|     recursive = etiquette.helpers.truthystring(recursive) | ||||
|  | @ -153,7 +153,7 @@ def post_album_edit(album_id): | |||
|     title = request.form.get('title', None) | ||||
|     description = request.form.get('description', None) | ||||
|     album.edit(title=title, description=description, commit=True) | ||||
|     response = etiquette.jsonify.album(album, minimal=True) | ||||
|     response = album.jsonify(minimal=True) | ||||
|     return jsonify.make_json_response(response) | ||||
| 
 | ||||
| # Album listings ################################################################################### | ||||
|  | @ -184,7 +184,7 @@ def get_albums_html(): | |||
| @site.route('/albums.json') | ||||
| def get_albums_json(): | ||||
|     albums = get_albums_core() | ||||
|     albums = [etiquette.jsonify.album(album, minimal=True) for album in albums] | ||||
|     albums = [album.jsonify(minimal=True) for album in albums] | ||||
|     return jsonify.make_json_response(albums) | ||||
| 
 | ||||
| # Album create and delete ########################################################################## | ||||
|  | @ -204,7 +204,7 @@ def post_albums_create(): | |||
|         parent.add_child(album) | ||||
|     common.P.commit('create album endpoint') | ||||
| 
 | ||||
|     response = etiquette.jsonify.album(album, minimal=False) | ||||
|     response = album.jsonify(minimal=False) | ||||
|     return jsonify.make_json_response(response) | ||||
| 
 | ||||
| @site.route('/album/<album_id>/delete', methods=['POST']) | ||||
|  |  | |||
|  | @ -14,7 +14,7 @@ session_manager = common.session_manager | |||
| @site.route('/bookmark/<bookmark_id>.json') | ||||
| def get_bookmark_json(bookmark_id): | ||||
|     bookmark = common.P_bookmark(bookmark_id, response_type='json') | ||||
|     response = etiquette.jsonify.bookmark(bookmark) | ||||
|     response = bookmark.jsonify() | ||||
|     return jsonify.make_json_response(response) | ||||
| 
 | ||||
| @site.route('/bookmark/<bookmark_id>/edit', methods=['POST']) | ||||
|  | @ -25,7 +25,7 @@ def post_bookmark_edit(bookmark_id): | |||
|     url = request.form.get('url', None) or None | ||||
|     bookmark.edit(title=title, url=url, commit=True) | ||||
| 
 | ||||
|     response = etiquette.jsonify.bookmark(bookmark) | ||||
|     response = bookmark.jsonify() | ||||
|     response = jsonify.make_json_response(response) | ||||
|     return response | ||||
| 
 | ||||
|  | @ -38,7 +38,7 @@ def get_bookmarks_html(): | |||
| 
 | ||||
| @site.route('/bookmarks.json') | ||||
| def get_bookmarks_json(): | ||||
|     bookmarks = [etiquette.jsonify.bookmark(b) for b in common.P.get_bookmarks()] | ||||
|     bookmarks = [b.jsonify() for b in common.P.get_bookmarks()] | ||||
|     return jsonify.make_json_response(bookmarks) | ||||
| 
 | ||||
| # Bookmark create and delete ####################################################################### | ||||
|  | @ -50,7 +50,7 @@ def post_bookmark_create(): | |||
|     title = request.form.get('title', None) | ||||
|     user = session_manager.get(request).user | ||||
|     bookmark = common.P.new_bookmark(url=url, title=title, author=user, commit=True) | ||||
|     response = etiquette.jsonify.bookmark(bookmark) | ||||
|     response = bookmark.jsonify() | ||||
|     response = jsonify.make_json_response(response) | ||||
|     return response | ||||
| 
 | ||||
|  |  | |||
|  | @ -26,7 +26,7 @@ def get_photo_html(photo_id): | |||
| @site.route('/photo/<photo_id>.json') | ||||
| def get_photo_json(photo_id): | ||||
|     photo = common.P_photo(photo_id, response_type='json') | ||||
|     photo = etiquette.jsonify.photo(photo) | ||||
|     photo = photo.jsonify() | ||||
|     photo = jsonify.make_json_response(photo) | ||||
|     return photo | ||||
| 
 | ||||
|  | @ -488,12 +488,12 @@ def get_search_json(): | |||
|     search_kwargs['tag_forbids'] = tagname_helper(search_kwargs['tag_forbids']) | ||||
| 
 | ||||
|     search_results['results'] = [ | ||||
|         etiquette.jsonify.photo(result, include_albums=False) | ||||
|         result.jsonify(include_albums=False) | ||||
|         if isinstance(result, etiquette.objects.Photo) else | ||||
|         etiquette.jsonify.album(result, minimal=True) | ||||
|         result.jsonify(minimal=True) | ||||
|         for result in search_results['results'] | ||||
|     ] | ||||
|     search_results['total_tags'] = [ | ||||
|         etiquette.jsonify.tag(tag, minimal=True) for tag in search_results['total_tags'] | ||||
|         tag.jsonify(minimal=True) for tag in search_results['total_tags'] | ||||
|     ] | ||||
|     return jsonify.make_json_response(search_results) | ||||
|  |  | |||
|  | @ -40,7 +40,7 @@ def post_tag_edit(tagname): | |||
|     description = request.form.get('description', None) | ||||
|     tag.edit(description=description, commit=True) | ||||
| 
 | ||||
|     response = etiquette.jsonify.tag(tag) | ||||
|     response = tag.jsonify() | ||||
|     response = jsonify.make_json_response(response) | ||||
|     return response | ||||
| 
 | ||||
|  | @ -129,7 +129,7 @@ def get_tags_json(specific_tag_name=None): | |||
|     else: | ||||
|         tags = list(specific_tag.walk_children()) | ||||
| 
 | ||||
|     tags = [etiquette.jsonify.tag(tag, include_synonyms=include_synonyms) for tag in tags] | ||||
|     tags = [tag.jsonify(include_synonyms=include_synonyms) for tag in tags] | ||||
|     return jsonify.make_json_response(tags) | ||||
| 
 | ||||
| # Tag create and delete ############################################################################ | ||||
|  | @ -141,7 +141,7 @@ def post_tag_create(): | |||
|     description = request.form.get('description', None) | ||||
| 
 | ||||
|     tag = common.P.new_tag(name, description, author=session_manager.get(request).user, commit=True) | ||||
|     response = etiquette.jsonify.tag(tag) | ||||
|     response = tag.jsonify() | ||||
|     return jsonify.make_json_response(response) | ||||
| 
 | ||||
| @site.route('/tags/easybake', methods=['POST']) | ||||
|  |  | |||
|  | @ -20,7 +20,7 @@ def get_user_html(username): | |||
| @site.route('/user/<username>.json') | ||||
| def get_user_json(username): | ||||
|     user = common.P_user(username, response_type='json') | ||||
|     user = etiquette.jsonify.user(user) | ||||
|     user = user.jsonify() | ||||
|     return jsonify.make_json_response(user) | ||||
| 
 | ||||
| @site.route('/userid/<user_id>') | ||||
|  | @ -53,7 +53,7 @@ def post_login(): | |||
|     session = session_manager.get(request) | ||||
|     if session.user: | ||||
|         exc = etiquette.exceptions.AlreadySignedIn() | ||||
|         response = etiquette.jsonify.exception(exc) | ||||
|         response = exc.jsonify() | ||||
|         return jsonify.make_json_response(response, status=403) | ||||
| 
 | ||||
|     username = request.form['username'] | ||||
|  | @ -66,10 +66,10 @@ def post_login(): | |||
|         user = common.P.login(username=username, password=password) | ||||
|     except (etiquette.exceptions.NoSuchUser, etiquette.exceptions.WrongLogin): | ||||
|         exc = etiquette.exceptions.WrongLogin() | ||||
|         response = etiquette.jsonify.exception(exc) | ||||
|         response = exc.jsonify() | ||||
|         return jsonify.make_json_response(response, status=422) | ||||
|     except etiquette.exceptions.FeatureDisabled as exc: | ||||
|         response = etiquette.jsonify.exception(exc) | ||||
|         response = exc.jsonify() | ||||
|         return jsonify.make_json_response(response, status=400) | ||||
|     session = sessions.Session(request, user) | ||||
|     session_manager.add(session) | ||||
|  | @ -93,7 +93,7 @@ def post_register(): | |||
|     session = session_manager.get(request) | ||||
|     if session.user: | ||||
|         exc = etiquette.exceptions.AlreadySignedIn() | ||||
|         response = etiquette.jsonify.exception(exc) | ||||
|         response = exc.jsonify() | ||||
|         return jsonify.make_json_response(response, status=403) | ||||
| 
 | ||||
|     username = request.form['username'] | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue