Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
CIRCLE
/
cloud
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
94
Merge Requests
10
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
cd848582
authored
Nov 11, 2017
by
Czémán Arnold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
common: add CreateLimitedResourceMixin
parent
f7d27476
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
1 deletions
+30
-1
circle/common/views.py
+30
-1
No files found.
circle/common/views.py
View file @
cd848582
...
...
@@ -19,8 +19,11 @@ from sys import exc_info
import
logging
from
django.shortcuts
import
render_to_response
from
django.shortcuts
import
render_to_response
,
redirect
from
django.contrib
import
messages
from
django.template
import
RequestContext
from
django.http
import
JsonResponse
from
django.utils.translation
import
ugettext_lazy
as
_
from
.models
import
HumanReadableException
...
...
@@ -59,3 +62,29 @@ def handler403(request):
resp
=
render_to_response
(
"403.html"
,
ctx
)
resp
.
status_code
=
403
return
resp
class
CreateLimitedResourceMixin
(
object
):
resource_name
=
None
model
=
None
profile_attribute
=
None
def
post
(
self
,
*
args
,
**
kwargs
):
user
=
self
.
request
.
user
try
:
limit
=
getattr
(
user
.
profile
,
self
.
profile_attribute
)
except
Exception
as
e
:
logger
.
debug
(
'No profile or
%
s:
%
s'
,
self
.
profile_attribute
,
e
)
else
:
current
=
self
.
model
.
objects
.
filter
(
owner
=
user
)
.
count
()
logger
.
debug
(
'
%
s current use:
%
d, limit:
%
d'
,
self
.
resource_name
,
current
,
limit
)
if
current
>
limit
:
messages
.
error
(
self
.
request
,
_
(
'
%
s limit (
%
d) exceeded.'
)
%
(
self
.
resource_name
,
limit
))
if
self
.
request
.
is_ajax
():
return
JsonResponse
({
'redirect'
:
'/'
})
else
:
return
redirect
(
'/'
)
return
super
(
CreateLimitedResourceMixin
,
self
)
.
post
(
*
args
,
**
kwargs
)
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