Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Community Surveys and Scoring #1984

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions galaxy_ng/app/access_control/access_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -820,3 +820,8 @@ def is_namespace_owner(self, request, viewset, action):
return True

return False


class SurveyAccessPolicy(AccessPolicyBase):

NAME = "SurveyAccessPolicy"
3 changes: 3 additions & 0 deletions galaxy_ng/app/access_control/statements/standalone.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# policies.

from galaxy_ng.app.access_control.statements.legacy import LEGACY_STATEMENTS
from galaxy_ng.app.access_control.statements.survey import SURVEY_STATEMENTS


_collection_statements = [
{
Expand Down Expand Up @@ -417,3 +419,4 @@
}

STANDALONE_STATEMENTS.update(LEGACY_STATEMENTS)
STANDALONE_STATEMENTS.update(SURVEY_STATEMENTS)
33 changes: 33 additions & 0 deletions galaxy_ng/app/access_control/statements/survey.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
SURVEY_STATEMENTS = {
"SurveyAccessPolicy": [
{
"action": [
"get",
"list",
"retrieve",
"create",
],
"principal": "authenticated",
"effect": "allow",
},
],
"SurveyRollupAccessPolicy": [
{
"action": [
"get",
"list",
"retrieve",
],
"principal": "*",
"effect": "allow",
},
{
"action": [
"create",
"update",
],
"principal": "authenticated",
"effect": "allow",
},
]
}
54 changes: 54 additions & 0 deletions galaxy_ng/app/api/v1/filtersets/scores.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from django_filters import filters
from django_filters.rest_framework import filterset

from galaxy_ng.app.api.v1.models import LegacyRoleSurveyRollup
from galaxy_ng.app.api.v1.models import CollectionSurveyRollup


class BaseSurveyRollupFilter(filterset.FilterSet):

sort = filters.OrderingFilter(
fields=(
('created', 'created'),
)
)


class LegacyRoleSurveyRollupFilter(BaseSurveyRollupFilter):

role = filters.CharFilter(method='role_filter')
namespace = filters.CharFilter(method='namespace_filter')
name = filters.CharFilter(method='name_filter')

class Meta:
model = LegacyRoleSurveyRollup
fields = ['created', 'role']

def role_filter(self, queryset, name, value):
return queryset.filter(role__id=int(value))

def namespace_filter(self, queryset, name, value):
return queryset.filter(role__namespace__name=value)

def name_filter(self, queryset, name, value):
return queryset.filter(role__name=value)


class CollectionSurveyRollupFilter(BaseSurveyRollupFilter):

collection = filters.CharFilter(method='collection_filter')
namespace = filters.CharFilter(method='namespace_filter')
name = filters.CharFilter(method='name_filter')

class Meta:
model = CollectionSurveyRollup
fields = ['created', 'collection']

def collection_filter(self, queryset, name, value):
return queryset.filter(collection__pulp_id=value)

def namespace_filter(self, queryset, name, value):
return queryset.filter(collection__namespace=value)

def name_filter(self, queryset, name, value):
return queryset.filter(collection__name=value)
53 changes: 53 additions & 0 deletions galaxy_ng/app/api/v1/filtersets/survey.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from django.db.models import Q
from django_filters import filters
from django_filters.rest_framework import filterset

from galaxy_ng.app.api.v1.models import LegacyRoleSurvey
from galaxy_ng.app.api.v1.models import CollectionSurvey


class BaseSurveyFilter(filterset.FilterSet):

user = filters.CharFilter(method='user_filter')

sort = filters.OrderingFilter(
fields=(
('created', 'created'),
)
)

def user_filter(self, queryset, name, value):

# allow filtering on uid and username ...
if value.isdigit():
queryset = queryset.filter(
Q(user__id=int(value)) | Q(user__username=value)
)
else:
queryset = queryset.filter(user__username=value)

return queryset


class LegacyRoleSurveyFilter(BaseSurveyFilter):

role = filters.CharFilter(method='role_filter')

class Meta:
model = LegacyRoleSurvey
fields = ['created', 'user', 'role']

def role_filter(self, queryset, name, value):
return queryset.filter(role__id=int(value))


class CollectionSurveyFilter(BaseSurveyFilter):

collection = filters.CharFilter(method='collection_filter')

class Meta:
model = CollectionSurvey
fields = ['created', 'user', 'collection']

def collection_filter(self, queryset, name, value):
return queryset.filter(collection__pulp_id=value)
68 changes: 68 additions & 0 deletions galaxy_ng/app/api/v1/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from django.db import models
from django.contrib.postgres.search import SearchVectorField
from django.contrib.postgres.indexes import GinIndex
from django.core.validators import MinValueValidator, MaxValueValidator

from galaxy_ng.app.models import Namespace
from galaxy_ng.app.models.auth import User

from pulpcore.plugin.models import Task
from pulp_ansible.app.models import Collection


"""
Expand Down Expand Up @@ -230,3 +232,69 @@ def add_log_record(self, log_record, state=None):
"level": log_record.levelname,
"time": log_record.created
})


class SurveyBase(models.Model):

class Meta:
abstract = True

created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)

docs = models.IntegerField(
null=True,
validators=[MinValueValidator(0), MaxValueValidator(5)]
)

ease_of_use = models.IntegerField(
null=True,
validators=[MinValueValidator(0), MaxValueValidator(5)]
)

does_what_it_says = models.IntegerField(
null=True,
validators=[MinValueValidator(0), MaxValueValidator(5)]
)

works_as_is = models.IntegerField(
null=True,
validators=[MinValueValidator(0), MaxValueValidator(5)]
)

used_in_production = models.IntegerField(
null=True,
validators=[MinValueValidator(0), MaxValueValidator(5)]
)


class CollectionSurvey(SurveyBase):
user = models.ForeignKey(User, on_delete=models.CASCADE)
collection = models.ForeignKey(Collection, on_delete=models.CASCADE)

class Meta:
unique_together = ('user', 'collection',)


class LegacyRoleSurvey(SurveyBase):
user = models.ForeignKey(User, on_delete=models.CASCADE)
role = models.ForeignKey(LegacyRole, on_delete=models.CASCADE)

class Meta:
unique_together = ('user', 'role',)


class CollectionSurveyRollup(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)

collection = models.ForeignKey(Collection, on_delete=models.CASCADE)
score = models.DecimalField(max_digits=5, decimal_places=2)


class LegacyRoleSurveyRollup(models.Model):
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)

role = models.ForeignKey(LegacyRole, on_delete=models.CASCADE)
score = models.DecimalField(max_digits=5, decimal_places=2)
126 changes: 126 additions & 0 deletions galaxy_ng/app/api/v1/serializers/survey.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
from rest_framework import serializers

from galaxy_ng.app.api.v1.models import (
CollectionSurvey,
CollectionSurveyRollup,
LegacyRoleSurvey,
LegacyRoleSurveyRollup,
)

from galaxy_ng.app.api.v1.utils.survey import SURVEY_FIELDS


class CollectionSurveySerializer(serializers.ModelSerializer):

responses = serializers.SerializerMethodField()
user = serializers.SerializerMethodField()
collection = serializers.SerializerMethodField()

class Meta:
model = CollectionSurvey
fields = [
'id',
'created',
'modified',
'collection',
'user',
'responses'
]

def get_user(self, obj):
return {
'id': obj.user.id,
'username': obj.user.username
}

def get_collection(self, obj):
return {
'id': obj.collection.pulp_id,
'namespace': obj.collection.namespace,
'name': obj.collection.name
}

def get_responses(self, obj):
return dict((k, getattr(obj, k)) for k in SURVEY_FIELDS)


class CollectionSurveyRollupSerializer(serializers.ModelSerializer):

namespace = serializers.SerializerMethodField()
name = serializers.SerializerMethodField()

class Meta:
model = CollectionSurveyRollup
fields = [
'id',
'created',
'modified',
'collection',
'namespace',
'name',
'score'
]

def get_namespace(self, obj):
return obj.collection.namespace

def get_name(self, obj):
return obj.collection.name


class LegacyRoleSurveySerializer(serializers.ModelSerializer):

responses = serializers.SerializerMethodField()
user = serializers.SerializerMethodField()
role = serializers.SerializerMethodField()

class Meta:
model = LegacyRoleSurvey
fields = [
'id',
'created',
'modified',
'role',
'user',
'responses',
]

def get_user(self, obj):
return {
'id': obj.user.id,
'username': obj.user.username
}

def get_role(self, obj):
return {
'id': obj.role.id,
'namespace': obj.role.namespace.name,
'name': obj.role.name
}

def get_responses(self, obj):
return dict((k, getattr(obj, k)) for k in SURVEY_FIELDS)


class LegacyRoleSurveyRollupSerializer(serializers.ModelSerializer):

namespace = serializers.SerializerMethodField()
name = serializers.SerializerMethodField()

class Meta:
model = LegacyRoleSurveyRollup
fields = [
'id',
'created',
'modified',
'role',
'namespace',
'name',
'score'
]

def get_namespace(self, obj):
return obj.role.namespace.name

def get_name(self, obj):
return obj.role.name
Loading
Loading