aboutsummaryrefslogtreecommitdiff
path: root/server/szurubooru/func
diff options
context:
space:
mode:
authorrr-2016-04-24 14:24:41 +0200
committerrr-2016-04-24 14:34:01 +0200
commit8fb536c8f0aa4e4a398f1d1db05151a3dd897c6f (patch)
tree524d96a48050bfd761bb3abffda0c1173c9f01ad /server/szurubooru/func
parent0b47957bb9032fd021c35e8e42c2dd2029915358 (diff)
server/general: move not found errors to func
Diffstat (limited to 'server/szurubooru/func')
-rw-r--r--server/szurubooru/func/comments.py8
-rw-r--r--server/szurubooru/func/posts.py10
-rw-r--r--server/szurubooru/func/tag_categories.py32
-rw-r--r--server/szurubooru/func/tags.py50
-rw-r--r--server/szurubooru/func/users.py22
5 files changed, 82 insertions, 40 deletions
diff --git a/server/szurubooru/func/comments.py b/server/szurubooru/func/comments.py
index 873bc7d..576cca2 100644
--- a/server/szurubooru/func/comments.py
+++ b/server/szurubooru/func/comments.py
@@ -15,12 +15,18 @@ def serialize_comment(comment, authenticated_user):
'lastEditTime': comment.last_edit_time,
}
-def get_comment_by_id(comment_id):
+def try_get_comment_by_id(comment_id):
return db.session \
.query(db.Comment) \
.filter(db.Comment.comment_id == comment_id) \
.one_or_none()
+def get_comment_by_id(comment_id):
+ comment = try_get_comment_by_id(comment_id)
+ if comment:
+ return comment
+ raise CommentNotFoundError('Comment %r not found.' % comment_id)
+
def create_comment(user, post, text):
comment = db.Comment()
comment.user = user
diff --git a/server/szurubooru/func/posts.py b/server/szurubooru/func/posts.py
index 8811caa..5d947b2 100644
--- a/server/szurubooru/func/posts.py
+++ b/server/szurubooru/func/posts.py
@@ -49,13 +49,19 @@ def serialize_post_with_details(post, authenticated_user):
def get_post_count():
return db.session.query(sqlalchemy.func.count(db.Post.post_id)).one()[0]
-def get_post_by_id(post_id):
+def try_get_post_by_id(post_id):
return db.session \
.query(db.Post) \
.filter(db.Post.post_id == post_id) \
.one_or_none()
-def get_featured_post():
+def get_post_by_id(post_id):
+ post = try_get_post_by_id(post_id)
+ if not post:
+ raise PostNotFoundError('Post %r not found.' % post_id)
+ return post
+
+def try_get_featured_post():
post_feature = db.session \
.query(db.PostFeature) \
.order_by(db.PostFeature.time.desc()) \
diff --git a/server/szurubooru/func/tag_categories.py b/server/szurubooru/func/tag_categories.py
index c4a55cd..90bb387 100644
--- a/server/szurubooru/func/tag_categories.py
+++ b/server/szurubooru/func/tag_categories.py
@@ -1,6 +1,6 @@
import re
from szurubooru import config, db, errors
-from szurubooru.func import util
+from szurubooru.func import util, snapshots
class TagCategoryNotFoundError(errors.NotFoundError): pass
class TagCategoryAlreadyExistsError(errors.ValidationError): pass
@@ -14,6 +14,18 @@ def _verify_name_validity(name):
raise InvalidTagCategoryNameError(
'Name must satisfy regex %r.' % name_regex)
+def serialize_category(category):
+ return {
+ 'name': category.name,
+ 'color': category.color,
+ }
+
+def serialize_category_with_details(category):
+ return {
+ 'tagCategory': serialize_category(category),
+ 'snapshots': snapshots.get_serialized_history(category),
+ }
+
def create_category(name, color):
category = db.TagCategory()
update_name(category, name)
@@ -42,11 +54,17 @@ def update_color(category, color):
raise InvalidTagCategoryColorError('Color is too long.')
category.color = color
-def get_category_by_name(name):
+def try_get_category_by_name(name):
return db.session \
.query(db.TagCategory) \
.filter(db.TagCategory.name.ilike(name)) \
- .first()
+ .one_or_none()
+
+def get_category_by_name(name):
+ category = try_get_category_by_name(name)
+ if not category:
+ raise TagCategoryNotFoundError('Tag category %r not found.' % name)
+ return category
def get_all_category_names():
return [row[0] for row in db.session.query(db.TagCategory.name).all()]
@@ -54,9 +72,15 @@ def get_all_category_names():
def get_all_categories():
return db.session.query(db.TagCategory).all()
-def get_default_category():
+def try_get_default_category():
return db.session \
.query(db.TagCategory) \
.order_by(db.TagCategory.tag_category_id.asc()) \
.limit(1) \
.one()
+
+def get_default_category():
+ category = try_get_default_category()
+ if not category:
+ raise TagCategoryNotFoundError('No tag category created yet.')
+ return category
diff --git a/server/szurubooru/func/tags.py b/server/szurubooru/func/tags.py
index 7b5c70f..14619a0 100644
--- a/server/szurubooru/func/tags.py
+++ b/server/szurubooru/func/tags.py
@@ -13,6 +13,20 @@ class InvalidTagNameError(errors.ValidationError): pass
class InvalidTagCategoryError(errors.ValidationError): pass
class InvalidTagRelationError(errors.ValidationError): pass
+def _verify_name_validity(name):
+ name_regex = config.config['tag_name_regex']
+ if not re.match(name_regex, name):
+ raise InvalidTagNameError('Name must satisfy regex %r.' % name_regex)
+
+def _get_plain_names(tag):
+ return [tag_name.name for tag_name in tag.names]
+
+def _lower_list(names):
+ return [name.lower() for name in names]
+
+def _check_name_intersection(names1, names2):
+ return len(set(_lower_list(names1)).intersection(_lower_list(names2))) > 0
+
def serialize_tag(tag):
return {
'names': [tag_name.name for tag_name in tag.names],
@@ -31,32 +45,6 @@ def serialize_tag_with_details(tag):
'snapshots': snapshots.get_serialized_history(tag),
}
-def serialize_category(category):
- return {
- 'name': category.name,
- 'color': category.color,
- }
-
-def serialize_category_with_details(category):
- return {
- 'tagCategory': serialize_category(category),
- 'snapshots': snapshots.get_serialized_history(category),
- }
-
-def _verify_name_validity(name):
- name_regex = config.config['tag_name_regex']
- if not re.match(name_regex, name):
- raise InvalidTagNameError('Name must satisfy regex %r.' % name_regex)
-
-def _get_plain_names(tag):
- return [tag_name.name for tag_name in tag.names]
-
-def _lower_list(names):
- return [name.lower() for name in names]
-
-def _check_name_intersection(names1, names2):
- return len(set(_lower_list(names1)).intersection(_lower_list(names2))) > 0
-
def export_to_json():
output = {
'tags': [],
@@ -90,12 +78,18 @@ def export_to_json():
with open(export_path, 'w') as handle:
handle.write(json.dumps(output, separators=(',', ':')))
-def get_tag_by_name(name):
+def try_get_tag_by_name(name):
return db.session \
.query(db.Tag) \
.join(db.TagName) \
.filter(db.TagName.name.ilike(name)) \
- .first()
+ .one_or_none()
+
+def get_tag_by_name(name):
+ tag = try_get_tag_by_name(name)
+ if not tag:
+ raise TagNotFoundError('Tag %r not found.' % name)
+ return tag
def get_tags_by_names(names):
names = util.icase_unique(names)
diff --git a/server/szurubooru/func/users.py b/server/szurubooru/func/users.py
index 85272ff..5a41881 100644
--- a/server/szurubooru/func/users.py
+++ b/server/szurubooru/func/users.py
@@ -44,19 +44,31 @@ def serialize_user(user, authenticated_user):
def get_user_count():
return db.session.query(db.User).count()
-def get_user_by_name(name):
+def try_get_user_by_name(name):
return db.session \
.query(db.User) \
.filter(func.lower(db.User.name) == func.lower(name)) \
- .first()
+ .one_or_none()
-def get_user_by_name_or_email(name_or_email):
+def get_user_by_name(name):
+ user = try_get_user_by_name(name)
+ if not user:
+ raise UserNotFoundError('User %r not found.' % name)
+ return user
+
+def try_get_user_by_name_or_email(name_or_email):
return db.session \
.query(db.User) \
.filter(
(func.lower(db.User.name) == func.lower(name_or_email))
| (func.lower(db.User.email) == func.lower(name_or_email))) \
- .first()
+ .one_or_none()
+
+def get_user_by_name_or_email(name_or_email):
+ user = try_get_user_by_name_or_email(name_or_email)
+ if not user:
+ raise UserNotFoundError('User %r not found.' % name_or_email)
+ return user
def create_user(name, password, email, auth_user):
user = db.User()
@@ -76,7 +88,7 @@ def update_name(user, name, auth_user):
raise InvalidUserNameError('Name cannot be empty.')
if util.value_exceeds_column_size(name, db.User.name):
raise InvalidUserNameError('User name is too long.')
- other_user = get_user_by_name(name)
+ other_user = try_get_user_by_name(name)
if other_user and other_user.user_id != auth_user.user_id:
raise UserAlreadyExistsError('User %r already exists.' % name)
name = name.strip()

© 2015 - 2026 Jakob L. Kreuze