Fix inSomeGroups for the superadmin, delete empty comments

This commit is contained in:
FinnStutzenstein 2019-03-06 08:01:46 +01:00
parent 118b853a91
commit b0d4851651
3 changed files with 33 additions and 23 deletions

View File

@ -12,6 +12,7 @@ import { HttpService } from 'app/core/core-services/http.service';
import { ViewModelStoreService } from 'app/core/core-services/view-model-store.service'; import { ViewModelStoreService } from 'app/core/core-services/view-model-store.service';
import { ViewGroup } from 'app/site/users/models/view-group'; import { ViewGroup } from 'app/site/users/models/view-group';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { ViewMotion } from 'app/site/motions/models/view-motion';
/** /**
* Repository Services for Categories * Repository Services for Categories
@ -106,17 +107,36 @@ export class MotionCommentSectionRepositoryService extends BaseRepository<
} }
/** /**
* Saves a comment made at a MotionCommentSection * Saves a comment made at a MotionCommentSection. Does an update, if
* there is a comment text. Deletes the comment, if the text is empty.
* *
* @param motionId ID of the Motion * @param motion the motion
* @param sectionId ID of the Section where the comment was made * @param section the section where the comment was made
* @param sectionComment the comment text * @param sectionComment the comment text
* @returns the promise to create the object * @returns the promise from the HTTP request
*/ */
public async saveComment(motionId: number, sectionId: number, sectionComment: String): Promise<object> { public async saveComment(motion: ViewMotion, section: ViewMotionCommentSection, comment: string): Promise<void> {
return this.http.post(`rest/motions/motion/${motionId}/manage_comments/`, { if (comment) {
section_id: sectionId, return await this.updateComment(motion, section, comment);
comment: sectionComment } else {
return await this.deleteComment(motion, section);
}
}
/**
* Updates the comment. Saves it on the server.
*/
private async updateComment(motion: ViewMotion, section: ViewMotionCommentSection, comment: string): Promise<void> {
return await this.http.post(`rest/motions/motion/${motion.id}/manage_comments/`, {
section_id: section.id,
comment: comment
}); });
} }
/**
* Deletes a comment from the server
*/
private async deleteComment(motion: ViewMotion, section: ViewMotionCommentSection): Promise<void> {
return await this.http.delete(`rest/motions/motion/${motion.id}/manage_comments/`, { section_id: section.id });
}
} }

View File

@ -130,15 +130,13 @@ export class MotionCommentsComponent extends BaseViewComponent {
} }
/** /**
* Saves the comment. Makes a request to the server. * Saves the comment.
* *
* @param section The section for the comment to save * @param section The section for the comment to save
*/ */
public async saveComment(section: ViewMotionCommentSection): Promise<void> { public saveComment(section: ViewMotionCommentSection): void {
const commentText = this.commentForms[section.id].get('comment').value; const commentText = this.commentForms[section.id].get('comment').value;
const sectionId = section.id; this.commentRepo.saveComment(this.motion, section, commentText).then(
const motionId = this.motion.id;
await this.commentRepo.saveComment(motionId, sectionId, commentText).then(
() => { () => {
this.cancelEditing(section); this.cancelEditing(section);
}, },

View File

@ -97,14 +97,10 @@ def in_some_groups(user_id: int, groups: List[int]) -> bool:
""" """
Checks that user is in at least one given group. Groups can be given as a list Checks that user is in at least one given group. Groups can be given as a list
of ids or group instances. If the user is in the admin group (pk = 2) the result of ids or group instances. If the user is in the admin group (pk = 2) the result
is always true. is always true, even if no groups are given.
user_id 0 means anonymous user. user_id 0 means anonymous user.
""" """
if len(groups) == 0:
return False # early end here, if no groups are given.
# Convert user to right type # Convert user to right type
# TODO: Remove this and make use, that user has always the right type # TODO: Remove this and make use, that user has always the right type
user_id = user_to_user_id(user_id) user_id = user_to_user_id(user_id)
@ -115,14 +111,10 @@ async def async_in_some_groups(user_id: int, groups: List[int]) -> bool:
""" """
Checks that user is in at least one given group. Groups can be given as a list Checks that user is in at least one given group. Groups can be given as a list
of ids. If the user is in the admin group (pk = 2) the result of ids. If the user is in the admin group (pk = 2) the result
is always true. is always true, even if no groups are given.
user_id 0 means anonymous user. user_id 0 means anonymous user.
""" """
if not len(groups):
return False # early end here, if no groups are given.
if not user_id and not await async_anonymous_is_enabled(): if not user_id and not await async_anonymous_is_enabled():
in_some_groups = False in_some_groups = False
elif not user_id: elif not user_id: