This repository has been archived by the owner on Mar 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Workspaces #32
Draft
b0g3r
wants to merge
9
commits into
develop
Choose a base branch
from
feature/workspaces
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Workspaces #32
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0ccfe8f
Add basic workspace models and relevant API endpoints
b0g3r 18a9b92
Add vk gate support (#12)
vladimirshkoda 76606bf
Add OAuth and registration (#13)
b0g3r 2c7a6e1
Merge branch 'develop' into feature/workspaces
b0g3r 61e4e81
Add documentation and other little things :moon_with_face:
b0g3r 53cc5ac
Fix small issues after force-push :see_no_evil:
b0g3r 5295cf4
Add superuser permission
b0g3r ce6c22b
Add current workspace injection to publication creating
b0g3r f980c13
Rewrite to dry permission framework
b0g3r File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,45 @@ | ||||||
from api.models import Workspace | ||||||
|
||||||
|
||||||
class GlobalWorkspaceMiddleware(object): | ||||||
""" | ||||||
Add request-relevant workspace object to request object. | ||||||
|
||||||
Because most of requests to our API tied to specific workspace (e.g. | ||||||
work with publications), the middleware saves us from a lot of repeated | ||||||
code for extracting workspace by request data. | ||||||
""" | ||||||
|
||||||
def __init__(self, get_response): | ||||||
""" | ||||||
Standard interface of django middleware. | ||||||
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.
Suggested change
|
||||||
|
||||||
See more: | ||||||
https://docs.djangoproject.com/en/2.1/topics/http/middleware/#init-get-response | ||||||
""" | ||||||
self.get_response = get_response | ||||||
|
||||||
def __call__(self, request): | ||||||
""" | ||||||
Executed before view and other middlewares are called. | ||||||
|
||||||
And this method does nothing. | ||||||
""" | ||||||
return self.get_response(request) | ||||||
|
||||||
def process_view(self, request, view_func, view_args, view_kwargs): | ||||||
""" | ||||||
One of the django middleware hook. | ||||||
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.
Suggested change
|
||||||
|
||||||
Uses here for inject relevant workspace to request objects. | ||||||
Search Workspace instance by url params that a router usually | ||||||
generates. | ||||||
|
||||||
See more about this middleware hook: | ||||||
https://docs.djangoproject.com/en/2.1/topics/http/middleware/#process-view | ||||||
""" | ||||||
workspace_name = view_kwargs.get('workspace_pk') or view_kwargs.get('workspace_name') | ||||||
if workspace_name: | ||||||
workspace = Workspace.objects.filter(name=workspace_name).first() | ||||||
if workspace: | ||||||
request.workspace = workspace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Generated by Django 2.1.5 on 2019-02-06 19:09 | ||
|
||
from django.conf import settings | ||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('api', '0002_auto_20190128_2219'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='Workspace', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('name', models.SlugField(unique=True)), | ||
('created_at', models.DateTimeField(auto_now_add=True)), | ||
('updated_at', models.DateTimeField(auto_now=True)), | ||
], | ||
), | ||
migrations.CreateModel( | ||
name='WorkspaceMember', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('role', models.CharField(choices=[('publisher', 'Publisher: only create and edit publications'), ('admin', 'Admin: also can edit platforms and members')], default='publisher', max_length=64)), | ||
('created_at', models.DateTimeField(auto_now_add=True)), | ||
('updated_at', models.DateTimeField(auto_now=True)), | ||
('member', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)), | ||
('workspace', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='api.Workspace')), | ||
], | ||
), | ||
migrations.AddField( | ||
model_name='publication', | ||
name='workspace', | ||
field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, to='api.Workspace'), | ||
preserve_default=False, | ||
), | ||
migrations.AlterUniqueTogether( | ||
name='workspacemember', | ||
unique_together={('member', 'workspace')}, | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
from api.models.platform_settings import PlatformPost # noqa: F401 | ||
from api.models.publications import Publication # noqa: F401 | ||
from api.models.platform_post import PlatformPost # noqa: F401 | ||
from api.models.publication import Publication # noqa: F401 | ||
from api.models.workspace import Workspace # noqa: F401 | ||
from api.models.workspace_member import WorkspaceMember # noqa: F401 |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
from django.db import models | ||
from rest_framework.request import Request | ||
|
||
from api import permissions | ||
|
||
|
||
class Workspace(models.Model): | ||
""" | ||
Workspace — space with members, publications and tuned platforms. Has a unique name. | ||
""" | ||
|
||
name = models.SlugField(unique=True) | ||
|
||
created_at = models.DateTimeField(auto_now_add=True, editable=False) | ||
updated_at = models.DateTimeField(auto_now=True, editable=False) | ||
|
||
@staticmethod | ||
def has_list_permission(request: Request) -> bool: | ||
return permissions.check_authenticated(request) | ||
|
||
@staticmethod | ||
def has_create_permission(request: Request) -> bool: | ||
return permissions.check_authenticated(request) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from django.conf import settings | ||
from django.db import models | ||
from rest_framework.request import Request | ||
|
||
from api import permissions | ||
from api.models.workspace import Workspace | ||
|
||
PUBLISHER_ROLE = 'publisher' | ||
ADMIN_ROLE = 'admin' | ||
WORKSPACE_ROLES = [ | ||
(PUBLISHER_ROLE, 'Publisher: only create and edit publications'), | ||
(ADMIN_ROLE, 'Admin: also can edit platforms and members'), | ||
] | ||
|
||
|
||
class WorkspaceMember(models.Model): | ||
""" | ||
Many-to-many junction table user <-> workspace with role. | ||
""" | ||
|
||
member = models.ForeignKey( | ||
settings.AUTH_USER_MODEL, | ||
on_delete=models.CASCADE, | ||
null=False, | ||
) | ||
workspace = models.ForeignKey( | ||
Workspace, | ||
on_delete=models.CASCADE, | ||
null=False, | ||
) | ||
role = models.CharField( | ||
choices=WORKSPACE_ROLES, | ||
default=PUBLISHER_ROLE, | ||
null=False, | ||
max_length=64, # noqa: Z432 | ||
) | ||
|
||
created_at = models.DateTimeField(auto_now_add=True, editable=False) | ||
updated_at = models.DateTimeField(auto_now=True, editable=False) | ||
|
||
@staticmethod | ||
def has_list_permission(request: Request) -> bool: | ||
return permissions.check_workspace_member(request) | ||
|
||
class Meta(object): | ||
unique_together = ('member', 'workspace') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from rest_framework import permissions | ||
from rest_framework.request import Request | ||
|
||
from api.models import WorkspaceMember | ||
from api.models.workspace_member import ADMIN_ROLE | ||
|
||
|
||
def check_authenticated(request: Request): | ||
return request.user.is_authenticated | ||
|
||
|
||
def check_workspace_admin(request: Request): | ||
""" | ||
Checks that the user role is admin role in current workspace. | ||
""" | ||
is_workspace_admin = WorkspaceMember.objects.filter( | ||
workspace=request.workspace, | ||
member=request.user, | ||
role=ADMIN_ROLE, | ||
).exists() | ||
return is_workspace_admin | ||
|
||
|
||
def check_workspace_member(request: Request): | ||
""" | ||
Just check user membership in current workspace. | ||
""" | ||
is_workspace_member = WorkspaceMember.objects.filter( | ||
workspace=request.workspace, | ||
member=request.user, | ||
).exists() | ||
return is_workspace_member | ||
|
||
|
||
def check_superuser(request: Request): | ||
""" | ||
Check standard django is_superuser flag :shrug:. | ||
|
||
More info: | ||
https://docs.djangoproject.com/en/2.1/ref/contrib/auth/#django.contrib.auth.models.User.is_superuser | ||
""" | ||
is_superuser = request.user.is_authenticated() and request.user.is_superuser | ||
return is_superuser |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.