server/general: refactor method names

This commit is contained in:
rr- 2016-04-30 23:53:50 +02:00
parent f38acf6868
commit a567974784
11 changed files with 90 additions and 80 deletions

View File

@ -14,7 +14,8 @@ class PasswordResetApi(BaseApi):
user = users.get_user_by_name_or_email(user_name)
if not user.email:
raise errors.ValidationError(
'User %r hasn\'t supplied email. Cannot reset password.' % user_name)
'User %r hasn\'t supplied email. Cannot reset password.' % (
user_name))
token = auth.generate_authentication_token(user)
url = '%s/password-reset/%s:%s' % (
config.config['base_url'].rstrip('/'), user.name, token)
@ -32,6 +33,6 @@ class PasswordResetApi(BaseApi):
token = ctx.get_param_as_string('token', required=True)
if token != good_token:
raise errors.ValidationError('Invalid password reset token.')
new_password = users.reset_password(user)
new_password = users.reset_user_password(user)
ctx.session.commit()
return {'password': new_password}

View File

@ -10,7 +10,7 @@ class PostDetailApi(BaseApi):
def delete(self, ctx, post_id):
auth.verify_privilege(ctx.user, 'posts:delete')
post = posts.get_post_by_id(post_id)
snapshots.delete(post, ctx.user)
snapshots.save_entity_deletion(post, ctx.user)
ctx.session.delete(post)
ctx.session.commit()
tags.export_to_json()
@ -27,8 +27,8 @@ class PostFeatureApi(BaseApi):
'Post %r is already featured.' % post_id)
posts.feature_post(post, ctx.user)
if featured_post:
snapshots.modify(featured_post, ctx.user)
snapshots.modify(post, ctx.user)
snapshots.save_entity_modification(featured_post, ctx.user)
snapshots.save_entity_modification(post, ctx.user)
ctx.session.commit()
return posts.serialize_post_with_details(post, ctx.user)

View File

