Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Fukász Rómeó Ervin
/
cloud
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
e1b8a82b
authored
Oct 06, 2013
by
Őry Máté
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
dashboard: separate view util functions
parent
8232e6dd
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
49 deletions
+51
-49
circle/dashboard/utils.py
+50
-0
circle/dashboard/views.py
+1
-49
No files found.
circle/dashboard/utils.py
0 → 100644
View file @
e1b8a82b
from
django.core.urlresolvers
import
reverse
from
guardian.shortcuts
import
(
get_users_with_perms
,
get_groups_with_perms
,
get_perms
,
remove_perm
,
assign_perm
)
def
split
(
t
,
at
):
"""
Split collection at first occurance of given element.
>>> split("FooBar", "B")
('Foo', 'Bar')
>>> split(range(5), 2)
([0, 1], [2, 3, 4])
"""
pos
=
t
.
index
(
at
)
return
t
[:
pos
],
t
[
pos
:]
def
first_common_element
(
a
,
b
):
for
i
in
a
:
if
i
in
b
:
return
i
return
None
def
get_acl_data
(
obj
):
levels
=
obj
.
_meta
.
permissions
levelids
=
[
id
for
(
id
,
name
)
in
levels
]
users
=
get_users_with_perms
(
obj
,
with_group_users
=
False
)
users
=
[{
'user'
:
u
,
'perm'
:
first_common_element
(
levelids
,
get_perms
(
u
,
obj
))}
for
u
in
users
]
groups
=
get_groups_with_perms
(
obj
)
groups
=
[{
'group'
:
g
,
'perm'
:
first_common_element
(
levelids
,
get_perms
(
g
,
obj
))}
for
g
in
groups
]
return
{
'users'
:
users
,
'groups'
:
groups
,
'levels'
:
levels
,
'url'
:
reverse
(
'dashboard.views.vm-acl'
,
args
=
[
obj
.
pk
])}
def
set_acl_level
(
obj
,
whom
,
level
):
levels
=
obj
.
_meta
.
permissions
levelids
=
[
id
for
(
id
,
name
)
in
levels
]
to_remove
,
to_add
=
split
(
levelids
,
level
)
for
p
in
to_remove
:
remove_perm
(
p
,
whom
,
obj
)
for
p
in
to_add
:
assign_perm
(
p
,
whom
,
obj
)
circle/dashboard/views.py
View file @
e1b8a82b
...
@@ -2,18 +2,16 @@ import re
...
@@ -2,18 +2,16 @@ import re
from
django.contrib.auth.models
import
User
,
Group
from
django.contrib.auth.models
import
User
,
Group
from
django.core
import
signing
from
django.core
import
signing
from
django.core.urlresolvers
import
reverse
from
django.shortcuts
import
redirect
from
django.shortcuts
import
redirect
from
django.views.generic
import
TemplateView
,
DetailView
,
View
from
django.views.generic
import
TemplateView
,
DetailView
,
View
from
django.views.generic.detail
import
SingleObjectMixin
from
django.views.generic.detail
import
SingleObjectMixin
from
django_tables2
import
SingleTableView
from
django_tables2
import
SingleTableView
from
guardian.shortcuts
import
(
get_users_with_perms
,
get_groups_with_perms
,
get_perms
,
remove_perm
,
assign_perm
)
from
vm.models
import
Instance
from
vm.models
import
Instance
from
.tables
import
VmListTable
from
.tables
import
VmListTable
from
.utils
import
get_acl_data
,
set_acl_level
class
IndexView
(
TemplateView
):
class
IndexView
(
TemplateView
):
...
@@ -32,52 +30,6 @@ class IndexView(TemplateView):
...
@@ -32,52 +30,6 @@ class IndexView(TemplateView):
return
context
return
context
def
split
(
t
,
at
):
"""
Split collection at first occurance of given element.
>>> split("FooBar", "B")
('Foo', 'Bar')
>>> split(range(5), 2)
([0, 1], [2, 3, 4])
"""
pos
=
t
.
index
(
at
)
return
t
[:
pos
],
t
[
pos
:]
def
first_common_element
(
a
,
b
):
for
i
in
a
:
if
i
in
b
:
return
i
return
None
def
get_acl_data
(
obj
):
levels
=
obj
.
_meta
.
permissions
levelids
=
[
id
for
(
id
,
name
)
in
levels
]
users
=
get_users_with_perms
(
obj
,
with_group_users
=
False
)
users
=
[{
'user'
:
u
,
'perm'
:
first_common_element
(
levelids
,
get_perms
(
u
,
obj
))}
for
u
in
users
]
groups
=
get_groups_with_perms
(
obj
)
groups
=
[{
'group'
:
g
,
'perm'
:
first_common_element
(
levelids
,
get_perms
(
g
,
obj
))}
for
g
in
groups
]
return
{
'users'
:
users
,
'groups'
:
groups
,
'levels'
:
levels
,
'url'
:
reverse
(
'dashboard.views.vm-acl'
,
args
=
[
obj
.
pk
])}
def
set_acl_level
(
obj
,
whom
,
level
):
levels
=
obj
.
_meta
.
permissions
levelids
=
[
id
for
(
id
,
name
)
in
levels
]
to_remove
,
to_add
=
split
(
levelids
,
level
)
for
p
in
to_remove
:
remove_perm
(
p
,
whom
,
obj
)
for
p
in
to_add
:
assign_perm
(
p
,
whom
,
obj
)
class
VmDetailView
(
DetailView
):
class
VmDetailView
(
DetailView
):
template_name
=
"dashboard/vm-detail.html"
template_name
=
"dashboard/vm-detail.html"
model
=
Instance
model
=
Instance
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment