Exemplo n.º 1
0
def make_auth_response_data(user) -> dict:
    """
    Given a domain and user, creates data structure
    using python dict containing a representation
    of the logged user.
    """
    serializer = UserSerializer(user)
    data = dict(serializer.data)
    data["auth_token"] = get_token_for_user(user, "authentication")
    return data
Exemplo n.º 2
0
class MembershipSerializer(ModelSerializer):
    role_name = serializers.CharField(source='role.name',
                                      required=False,
                                      read_only=True)
    full_name = serializers.CharField(source='user.get_full_name',
                                      required=False,
                                      read_only=True)
    user_email = serializers.EmailField(source='user.email',
                                        required=False,
                                        read_only=True)
    email = serializers.EmailField(required=True)
    color = serializers.CharField(source='user.color',
                                  required=False,
                                  read_only=True)
    photo = serializers.SerializerMethodField("get_photo")
    project_name = serializers.SerializerMethodField("get_project_name")
    project_slug = serializers.SerializerMethodField("get_project_slug")
    invited_by = UserSerializer(read_only=True)

    class Meta:
        model = models.Membership
        read_only_fields = ("user", )
        exclude = ("token", )

    def get_photo(self, project):
        return get_photo_or_gravatar_url(project.user)

    def get_project_name(self, obj):
        return obj.project.name if obj and obj.project else ""

    def get_project_slug(self, obj):
        return obj.project.slug if obj and obj.project else ""

    def validate_email(self, attrs, source):
        project = attrs["project"]
        email = attrs[source]

        qs = models.Membership.objects.all()

        # If self.object is not None, the serializer is in update
        # mode, and for it, it should exclude self.
        if self.object:
            qs = qs.exclude(pk=self.object.pk)

        qs = qs.filter(
            Q(project_id=project.id, user__email=email)
            | Q(project_id=project.id, email=email))

        if qs.count() > 0:
            raise serializers.ValidationError(
                _("Email address is already taken"))

        return attrs
Exemplo n.º 3
0
    def attach_user_info_to_comment_versions(self):
        if not self.comment_versions:
            return

        from taiga.users.serializers import UserSerializer

        user_ids = [v["user"]["id"] for v in self.comment_versions if "user" in v and "id" in v["user"]]
        users_by_id = {u.id: u for u in get_user_model().objects.filter(id__in=user_ids)}

        for version in self.comment_versions:
            user = users_by_id.get(version["user"]["id"], None)
            if user:
                version["user"] = UserSerializer(user).data
Exemplo n.º 4
0
def make_auth_response_data(domain, user) -> dict:
    """
    Given a domain and user, creates data structure
    using python dict containing a representation
    of the logged user.
    """
    serializer = UserSerializer(user)
    data = dict(serializer.data)

    data['is_site_owner'] = domain.user_is_owner(user)
    data['is_site_staff'] = domain.user_is_staff(user)
    data["auth_token"] = get_token_for_user(user)

    return data
Exemplo n.º 5
0
def test_user_update(client, data):
    url = reverse('users-detail', kwargs={"pk": data.registered_user.pk})

    users = [
        None,
        data.registered_user,
        data.other_user,
        data.superuser,
    ]

    user_data = UserSerializer(data.registered_user).data
    user_data["full_name"] = "test"
    user_data = json.dumps(user_data)
    results = helper_test_http_method(client, 'put', url, user_data, users)
    assert results == [401, 200, 403, 200]
Exemplo n.º 6
0
 def get_users(self, obj):
     return UserSerializer(obj.members.all(), many=True).data
Exemplo n.º 7
0
class MembershipSerializer(serializers.ModelSerializer):
    role_name = serializers.CharField(source='role.name',
                                      required=False,
                                      read_only=True,
                                      i18n=True)
    full_name = serializers.CharField(source='user.get_full_name',
                                      required=False,
                                      read_only=True)
    user_email = serializers.EmailField(source='user.email',
                                        required=False,
                                        read_only=True)
    is_user_active = serializers.BooleanField(source='user.is_active',
                                              required=False,
                                              read_only=True)
    email = serializers.EmailField(required=True)
    color = serializers.CharField(source='user.color',
                                  required=False,
                                  read_only=True)
    photo = serializers.SerializerMethodField("get_photo")
    project_name = serializers.SerializerMethodField("get_project_name")
    project_slug = serializers.SerializerMethodField("get_project_slug")
    invited_by = UserSerializer(read_only=True)

    class Meta:
        model = models.Membership
        # IMPORTANT: Maintain the MembershipAdminSerializer Meta up to date
        # with this info (excluding here user_email and email)
        read_only_fields = ("user", )
        exclude = ("token", "user_email", "email")

    def get_photo(self, project):
        return get_photo_or_gravatar_url(project.user)

    def get_project_name(self, obj):
        return obj.project.name if obj and obj.project else ""

    def get_project_slug(self, obj):
        return obj.project.slug if obj and obj.project else ""

    def validate_email(self, attrs, source):
        project = attrs.get("project", None)
        if project is None:
            project = self.object.project

        email = attrs[source]

        qs = models.Membership.objects.all()

        # If self.object is not None, the serializer is in update
        # mode, and for it, it should exclude self.
        if self.object:
            qs = qs.exclude(pk=self.object.pk)

        qs = qs.filter(
            Q(project_id=project.id, user__email=email)
            | Q(project_id=project.id, email=email))

        if qs.count() > 0:
            raise serializers.ValidationError(
                _("Email address is already taken"))

        return attrs

    def validate_role(self, attrs, source):
        project = attrs.get("project", None)
        if project is None:
            project = self.object.project

        role = attrs[source]

        if project.roles.filter(id=role.id).count() == 0:
            raise serializers.ValidationError(
                _("Invalid role for the project"))

        return attrs

    def validate_is_owner(self, attrs, source):
        is_owner = attrs[source]
        project = attrs.get("project", None)
        if project is None:
            project = self.object.project

        if (self.object and not services.project_has_valid_owners(
                project, exclude_user=self.object.user)):
            raise serializers.ValidationError(
                _("At least one of the user must be an active admin"))

        return attrs
Exemplo n.º 8
0
class DomainMemberSerializer(serializers.ModelSerializer):
    user = UserSerializer()

    class Meta:
        model = DomainMember