-
Notifications
You must be signed in to change notification settings - Fork 24
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
Jkmarx/add primary group api #2820
Changes from 5 commits
78a52ca
b087b8c
7949c50
4391520
d41953a
cf9881c
84c9b54
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,18 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import models, migrations | ||
from django.db import migrations | ||
from django.utils import timezone | ||
|
||
|
||
from core.models import DataSet, UserProfile | ||
|
||
|
||
def create_initial_site_statistics(apps, schema_editor): | ||
SiteStatistics = apps.get_model("core", "SiteStatistics") | ||
User = apps.get_model("auth", "User") | ||
ExtendedGroup = apps.get_model("core", "ExtendedGroup") | ||
WorkflowTool = apps.get_model("tool_manager", "WorkflowTool") | ||
VisualizationTool = apps.get_model("tool_manager", "VisualizationTool") | ||
UserProfile = apps.get_model("core", "UserProfile") | ||
DataSet = apps.get_model("core", "DataSet") | ||
|
||
SiteStatistics.objects.create( | ||
run_date=timezone.now(), | ||
|
@@ -22,7 +21,9 @@ def create_initial_site_statistics(apps, schema_editor): | |
[dataset for dataset in DataSet.objects.all() if dataset.shared] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry, I forgot to push my update. |
||
), | ||
users_created=User.objects.count(), | ||
groups_created=ExtendedGroup.objects.exclude(manager_group=None).count(), | ||
groups_created=ExtendedGroup.objects.exclude( | ||
manager_group=None | ||
).count(), | ||
unique_user_logins=User.objects.filter( | ||
last_login__lte=timezone.now() | ||
).count(), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('auth', '0006_require_contenttypes_0002'), | ||
('core', '0026_auto_20180608_1123'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='userprofile', | ||
name='primary_group', | ||
field=models.ForeignKey(blank=True, to='auth.Group', null=True), | ||
), | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
from data_set_manager.models import Node | ||
from file_store.models import FileStoreItem | ||
|
||
from .models import DataSet, Workflow | ||
from .models import DataSet, UserProfile, Workflow | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
@@ -62,6 +62,38 @@ def partial_update(self, instance, validated_data): | |
return instance | ||
|
||
|
||
class UserProfileSerializer(serializers.ModelSerializer): | ||
|
||
class Meta: | ||
model = UserProfile | ||
fields = ['primary_group'] | ||
|
||
def validate_primary_group(self, group): | ||
user = self.context.get('request').user | ||
if user.id in group.user_set.values_list('id', flat=True): | ||
pass | ||
else: | ||
raise serializers.ValidationError( | ||
'User is not a member of group, %s', group | ||
) | ||
|
||
if group.name != 'Public': | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return group | ||
else: | ||
raise serializers.ValidationError('Primary group can not be ' | ||
'the Public group') | ||
|
||
def partial_update(self, instance, validated_data): | ||
""" | ||
Update and return an existing `UserProfile` instance, given the | ||
validated data. | ||
""" | ||
instance.primary_group = validated_data.get('primary_group', | ||
instance.primary_group) | ||
instance.save() | ||
return instance | ||
|
||
|
||
class WorkflowSerializer(serializers.HyperlinkedModelSerializer): | ||
instance = serializers.HyperlinkedIdentityField( | ||
view_name='workflow-detail') | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,8 @@ | |
from .models import (Analysis, CustomRegistrationProfile, DataSet, | ||
ExtendedGroup, Invitation, Ontology, Project, | ||
UserProfile, Workflow, WorkflowEngine) | ||
from .serializers import DataSetSerializer, NodeSerializer, WorkflowSerializer | ||
from .serializers import (DataSetSerializer, NodeSerializer, | ||
UserProfileSerializer, WorkflowSerializer) | ||
from .utils import (api_error_response, get_data_sets_annotations, | ||
get_resources_for_user) | ||
|
||
|
@@ -1079,3 +1080,49 @@ def post(self, request): | |
token["Region"] = region | ||
|
||
return Response(token) | ||
|
||
|
||
class UserProfileViewSet(APIView): | ||
"""API endpoint that allows for UserProfiles to be edits. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
--- | ||
#YAML | ||
|
||
PATCH: | ||
parameters_strategy: | ||
form: replace | ||
query: merge | ||
|
||
parameters: | ||
- name: uuid | ||
description: User profile uuid used as an identifier | ||
type: string | ||
paramType: path | ||
required: true | ||
- name: primary_group | ||
description: group id | ||
type: int | ||
paramType: form | ||
required: false | ||
... | ||
""" | ||
http_method_names = ["patch"] | ||
|
||
def patch(self, request, uuid): | ||
if not request.user.is_anonymous(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about flipping the first ifs logic here:
|
||
serializer = UserProfileSerializer(request.user.profile, | ||
data=request.data, | ||
partial=True, | ||
context={'request': request}) | ||
|
||
if serializer.is_valid(): | ||
serializer.save() | ||
return Response( | ||
serializer.data, status=status.HTTP_202_ACCEPTED | ||
) | ||
return Response( | ||
serializer.errors, status=status.HTTP_400_BAD_REQUEST | ||
) | ||
else: | ||
return Response( | ||
self.user, status=status.HTTP_401_UNAUTHORIZED | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add
from guardian.shortcuts import get_groups_with_perms