Exemplo n.º 1
0
 def save(self, *args, **kwargs):
     current_user = get_current_user()
     if current_user:
         if self.pk is None:
             self.created_by = current_user
         self.modified_by = current_user
     super().save(*args, **kwargs)
Exemplo n.º 2
0
def entrycomment_assignment_signal(sender, instance, action, **kwargs):
    pk_set = kwargs.get('pk_set', [])
    # Gets the username from the request with a middleware helper
    user = get_current_user()
    if action == 'post_add' and pk_set and user:
        for receiver_user in pk_set:
            if Assignment.objects.filter(
                    entry_review_comment__id=instance.id,
                    created_for_id=receiver_user,
                    project=instance.entry.project,
            ).exists():
                continue
            Assignment.objects.create(
                content_object=instance,
                created_for_id=receiver_user,
                project=instance.entry.project,
                created_by=user,
            )

    elif action == 'post_remove' and pk_set and user:
        for receiver_user in pk_set:
            Assignment.objects.filter(
                entry_review_comment__id=instance.id,
                created_for_id=receiver_user,
            ).delete()
Exemplo n.º 3
0
def lead_assignment_signal(sender, instance, action, **kwargs):
    pk_set = kwargs.get('pk_set', [])
    # Gets the username from the request with a middleware helper
    user = get_current_user()
    if action == 'post_add' and pk_set and user:
        for receiver_user in pk_set:
            if Assignment.objects.filter(
                    lead__id=instance.id,
                    created_for_id=receiver_user,
                    project=instance.project,
            ).exists():
                continue
            Assignment.objects.create(
                content_object=instance,
                created_for_id=receiver_user,
                project=instance.project,
                created_by=user,
            )

    elif action == 'post_remove' and pk_set and user:
        for receiver_user in pk_set:
            Assignment.objects.filter(
                lead__id=instance.id,
                created_for_id=receiver_user,
            ).delete()

    # handling `post_clear` since single assignee is passed
    # though the api
    elif action == 'post_clear':
        Assignment.objects.filter(lead__id=instance.id).delete()
Exemplo n.º 4
0
 def validate(self, data):
     mentioned_users = data.get('mentioned_users')
     data['entry'] = entry = self._get_entry()
     # Check if all assignes are members
     if mentioned_users:
         selected_existing_members_count = (
             ProjectMembership.objects.filter(
                 project=entry.project,
                 member__in=mentioned_users).distinct('member').count())
         if selected_existing_members_count != len(mentioned_users):
             raise serializers.ValidationError({
                 'mentioned_users':
                 "Selected mentioned users don't belong to this project"
             })
     data['created_by'] = get_current_user()
     return data
Exemplo n.º 5
0
    def validate_comment_type(self, comment_type):
        if self.instance and self.instance.comment_type:  # No validation needed for edit
            return self.instance.comment_type

        if comment_type == EntryReviewComment.CommentType.COMMENT:
            return comment_type  # No additional validation/action required

        entry = self._get_entry()
        current_user = get_current_user()
        verified_by_qs = Entry.verified_by.through.objects.filter(
            entry=entry, user=current_user)

        if (comment_type in [
                EntryReviewComment.CommentType.CONTROL,
                EntryReviewComment.CommentType.UNCONTROL,
        ] and not ProjectMembership.objects.filter(
                project=entry.project,
                member=self.context['request'].user,
                badges__contains=[ProjectMembership.BadgeType.QA.value],
        ).exists()):
            raise serializers.ValidationError({
                'comment_type':
                'Controlled/UnControlled comment are only allowd by QA',
            })

        if comment_type == EntryReviewComment.CommentType.VERIFY:
            if verified_by_qs.exists():
                raise serializers.ValidationError(
                    {'comment_type': 'Already verified'})
            entry.verified_by.add(current_user)
        elif comment_type == EntryReviewComment.CommentType.UNVERIFY:
            if not verified_by_qs.exists():
                raise serializers.ValidationError(
                    {'comment_type': 'Need to be verified first'})
            entry.verified_by.remove(current_user)
        elif comment_type == EntryReviewComment.CommentType.CONTROL:
            if entry.controlled:
                raise serializers.ValidationError(
                    {'comment_type': 'Already controlled'})
            entry.control(current_user)
        elif comment_type == EntryReviewComment.CommentType.UNCONTROL:
            if not entry.controlled:
                raise serializers.ValidationError(
                    {'comment_type': 'Need to be controlled first'})
            entry.control(current_user, controlled=False)
        return comment_type
Exemplo n.º 6
0
 def save(self, commit=True):
     new_logo_file = self.cleaned_data.pop('update_logo_direct', None)
     instance = super().save(commit=False)
     if new_logo_file:
         mime_type = new_logo_file.content_type
         current_user = get_current_user()
         new_logo = File.objects.create(
             title=new_logo_file.name,
             mime_type=mime_type,
             created_by=current_user,
             modified_by=current_user,
             is_public=True,
         )
         new_logo.file.save(new_logo_file.name, new_logo_file)
         instance.logo = new_logo
     if commit:
         instance.save()
     return instance