Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
AE UTBM
Sith
Commits
e2311dcb
Commit
e2311dcb
authored
Jun 12, 2017
by
Krophil
Browse files
Format sas
parent
13ec91e7
Changes
4
Hide whitespace changes
Inline
Side-by-side
sas/admin.py
View file @
e2311dcb
...
...
@@ -30,5 +30,3 @@ from sas.models import *
admin
.
site
.
register
(
Album
)
# admin.site.register(Picture)
admin
.
site
.
register
(
PeoplePictureRelation
)
sas/models.py
View file @
e2311dcb
...
...
@@ -23,11 +23,9 @@
#
from
django.db
import
models
from
django.core.urlresolvers
import
reverse_lazy
,
reverse
from
django.conf
import
settings
from
django.core.urlresolvers
import
reverse
from
django.conf
import
settings
from
django.utils.translation
import
ugettext_lazy
as
_
from
django.core.files.base
import
ContentFile
from
PIL
import
Image
from
io
import
BytesIO
...
...
@@ -36,6 +34,7 @@ import os
from
core.models
import
SithFile
,
User
from
core.utils
import
resize_image
,
exif_auto_rotate
class
Picture
(
SithFile
):
class
Meta
:
proxy
=
True
...
...
@@ -50,12 +49,12 @@ class Picture(SithFile):
def
can_be_edited_by
(
self
,
user
):
# file = SithFile.objects.filter(id=self.id).first()
return
user
.
is_in_group
(
settings
.
SITH_GROUP_SAS_ADMIN_ID
)
# or user.can_edit(file)
return
user
.
is_in_group
(
settings
.
SITH_GROUP_SAS_ADMIN_ID
)
# or user.can_edit(file)
def
can_be_viewed_by
(
self
,
user
):
# file = SithFile.objects.filter(id=self.id).first()
return
self
.
can_be_edited_by
(
user
)
or
(
self
.
is_in_sas
and
self
.
is_moderated
and
user
.
was_subscribed
)
# or user.can_view(file)
user
.
was_subscribed
)
# or user.can_view(file)
def
get_download_url
(
self
):
return
reverse
(
'sas:download'
,
kwargs
=
{
'picture_id'
:
self
.
id
})
...
...
@@ -73,7 +72,8 @@ class Picture(SithFile):
im
=
Image
.
open
(
BytesIO
(
self
.
file
.
read
()))
try
:
im
=
exif_auto_rotate
(
im
)
except
:
pass
except
:
pass
file
=
resize_image
(
im
,
max
(
im
.
size
),
self
.
mime_type
.
split
(
'/'
)[
-
1
])
thumb
=
resize_image
(
im
,
200
,
self
.
mime_type
.
split
(
'/'
)[
-
1
])
compressed
=
resize_image
(
im
,
1200
,
self
.
mime_type
.
split
(
'/'
)[
-
1
])
...
...
@@ -102,17 +102,18 @@ class Picture(SithFile):
def
get_next
(
self
):
if
self
.
is_moderated
:
return
self
.
parent
.
children
.
filter
(
is_moderated
=
True
,
asked_for_removal
=
False
,
is_folder
=
False
,
id__gt
=
self
.
id
).
order_by
(
'id'
).
first
()
id__gt
=
self
.
id
).
order_by
(
'id'
).
first
()
else
:
return
Picture
.
objects
.
filter
(
id__gt
=
self
.
id
,
is_moderated
=
False
,
is_in_sas
=
True
).
order_by
(
'id'
).
first
()
def
get_previous
(
self
):
if
self
.
is_moderated
:
return
self
.
parent
.
children
.
filter
(
is_moderated
=
True
,
asked_for_removal
=
False
,
is_folder
=
False
,
id__lt
=
self
.
id
).
order_by
(
'id'
).
last
()
id__lt
=
self
.
id
).
order_by
(
'id'
).
last
()
else
:
return
Picture
.
objects
.
filter
(
id__lt
=
self
.
id
,
is_moderated
=
False
,
is_in_sas
=
True
).
order_by
(
'-id'
).
first
()
class
Album
(
SithFile
):
class
Meta
:
proxy
=
True
...
...
@@ -127,12 +128,12 @@ class Album(SithFile):
def
can_be_edited_by
(
self
,
user
):
# file = SithFile.objects.filter(id=self.id).first()
return
user
.
is_in_group
(
settings
.
SITH_GROUP_SAS_ADMIN_ID
)
# or user.can_edit(file)
return
user
.
is_in_group
(
settings
.
SITH_GROUP_SAS_ADMIN_ID
)
# or user.can_edit(file)
def
can_be_viewed_by
(
self
,
user
):
# file = SithFile.objects.filter(id=self.id).first()
return
self
.
can_be_edited_by
(
user
)
or
(
self
.
is_in_sas
and
self
.
is_moderated
and
user
.
was_subscribed
)
# or user.can_view(file)
user
.
was_subscribed
)
# or user.can_view(file)
def
get_absolute_url
(
self
):
return
reverse
(
'sas:album'
,
kwargs
=
{
'album_id'
:
self
.
id
})
...
...
@@ -148,6 +149,7 @@ class Album(SithFile):
self
.
file
.
name
=
self
.
name
+
'/thumb.jpg'
self
.
save
()
class
PeoplePictureRelation
(
models
.
Model
):
"""
The PeoplePictureRelation class makes the connection between User and Picture
...
...
sas/urls.py
View file @
e2311dcb
...
...
@@ -22,7 +22,7 @@
#
#
from
django.conf.urls
import
url
,
include
from
django.conf.urls
import
url
from
sas.views
import
*
...
...
@@ -40,4 +40,3 @@ urlpatterns = [
# url(r'^album/new$', AlbumCreateView.as_view(), name='album_new'),
# url(r'^(?P<club_id>[0-9]+)/$', ClubView.as_view(), name='club_view'),
]
sas/views.py
View file @
e2311dcb
...
...
@@ -22,35 +22,30 @@
#
#
from
django.shortcuts
import
render
,
redirect
from
django.http
import
HttpResponse
Redirect
,
HttpResponse
from
django.shortcuts
import
redirect
from
django.http
import
HttpResponse
from
django.core.urlresolvers
import
reverse_lazy
,
reverse
from
django.views.generic
import
ListView
,
DetailView
,
Redirect
View
,
TemplateView
from
django.views.generic.edit
import
UpdateView
,
CreateView
,
DeleteView
,
ProcessFormView
,
FormMixin
,
FormView
from
django.views.generic
import
Detail
View
,
TemplateView
from
django.views.generic.edit
import
UpdateView
,
FormMixin
,
FormView
from
django.utils.translation
import
ugettext_lazy
as
_
from
django.utils
import
timezone
from
django.conf
import
settings
from
django.forms.models
import
modelform_factory
from
django
import
forms
from
django.core.exceptions
import
PermissionDenied
from
ajax_select
import
make_ajax_form
,
make_ajax_field
from
ajax_select.fields
import
AutoCompleteSelectField
,
AutoCompleteSelectMultipleField
from
ajax_select
import
make_ajax_field
from
ajax_select.fields
import
AutoCompleteSelectMultipleField
from
io
import
BytesIO
from
PIL
import
Image
from
core.views
import
CanViewMixin
,
CanEditMixin
,
CanEditPropMixin
,
CanCreateMixin
,
TabedViewMixin
from
core.views.forms
import
SelectUser
,
LoginForm
,
SelectDate
,
SelectDateTime
from
core.views
import
CanViewMixin
,
CanEditMixin
from
core.views.files
import
send_file
,
FileView
from
core.models
import
SithFile
,
User
,
Notification
,
RealGroup
from
sas.models
import
Picture
,
Album
,
PeoplePictureRelation
class
SASForm
(
forms
.
Form
):
album_name
=
forms
.
CharField
(
label
=
_
(
"Add a new album"
),
max_length
=
30
,
required
=
False
)
images
=
forms
.
ImageField
(
widget
=
forms
.
ClearableFileInput
(
attrs
=
{
'multiple'
:
True
}),
label
=
_
(
"Upload images"
),
required
=
False
)
required
=
False
)
def
process
(
self
,
parent
,
owner
,
files
,
automodere
=
False
):
notif
=
False
...
...
@@ -62,10 +57,10 @@ class SASForm(forms.Form):
notif
=
not
automodere
except
Exception
as
e
:
self
.
add_error
(
None
,
_
(
"Error creating album %(album)s: %(msg)s"
)
%
{
'album'
:
self
.
cleaned_data
[
'album_name'
],
'msg'
:
repr
(
e
)})
{
'album'
:
self
.
cleaned_data
[
'album_name'
],
'msg'
:
repr
(
e
)})
for
f
in
files
:
new_file
=
Picture
(
parent
=
parent
,
name
=
f
.
name
,
file
=
f
,
owner
=
owner
,
mime_type
=
f
.
content_type
,
size
=
f
.
_size
,
is_folder
=
False
,
is_moderated
=
automodere
)
is_folder
=
False
,
is_moderated
=
automodere
)
if
automodere
:
new_file
.
moderator
=
owner
try
:
...
...
@@ -80,6 +75,7 @@ class SASForm(forms.Form):
if
not
u
.
notifications
.
filter
(
type
=
"SAS_MODERATION"
,
viewed
=
False
).
exists
():
Notification
(
user
=
u
,
url
=
reverse
(
"sas:moderation"
),
type
=
"SAS_MODERATION"
).
save
()
class
RelationForm
(
forms
.
ModelForm
):
class
Meta
:
model
=
PeoplePictureRelation
...
...
@@ -87,6 +83,7 @@ class RelationForm(forms.ModelForm):
widgets
=
{
'picture'
:
forms
.
HiddenInput
}
users
=
AutoCompleteSelectMultipleField
(
'users'
,
show_help_text
=
False
,
help_text
=
""
,
label
=
_
(
"Add user"
),
required
=
False
)
class
SASMainView
(
FormView
):
form_class
=
SASForm
template_name
=
"sas/main.jinja"
...
...
@@ -112,6 +109,7 @@ class SASMainView(FormView):
kwargs
[
'latest'
]
=
SithFile
.
objects
.
filter
(
is_in_sas
=
True
,
is_folder
=
True
,
is_moderated
=
True
).
order_by
(
'-id'
)[:
5
]
return
kwargs
class
PictureView
(
CanViewMixin
,
DetailView
,
FormMixin
):
model
=
Picture
form_class
=
RelationForm
...
...
@@ -133,7 +131,8 @@ class PictureView(CanViewMixin, DetailView, FormMixin):
user
=
User
.
objects
.
filter
(
id
=
int
(
request
.
GET
[
'remove_user'
])).
first
()
if
user
.
id
==
request
.
user
.
id
or
request
.
user
.
is_in_group
(
settings
.
SITH_GROUP_SAS_ADMIN_ID
):
r
=
PeoplePictureRelation
.
objects
.
filter
(
user
=
user
,
picture
=
self
.
object
).
delete
()
except
:
pass
except
:
pass
if
'ask_removal'
in
request
.
GET
.
keys
():
self
.
object
.
is_moderated
=
False
self
.
object
.
asked_for_removal
=
True
...
...
@@ -149,7 +148,7 @@ class PictureView(CanViewMixin, DetailView, FormMixin):
for
uid
in
self
.
form
.
cleaned_data
[
'users'
]:
u
=
User
.
objects
.
filter
(
id
=
uid
).
first
()
PeoplePictureRelation
(
user
=
u
,
picture
=
self
.
form
.
cleaned_data
[
'picture'
]).
save
()
picture
=
self
.
form
.
cleaned_data
[
'picture'
]).
save
()
if
not
u
.
notifications
.
filter
(
type
=
"NEW_PICTURES"
,
viewed
=
False
).
exists
():
Notification
(
user
=
u
,
url
=
reverse
(
"core:user_pictures"
,
kwargs
=
{
'user_id'
:
u
.
id
}),
type
=
"NEW_PICTURES"
).
save
()
return
super
(
PictureView
,
self
).
form_valid
(
self
.
form
)
...
...
@@ -165,15 +164,19 @@ class PictureView(CanViewMixin, DetailView, FormMixin):
def
get_success_url
(
self
):
return
reverse
(
'sas:picture'
,
kwargs
=
{
'picture_id'
:
self
.
object
.
id
})
def
send_pict
(
request
,
picture_id
):
return
send_file
(
request
,
picture_id
,
Picture
)
def
send_compressed
(
request
,
picture_id
):
return
send_file
(
request
,
picture_id
,
Picture
,
"compressed"
)
def
send_thumb
(
request
,
picture_id
):
return
send_file
(
request
,
picture_id
,
Picture
,
"thumbnail"
)
class
AlbumUploadView
(
CanViewMixin
,
DetailView
,
FormMixin
):
model
=
Album
form_class
=
SASForm
...
...
@@ -189,7 +192,7 @@ class AlbumUploadView(CanViewMixin, DetailView, FormMixin):
if
request
.
user
.
is_authenticated
()
and
request
.
user
.
is_subscribed
:
if
self
.
form
.
is_valid
():
self
.
form
.
process
(
parent
=
parent
,
owner
=
request
.
user
,
files
=
files
,
automodere
=
request
.
user
.
is_in_group
(
settings
.
SITH_GROUP_SAS_ADMIN_ID
))
automodere
=
request
.
user
.
is_in_group
(
settings
.
SITH_GROUP_SAS_ADMIN_ID
))
if
self
.
form
.
is_valid
():
return
HttpResponse
(
str
(
self
.
form
.
errors
),
status
=
200
)
return
HttpResponse
(
str
(
self
.
form
.
errors
),
status
=
500
)
...
...
@@ -214,14 +217,14 @@ class AlbumView(CanViewMixin, DetailView, FormMixin):
self
.
form
=
self
.
get_form
()
if
'clipboard'
not
in
request
.
session
.
keys
():
request
.
session
[
'clipboard'
]
=
[]
if
request
.
user
.
can_edit
(
self
.
object
):
# Handle the copy-paste functions
if
request
.
user
.
can_edit
(
self
.
object
):
# Handle the copy-paste functions
FileView
.
handle_clipboard
(
request
,
self
.
object
)
parent
=
SithFile
.
objects
.
filter
(
id
=
self
.
object
.
id
).
first
()
files
=
request
.
FILES
.
getlist
(
'images'
)
if
request
.
user
.
is_authenticated
()
and
request
.
user
.
is_subscribed
:
if
self
.
form
.
is_valid
():
self
.
form
.
process
(
parent
=
parent
,
owner
=
request
.
user
,
files
=
files
,
automodere
=
request
.
user
.
is_in_group
(
settings
.
SITH_GROUP_SAS_ADMIN_ID
))
automodere
=
request
.
user
.
is_in_group
(
settings
.
SITH_GROUP_SAS_ADMIN_ID
))
if
self
.
form
.
is_valid
():
return
super
(
AlbumView
,
self
).
form_valid
(
self
.
form
)
else
:
...
...
@@ -239,6 +242,7 @@ class AlbumView(CanViewMixin, DetailView, FormMixin):
# Admin views
class
ModerationView
(
TemplateView
):
template_name
=
"sas/moderation.jinja"
...
...
@@ -257,23 +261,26 @@ class ModerationView(TemplateView):
a
.
save
()
elif
'delete'
in
request
.
POST
.
keys
():
a
.
delete
()
except
:
pass
except
:
pass
return
super
(
ModerationView
,
self
).
get
(
request
,
*
args
,
**
kwargs
)
def
get_context_data
(
self
,
**
kwargs
):
kwargs
=
super
(
ModerationView
,
self
).
get_context_data
(
**
kwargs
)
kwargs
[
'albums_to_moderate'
]
=
Album
.
objects
.
filter
(
is_moderated
=
False
,
is_in_sas
=
True
,
is_folder
=
True
).
order_by
(
'id'
)
is_folder
=
True
).
order_by
(
'id'
)
kwargs
[
'pictures'
]
=
Picture
.
objects
.
filter
(
is_moderated
=
False
,
is_in_sas
=
True
,
is_folder
=
False
)
kwargs
[
'albums'
]
=
Album
.
objects
.
filter
(
id__in
=
kwargs
[
'pictures'
].
values
(
'parent'
).
distinct
(
'parent'
))
return
kwargs
class
PictureEditForm
(
forms
.
ModelForm
):
class
Meta
:
model
=
Picture
fields
=
[
'name'
,
'parent'
]
fields
=
[
'name'
,
'parent'
]
parent
=
make_ajax_field
(
Picture
,
'parent'
,
'files'
,
help_text
=
""
)
class
AlbumEditForm
(
forms
.
ModelForm
):
class
Meta
:
model
=
Album
...
...
@@ -283,16 +290,18 @@ class AlbumEditForm(forms.ModelForm):
edit_groups
=
make_ajax_field
(
Album
,
'edit_groups'
,
'groups'
,
help_text
=
""
)
recursive
=
forms
.
BooleanField
(
label
=
_
(
"Apply rights recursively"
),
required
=
False
)
class
PictureEditView
(
CanEditMixin
,
UpdateView
):
model
=
Picture
form_class
=
PictureEditForm
template_name
=
'core/edit.jinja'
model
=
Picture
form_class
=
PictureEditForm
template_name
=
'core/edit.jinja'
pk_url_kwarg
=
"picture_id"
class
AlbumEditView
(
CanEditMixin
,
UpdateView
):
model
=
Album
form_class
=
AlbumEditForm
template_name
=
'core/edit.jinja'
model
=
Album
form_class
=
AlbumEditForm
template_name
=
'core/edit.jinja'
pk_url_kwarg
=
"album_id"
def
form_valid
(
self
,
form
):
...
...
@@ -300,4 +309,3 @@ class AlbumEditView(CanEditMixin, UpdateView):
if
form
.
cleaned_data
[
'recursive'
]:
self
.
object
.
apply_rights_recursively
(
True
)
return
ret
Write
Preview
Supports
Markdown
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