@ -26,7 +26,7 @@ class TagListApi(BaseApi):
tag = tags.create_tag(names, category, suggestions, implications)
ctx.session.add(tag)
ctx.session.flush()
snapshots.create(tag, ctx.user)
snapshots.save_entity_creation(tag, ctx.user)
ctx.session.commit()
tags.export_to_json()
return tags.serialize_tag_with_details(tag)
@ -41,19 +41,22 @@ class TagDetailApi(BaseApi):
tag = tags.get_tag_by_name(tag_name)
if ctx.has_param('names'):
auth.verify_privilege(ctx.user, 'tags:edit:names')
tags.update_names(tag, ctx.get_param_as_list('names'))
tags.update_tag_names(tag, ctx.get_param_as_list('names'))
if ctx.has_param('category'):
auth.verify_privilege(ctx.user, 'tags:edit:category')
tags.update_category_name(tag, ctx.get_param_as_string('category'))
tags.update_tag_category_name(
tag, ctx.get_param_as_string('category'))
if ctx.has_param('suggestions'):
auth.verify_privilege(ctx.user, 'tags:edit:suggestions')
tags.update_suggestions(tag, ctx.get_param_as_list('suggestions'))
tags.update_tag_suggestions(
tag, ctx.get_param_as_list('suggestions'))
if ctx.has_param('implications'):
auth.verify_privilege(ctx.user, 'tags:edit:implications')
tags.update_implications(tag, ctx.get_param_as_list('implications'))
tags.update_tag_implications(
tag, ctx.get_param_as_list('implications'))
tag.last_edit_time = datetime.datetime.now()
ctx.session.flush()
snapshots.modify(tag, ctx.user)
snapshots.save_entity_modification(tag, ctx.user)
ctx.session.commit()
tags.export_to_json()
return tags.serialize_tag_with_details(tag)
@ -65,7 +68,7 @@ class TagDetailApi(BaseApi):
'Tag has some usages and cannot be deleted. ' +
'Please untag relevant posts first.')
auth.verify_privilege(ctx.user, 'tags:delete')
snapshots.delete(tag, ctx.user)
snapshots.save_entity_deletion(tag, ctx.user)
ctx.session.delete(tag)
ctx.session.commit()
tags.export_to_json()
@ -80,7 +83,7 @@ class TagMergeApi(BaseApi):
if source_tag.tag_id == target_tag.tag_id:
raise tags.InvalidTagRelationError('Cannot merge tag with itself.')
auth.verify_privilege(ctx.user, 'tags:merge')
snapshots.delete(source_tag, ctx.user)
snapshots.save_entity_deletion(source_tag, ctx.user)
tags.merge_tags(source_tag, target_tag)
ctx.session.commit()
tags.export_to_json()
@ -90,7 +93,7 @@ class TagSiblingsApi(BaseApi):
def get(self, ctx, tag_name):
auth.verify_privilege(ctx.user, 'tags:view')
tag = tags.get_tag_by_name(tag_name)
result = tags.get_siblings(tag)
result = tags.get_tag_siblings(tag)
serialized_siblings = []
for sibling, occurrences in result:
serialized_siblings.append({

View File

@ -18,7 +18,7 @@ class TagCategoryListApi(BaseApi):
category = tag_categories.create_category(name, color)
ctx.session.add(category)
ctx.session.flush()
snapshots.create(category, ctx.user)
snapshots.save_entity_creation(category, ctx.user)
ctx.session.commit()
tags.export_to_json()
return tag_categories.serialize_category_with_details(category)
@ -33,14 +33,14 @@ class TagCategoryDetailApi(BaseApi):
category = tag_categories.get_category_by_name(category_name)
if ctx.has_param('name'):
auth.verify_privilege(ctx.user, 'tag_categories:edit:name')
tag_categories.update_name(
tag_categories.update_category_name(
category, ctx.get_param_as_string('name'))
if ctx.has_param('color'):
auth.verify_privilege(ctx.user, 'tag_categories:edit:color')
tag_categories.update_color(
tag_categories.update_category_color(
category, ctx.get_param_as_string('color'))
ctx.session.flush()
snapshots.modify(category, ctx.user)
snapshots.save_entity_modification(category, ctx.user)
ctx.session.commit()
tags.export_to_json()
return tag_categories.serialize_category_with_details(category)
@ -55,7 +55,7 @@ class TagCategoryDetailApi(BaseApi):
raise tag_categories.TagCategoryIsInUseError(
'Tag category has some usages and cannot be deleted. ' +
'Please remove this category from relevant tags first..')
snapshots.delete(category, ctx.user)
snapshots.save_entity_deletion(category, ctx.user)
ctx.session.delete(category)
ctx.session.commit()
tags.export_to_json()

View File

@ -19,9 +19,10 @@ class UserListApi(BaseApi):
email = ctx.get_param_as_string('email', required=False, default='')
user = users.create_user(name, password, email, ctx.user)
if ctx.has_param('rank'):
users.update_rank(user, ctx.get_param_as_string('rank'), ctx.user)
users.update_user_rank(
user, ctx.get_param_as_string('rank'), ctx.user)
if ctx.has_param('avatarStyle'):
users.update_avatar(
users.update_user_avatar(
user,
ctx.get_param_as_string('avatarStyle'),
ctx.get_file('avatar'))
@ -41,19 +42,22 @@ class UserDetailApi(BaseApi):
infix = 'self' if ctx.user.user_id == user.user_id else 'any'
if ctx.has_param('name'):
auth.verify_privilege(ctx.user, 'users:edit:%s:name' % infix)
users.update_name(user, ctx.get_param_as_string('name'), ctx.user)
users.update_user_name(
user, ctx.get_param_as_string('name'), ctx.user)
if ctx.has_param('password'):
auth.verify_privilege(ctx.user, 'users:edit:%s:pass' % infix)
users.update_password(user, ctx.get_param_as_string('password'))
users.update_user_password(
user, ctx.get_param_as_string('password'))
if ctx.has_param('email'):
auth.verify_privilege(ctx.user, 'users:edit:%s:email' % infix)
users.update_email(user, ctx.get_param_as_string('email'))
users.update_user_email(user, ctx.get_param_as_string('email'))
if ctx.has_param('rank'):
auth.verify_privilege(ctx.user, 'users:edit:%s:rank' % infix)
users.update_rank(user, ctx.get_param_as_string('rank'), ctx.user)
users.update_user_rank(
user, ctx.get_param_as_string('rank'), ctx.user)
if ctx.has_param('avatarStyle'):
auth.verify_privilege(ctx.user, 'users:edit:%s:avatar' % infix)
users.update_avatar(
users.update_user_avatar(
user,
ctx.get_param_as_string('avatarStyle'),
ctx.get_file('avatar'))

View File

@ -80,7 +80,7 @@ def get_serialized_history(entity):
earlier_snapshot = snapshot
return ret
def save(operation, entity, auth_user):
def _save(operation, entity, auth_user):
resource_type, resource_id, resource_repr = util.get_resource_info(entity)
now = datetime.datetime.now()
@ -113,11 +113,11 @@ def save(operation, entity, auth_user):
else:
db.session.add(snapshot)
def create(entity, auth_user):
save(db.Snapshot.OPERATION_CREATED, entity, auth_user)
def save_entity_creation(entity, auth_user):
_save(db.Snapshot.OPERATION_CREATED, entity, auth_user)
def modify(entity, auth_user):
save(db.Snapshot.OPERATION_MODIFIED, entity, auth_user)
def save_entity_modification(entity, auth_user):
_save(db.Snapshot.OPERATION_MODIFIED, entity, auth_user)
def delete(entity, auth_user):
save(db.Snapshot.OPERATION_DELETED, entity, auth_user)
def save_entity_deletion(entity, auth_user):
_save(db.Snapshot.OPERATION_DELETED, entity, auth_user)

View File

@ -28,11 +28,11 @@ def serialize_category_with_details(category):
def create_category(name, color):
category = db.TagCategory()
update_name(category, name)
update_color(category, color)
update_category_name(category, name)
update_category_color(category, color)
return category
def update_name(category, name):
def update_category_name(category, name):
if not name:
raise InvalidTagCategoryNameError('Name cannot be empty.')
expr = db.TagCategory.name.ilike(name)
@ -47,7 +47,7 @@ def update_name(category, name):
_verify_name_validity(name)
category.name = name
def update_color(category, color):
def update_category_color(category, color):
if not color:
raise InvalidTagCategoryNameError('Color cannot be empty.')
if util.value_exceeds_column_size(color, db.TagCategory.color):

View File

@ -122,7 +122,7 @@ def get_or_create_tags_by_names(names):
new_tags.append(new_tag)
return related_tags, new_tags
def get_siblings(tag):
def get_tag_siblings(tag):
tag_alias = sqlalchemy.orm.aliased(db.Tag)
pt_alias1 = sqlalchemy.orm.aliased(db.PostTag)
pt_alias2 = sqlalchemy.orm.aliased(db.PostTag)
@ -147,13 +147,13 @@ def merge_tags(source_tag, target_tag):
def create_tag(names, category_name, suggestions, implications):
tag = db.Tag()
tag.creation_time = datetime.datetime.now()
update_names(tag, names)
update_category_name(tag, category_name)
update_suggestions(tag, suggestions)
update_implications(tag, implications)
update_tag_names(tag, names)
update_tag_category_name(tag, category_name)
update_tag_suggestions(tag, suggestions)
update_tag_implications(tag, implications)
return tag
def update_category_name(tag, category_name):
def update_tag_category_name(tag, category_name):
category = db.session \
.query(db.TagCategory) \
.filter(db.TagCategory.name == category_name) \
@ -163,7 +163,7 @@ def update_category_name(tag, category_name):
db.session.add(category)
tag.category = category
def update_names(tag, names):
def update_tag_names(tag, names):
names = util.icase_unique([name for name in names if name])
if not len(names):
raise InvalidTagNameError('At least one name must be specified.')
@ -190,14 +190,14 @@ def update_names(tag, names):
if not _check_name_intersection(_get_plain_names(tag), [name]):
tag.names.append(db.TagName(name))
def update_implications(tag, relations):
def update_tag_implications(tag, relations):
if _check_name_intersection(_get_plain_names(tag), relations):
raise InvalidTagRelationError('Tag cannot imply itself.')
related_tags, new_tags = get_or_create_tags_by_names(relations)
db.session.flush()
tag.implications = related_tags + new_tags
def update_suggestions(tag, relations):
def update_tag_suggestions(tag, relations):
if _check_name_intersection(_get_plain_names(tag), relations):
raise InvalidTagRelationError('Tag cannot suggest itself.')
related_tags, new_tags = get_or_create_tags_by_names(relations)

View File

@ -78,9 +78,9 @@ def get_user_by_name_or_email(name_or_email):
def create_user(name, password, email, auth_user):
user = db.User()
update_name(user, name, auth_user)
update_password(user, password)
update_email(user, email)
update_user_name(user, name, auth_user)
update_user_password(user, password)
update_user_email(user, email)
if get_user_count() > 0:
user.rank = config.config['default_rank']
else:
@ -89,7 +89,7 @@ def create_user(name, password, email, auth_user):
user.avatar_style = db.User.AVATAR_GRAVATAR
return user
def update_name(user, name, auth_user):
def update_user_name(user, name, auth_user):
if not name:
raise InvalidUserNameError('Name cannot be empty.')
if util.value_exceeds_column_size(name, db.User.name):
@ -104,7 +104,7 @@ def update_name(user, name, auth_user):
'User name %r must satisfy regex %r.' % (name, name_regex))
user.name = name
def update_password(user, password):
def update_user_password(user, password):
if not password:
raise InvalidPasswordError('Password cannot be empty.')
password_regex = config.config['password_regex']
@ -114,7 +114,7 @@ def update_password(user, password):
user.password_salt = auth.create_password()
user.password_hash = auth.get_password_hash(user.password_salt, password)
def update_email(user, email):
def update_user_email(user, email):
if email:
email = email.strip()
if not email:
@ -125,7 +125,7 @@ def update_email(user, email):
raise InvalidEmailError('E-mail is invalid.')
user.email = email
def update_rank(user, rank, authenticated_user):
def update_user_rank(user, rank, authenticated_user):
if not rank:
raise InvalidRankError('Rank cannot be empty.')
rank = rank.strip()
@ -138,7 +138,7 @@ def update_rank(user, rank, authenticated_user):
raise errors.AuthError('Trying to set higher rank than your own.')
user.rank = rank
def update_avatar(user, avatar_style, avatar_content):
def update_user_avatar(user, avatar_style, avatar_content):
if avatar_style == 'gravatar':
user.avatar_style = user.AVATAR_GRAVATAR
elif avatar_style == 'manual':
@ -155,10 +155,10 @@ def update_avatar(user, avatar_style, avatar_content):
'Avatar style %r is invalid. Valid avatar styles: %r.' % (
avatar_style, ['gravatar', 'manual']))
def bump_login_time(user):
def bump_user_login_time(user):
user.last_login_time = datetime.datetime.now()
def reset_password(user):
def reset_user_password(user):
password = auth.create_password()
user.password_salt = auth.create_password()
user.password_hash = auth.get_password_hash(user.password_salt, password)

View File

@ -14,7 +14,7 @@ class Authenticator(object):
request.context.user = self._get_user(request)
if request.get_param_as_bool('bump-login') \
and request.context.user.user_id:
users.bump_login_time(request.context.user)
users.bump_user_login_time(request.context.user)
request.context.session.commit()
def _get_user(self, request):

View File

@ -99,9 +99,9 @@ def test_merging_modification_to_creation(tag_factory, user_factory):
user = user_factory()
db.session.add_all([tag, user])
db.session.flush()
snapshots.create(tag, user)
snapshots.save_entity_creation(tag, user)
tag.names = [db.TagName('changed')]
snapshots.modify(tag, user)
snapshots.save_entity_modification(tag, user)
results = db.session.query(db.Snapshot).all()
assert len(results) == 1
assert results[0].operation == db.Snapshot.OPERATION_CREATED
@ -113,13 +113,13 @@ def test_merging_modifications(fake_datetime, tag_factory, user_factory):
db.session.add_all([tag, user])
db.session.flush()
with fake_datetime('13:00:00'):
snapshots.create(tag, user)
snapshots.save_entity_creation(tag, user)
tag.names = [db.TagName('changed')]
with fake_datetime('14:00:00'):
snapshots.modify(tag, user)
snapshots.save_entity_modification(tag, user)
tag.names = [db.TagName('changed again')]
with fake_datetime('14:00:01'):
snapshots.modify(tag, user)
snapshots.save_entity_modification(tag, user)
results = db.session.query(db.Snapshot).all()
assert len(results) == 2
assert results[0].operation == db.Snapshot.OPERATION_CREATED
@ -134,9 +134,9 @@ def test_not_adding_snapshot_if_data_doesnt_change(
db.session.add_all([tag, user])
db.session.flush()
with fake_datetime('13:00:00'):
snapshots.create(tag, user)
snapshots.save_entity_creation(tag, user)
with fake_datetime('14:00:00'):
snapshots.modify(tag, user)
snapshots.save_entity_modification(tag, user)
results = db.session.query(db.Snapshot).all()
assert len(results) == 1
assert results[0].operation == db.Snapshot.OPERATION_CREATED
@ -149,10 +149,10 @@ def test_not_merging_due_to_time_difference(
db.session.add_all([tag, user])
db.session.flush()
with fake_datetime('13:00:00'):
snapshots.create(tag, user)
snapshots.save_entity_creation(tag, user)
tag.names = [db.TagName('changed')]
with fake_datetime('13:10:01'):
snapshots.modify(tag, user)
snapshots.save_entity_modification(tag, user)
assert db.session.query(db.Snapshot).count() == 2
def test_not_merging_operations_by_different_users(
@ -162,9 +162,9 @@ def test_not_merging_operations_by_different_users(
db.session.add_all([tag, user1, user2])
db.session.flush()
with fake_datetime('13:00:00'):
snapshots.create(tag, user1)
snapshots.save_entity_creation(tag, user1)
tag.names = [db.TagName('changed')]
snapshots.modify(tag, user2)
snapshots.save_entity_modification(tag, user2)
assert db.session.query(db.Snapshot).count() == 2
def test_merging_resets_merging_time_window(
@ -174,19 +174,20 @@ def test_merging_resets_merging_time_window(
db.session.add_all([tag, user])
db.session.flush()
with fake_datetime('13:00:00'):
snapshots.create(tag, user)
snapshots.save_entity_creation(tag, user)
tag.names = [db.TagName('changed')]
with fake_datetime('13:09:59'):
snapshots.modify(tag, user)
snapshots.save_entity_modification(tag, user)
tag.names = [db.TagName('changed again')]
with fake_datetime('13:19:59'):
snapshots.modify(tag, user)
snapshots.save_entity_modification(tag, user)
results = db.session.query(db.Snapshot).all()
assert len(results) == 1
assert results[0].data['names'] == ['changed again']
@pytest.mark.parametrize(
'initial_operation', [snapshots.create, snapshots.modify])
'initial_operation',
[snapshots.save_entity_creation, snapshots.save_entity_modification])
def test_merging_deletion_to_modification_or_creation(
fake_datetime, tag_factory, user_factory, initial_operation):
tag = tag_factory(names=['dummy'], category_name='dummy')
@ -197,10 +198,10 @@ def test_merging_deletion_to_modification_or_creation(
initial_operation(tag, user)
tag.names = [db.TagName('changed')]
with fake_datetime('14:00:00'):
snapshots.modify(tag, user)
snapshots.save_entity_modification(tag, user)
tag.names = [db.TagName('changed again')]
with fake_datetime('14:00:01'):
snapshots.delete(tag, user)
snapshots.save_entity_deletion(tag, user)
assert db.session.query(db.Snapshot).count() == 2
results = db.session \
.query(db.Snapshot) \
@ -215,7 +216,8 @@ def test_merging_deletion_to_modification_or_creation(
}
@pytest.mark.parametrize(
'expected_operation', [snapshots.create, snapshots.modify])
'expected_operation',
[snapshots.save_entity_creation, snapshots.save_entity_modification])
def test_merging_deletion_all_the_way_deletes_all_snapshots(
fake_datetime, tag_factory, user_factory, expected_operation):
tag = tag_factory(names=['dummy'])
@ -223,13 +225,13 @@ def test_merging_deletion_all_the_way_deletes_all_snapshots(
db.session.add_all([tag, user])
db.session.flush()
with fake_datetime('13:00:00'):
snapshots.create(tag, user)
snapshots.save_entity_creation(tag, user)
tag.names = [db.TagName('changed')]
with fake_datetime('13:00:01'):
snapshots.modify(tag, user)
snapshots.save_entity_modification(tag, user)
tag.names = [db.TagName('changed again')]
with fake_datetime('13:00:02'):
snapshots.delete(tag, user)
snapshots.save_entity_deletion(tag, user)
assert db.session.query(db.Snapshot).count() == 0
def test_get_serialized_history(fake_datetime, tag_factory, user_factory):
@ -238,11 +240,11 @@ def test_get_serialized_history(fake_datetime, tag_factory, user_factory):
db.session.add_all([tag, user])
db.session.flush()
with fake_datetime('2016-04-19 13:00:00'):
snapshots.create(tag, user)
snapshots.save_entity_creation(tag, user)
tag.names = [db.TagName('changed')]
db.session.flush()
with fake_datetime('2016-04-19 13:10:01'):
snapshots.modify(tag, user)
snapshots.save_entity_modification(tag, user)
assert snapshots.get_serialized_history(tag) == [
{
'operation': 'modified',