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
83555a36
Commit
83555a36
authored
Jan 11, 2017
by
Skia
Committed by
Skia
Jan 15, 2017
Browse files
Add preferences and improve weekmail
parent
d988c093
Changes
11
Hide whitespace changes
Inline
Side-by-side
com/models.py
View file @
83555a36
...
...
@@ -3,9 +3,9 @@ from django.db import models, transaction
from
django.utils.translation
import
ugettext_lazy
as
_
from
django.core.urlresolvers
import
reverse_lazy
,
reverse
from
django.conf
import
settings
from
django.core.mail
import
EmailM
essage
from
django.core.mail
import
EmailM
ultiAlternatives
from
core.models
import
User
from
core.models
import
User
,
Preferences
from
club.models
import
Club
class
Sith
(
models
.
Model
):
...
...
@@ -83,26 +83,34 @@ class Weekmail(models.Model):
ordering
=
[
'-id'
]
def
send
(
self
):
dest
=
[
i
[
0
]
for
i
in
Preferences
.
objects
.
filter
(
receive_weekmail
=
True
).
values_list
(
'user__email'
)]
with
transaction
.
atomic
():
print
(
"Sending weekmail n°"
+
str
(
self
.
id
))
email
=
EmailMessage
(
email
=
EmailMultiAlternatives
(
subject
=
self
.
title
,
body
=
self
.
render
(),
body
=
self
.
render
_text
(),
from_email
=
settings
.
DEFAULT_FROM_EMAIL
,
to
=
[
'skia@git.an'
],
bcc
=
Sith
.
objects
.
first
().
weekmail_destinations
.
split
(
' '
),
# TODO: Content-Type: text/html
to
=
Sith
.
objects
.
first
().
weekmail_destinations
.
split
(
' '
),
bcc
=
dest
,
)
email
.
attach_alternative
(
self
.
render_html
(),
"text/html"
)
email
.
send
()
self
.
sent
=
True
self
.
save
()
Weekmail
().
save
()
def
render
(
self
):
return
render
(
None
,
"com/weekmail_renderer.jinja"
,
context
=
{
def
render
_text
(
self
):
return
render
(
None
,
"com/weekmail_renderer
_text
.jinja"
,
context
=
{
'weekmail'
:
self
,
}).
content
.
decode
(
'utf-8'
)
def
render_html
(
self
):
return
render
(
None
,
"com/weekmail_renderer_html.jinja"
,
context
=
{
'weekmail'
:
self
,
}).
content
.
decode
(
'utf-8'
)
def
__str__
(
self
):
return
"Weekmail %s (sent: %s) - %s"
%
(
self
.
id
,
self
.
sent
,
self
.
title
)
class
WeekmailArticle
(
models
.
Model
):
weekmail
=
models
.
ForeignKey
(
Weekmail
,
related_name
=
"articles"
,
verbose_name
=
_
(
"weekmail"
),
null
=
True
)
title
=
models
.
CharField
(
_
(
"title"
),
max_length
=
64
)
...
...
com/templates/com/weekmail_renderer.jinja
→
com/templates/com/weekmail_renderer
_html
.jinja
View file @
83555a36
...
...
@@ -9,26 +9,26 @@
<ul>
{%
for
a
in
weekmail.articles.all
()
%}
<li>
[
{{
a.club
}}
]
{{
a.title
}}
</li>
{%
endfor
%}
{%
-
endfor
%}
</ul>
{%
for
a
in
weekmail.articles.all
()
%}
{%
-
for
a
in
weekmail.articles.all
()
%}
<h3>
[
{{
a.club
}}
]
{{
a.title
}}
</h3>
{{
a.content
|
markdown
}}
{%
endfor
%}
{%
-
endfor
-
%}
{%
if
weekmail.joke
%}
{%
-
if
weekmail.joke
%}
<h3>
{%
trans
%}
Joke
{%
endtrans
%}
</h3>
{{
weekmail.joke
|
markdown
}}
{%
endif
%}
{%
endif
-
%}
{%
if
weekmail.protip
%}
{%
-
if
weekmail.protip
%}
<h3>
{%
trans
%}
Pro tip
{%
endtrans
%}
</h3>
{{
weekmail.protip
|
markdown
}}
{%
endif
%}
{%
endif
-
%}
{%
if
weekmail.conclusion
%}
{%
-
if
weekmail.conclusion
%}
<h3>
{%
trans
%}
Final word
{%
endtrans
%}
</h3>
{{
weekmail.conclusion
|
markdown
}}
{%
endif
%}
{%
endif
-
%}
com/templates/com/weekmail_renderer_text.jinja
0 → 100644
View file @
83555a36
#
{{
weekmail.title
}}
{%
-
if
weekmail.intro
%}
## {% trans %}Intro{% endtrans %}
{{
weekmail.intro
}}
{%
endif
%}
## {% trans %}Table of content{% endtrans %}
{%
for
a
in
weekmail.articles.all
()
%}
* [
{{
a.club
}}
]
{{
a.title
}}
{%
endfor
-
%}
{%
for
a
in
weekmail.articles.all
()
%}
## [{{ a.club }}] {{ a.title }}
{{
a.content
}}
{%
endfor
-
%}
{%
-
if
weekmail.joke
%}
## {% trans %}Joke{% endtrans %}
{{
weekmail.joke
}}
{%
endif
-
%}
{%
-
if
weekmail.protip
%}
## {% trans %}Pro tip{% endtrans %}
{{
weekmail.protip
}}
{%
endif
-
%}
{%
-
if
weekmail.conclusion
%}
## {% trans %}Final word{% endtrans %}
{{
weekmail.conclusion
}}
{%
endif
-
%}
com/urls.py
View file @
83555a36
...
...
@@ -6,6 +6,7 @@ urlpatterns = [
url
(
r
'^sith/edit/alert$'
,
AlertMsgEditView
.
as_view
(),
name
=
'alert_edit'
),
url
(
r
'^sith/edit/info$'
,
InfoMsgEditView
.
as_view
(),
name
=
'info_edit'
),
url
(
r
'^sith/edit/index$'
,
IndexEditView
.
as_view
(),
name
=
'index_edit'
),
url
(
r
'^sith/edit/weekmail_destinations$'
,
WeekmailDestinationEditView
.
as_view
(),
name
=
'weekmail_destinations'
),
url
(
r
'^weekmail$'
,
WeekmailEditView
.
as_view
(),
name
=
'weekmail'
),
url
(
r
'^weekmail/preview$'
,
WeekmailPreviewView
.
as_view
(),
name
=
'weekmail_preview'
),
url
(
r
'^weekmail/club/(?P<club_id>[0-9]+)/new_article$'
,
WeekmailArticleCreateView
.
as_view
(),
name
=
'weekmail_article'
),
...
...
com/views.py
View file @
83555a36
...
...
@@ -34,6 +34,11 @@ class ComTabsMixin(TabedViewMixin):
'slug'
:
'weekmail'
,
'name'
:
_
(
"Weekmail"
),
})
tab_list
.
append
({
'url'
:
reverse
(
'com:weekmail_destinations'
),
'slug'
:
'weekmail_destinations'
,
'name'
:
_
(
"Weekmail destinations"
),
})
tab_list
.
append
({
'url'
:
reverse
(
'com:index_edit'
),
'slug'
:
'index'
,
...
...
@@ -73,6 +78,11 @@ class IndexEditView(ComEditView):
current_tab
=
"index"
success_url
=
reverse_lazy
(
'com:index_edit'
)
class
WeekmailDestinationEditView
(
ComEditView
):
fields
=
[
'weekmail_destinations'
]
current_tab
=
"weekmail_destinations"
success_url
=
reverse_lazy
(
'com:weekmail_destinations'
)
# News
class
NewsForm
(
forms
.
ModelForm
):
...
...
@@ -232,14 +242,15 @@ class WeekmailPreviewView(DetailView):
def
get_context_data
(
self
,
**
kwargs
):
"""Add rendered weekmail"""
kwargs
=
super
(
WeekmailPreviewView
,
self
).
get_context_data
(
**
kwargs
)
kwargs
[
'weekmail_rendered'
]
=
self
.
object
.
render
()
kwargs
[
'weekmail_rendered'
]
=
self
.
object
.
render
_html
()
return
kwargs
class
WeekmailEditView
(
QuickNotifMixin
,
UpdateView
):
class
WeekmailEditView
(
ComTabsMixin
,
QuickNotifMixin
,
UpdateView
):
model
=
Weekmail
template_name
=
'com/weekmail.jinja'
fields
=
[
'title'
,
'intro'
,
'joke'
,
'protip'
,
'conclusion'
]
success_url
=
reverse_lazy
(
'com:weekmail'
)
current_tab
=
"weekmail"
def
get_object
(
self
,
queryset
=
None
):
weekmail
=
self
.
model
.
objects
.
filter
(
sent
=
False
).
order_by
(
'-id'
).
first
()
...
...
@@ -291,7 +302,7 @@ class WeekmailEditView(QuickNotifMixin, UpdateView):
kwargs
[
'orphans'
]
=
WeekmailArticle
.
objects
.
filter
(
weekmail
=
None
)
return
kwargs
class
WeekmailArticleEditView
(
QuickNotifMixin
,
UpdateView
):
class
WeekmailArticleEditView
(
ComTabsMixin
,
QuickNotifMixin
,
UpdateView
):
"""Edit an article"""
model
=
WeekmailArticle
fields
=
[
'title'
,
'content'
]
...
...
@@ -299,6 +310,7 @@ class WeekmailArticleEditView(QuickNotifMixin, UpdateView):
template_name
=
'core/edit.jinja'
success_url
=
reverse_lazy
(
'com:weekmail'
)
quick_notif_url_arg
=
"qn_weekmail_article_edit"
current_tab
=
"weekmail"
class
WeekmailArticleCreateView
(
QuickNotifMixin
,
CreateView
):
"""Post an article"""
...
...
core/management/commands/populate.py
View file @
83555a36
...
...
@@ -86,7 +86,7 @@ class Command(BaseCommand):
home_root
.
save
()
club_root
.
save
()
Sith
().
save
()
Sith
(
weekmail_destinations
=
"etudiants@git.an personnel@git.an"
).
save
()
Weekmail
().
save
()
p
=
Page
(
name
=
'Index'
)
...
...
core/migrations/0019_preferences_receive_weekmail.py
0 → 100644
View file @
83555a36
# -*- coding: utf-8 -*-
from
__future__
import
unicode_literals
from
django.db
import
migrations
,
models
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'core'
,
'0018_auto_20161224_0211'
),
]
operations
=
[
migrations
.
AddField
(
model_name
=
'preferences'
,
name
=
'receive_weekmail'
,
field
=
models
.
BooleanField
(
verbose_name
=
'define if we want to receive the weekmail'
,
default
=
False
,
help_text
=
'Do you want to receive the weekmail'
),
),
]
core/models.py
View file @
83555a36
...
...
@@ -487,12 +487,23 @@ class AnonymousUser(AuthAnonymousUser):
class
Preferences
(
models
.
Model
):
user
=
models
.
OneToOneField
(
User
,
related_name
=
"preferences"
)
receive_weekmail
=
models
.
BooleanField
(
_
(
'do you want to receive the weekmail'
),
default
=
False
,
# help_text=_('Do you want to receive the weekmail?'),
)
show_my_stats
=
models
.
BooleanField
(
_
(
'define if we show a users stats'
),
default
=
False
,
help_text
=
_
(
'Show your account statistics to others'
),
)
def
get_display_name
(
self
):
return
self
.
user
.
get_display_name
()
def
get_absolute_url
(
self
):
return
self
.
user
.
get_absolute_url
()
def
get_directory
(
instance
,
filename
):
return
'.{0}/{1}'
.
format
(
instance
.
get_parent_path
(),
filename
)
...
...
core/templates/core/user_tools.jinja
View file @
83555a36
...
...
@@ -67,6 +67,7 @@
<ul>
{%
if
user.is_in_group
(
settings.SITH_GROUP_COM_ADMIN_ID
)
or
user.is_root
%}
<li><a
href=
"
{{
url
(
'com:weekmail'
)
}}
"
>
{%
trans
%}
Weekmail
{%
endtrans
%}
</a></li>
<li><a
href=
"
{{
url
(
'com:weekmail_destinations'
)
}}
"
>
{%
trans
%}
Weekmail destinations
{%
endtrans
%}
</a></li>
<li><a
href=
"
{{
url
(
'com:news_admin_list'
)
}}
"
>
{%
trans
%}
Moderate news
{%
endtrans
%}
</a></li>
<li><a
href=
"
{{
url
(
'com:index_edit'
)
}}
"
>
{%
trans
%}
Edit index page
{%
endtrans
%}
</a></li>
<li><a
href=
"
{{
url
(
'com:alert_edit'
)
}}
"
>
{%
trans
%}
Edit alert message
{%
endtrans
%}
</a></li>
...
...
core/urls.py
View file @
83555a36
...
...
@@ -40,6 +40,7 @@ urlpatterns = [
url
(
r
'^user/(?P<user_id>[0-9]+)/edit$'
,
UserUpdateProfileView
.
as_view
(),
name
=
'user_edit'
),
url
(
r
'^user/(?P<user_id>[0-9]+)/profile_upload$'
,
UserUploadProfilePictView
.
as_view
(),
name
=
'user_profile_upload'
),
url
(
r
'^user/(?P<user_id>[0-9]+)/clubs$'
,
UserClubView
.
as_view
(),
name
=
'user_clubs'
),
url
(
r
'^user/(?P<user_id>[0-9]+)/prefs$'
,
UserPreferencesView
.
as_view
(),
name
=
'user_prefs'
),
url
(
r
'^user/(?P<user_id>[0-9]+)/groups$'
,
UserUpdateGroupView
.
as_view
(),
name
=
'user_groups'
),
url
(
r
'^user/tools/$'
,
UserToolsView
.
as_view
(),
name
=
'user_tools'
),
url
(
r
'^user/(?P<user_id>[0-9]+)/account$'
,
UserAccountView
.
as_view
(),
name
=
'user_account'
),
...
...
core/views/user.py
View file @
83555a36
...
...
@@ -19,7 +19,7 @@ import logging
from
core.views
import
CanViewMixin
,
CanEditMixin
,
CanEditPropMixin
,
TabedViewMixin
from
core.views.forms
import
RegisteringForm
,
UserPropForm
,
UserProfileForm
,
LoginForm
,
UserGodfathersForm
from
core.models
import
User
,
SithFile
from
core.models
import
User
,
SithFile
,
Preferences
from
club.models
import
Club
from
subscription.models
import
Subscription
...
...
@@ -151,6 +151,11 @@ class UserTabsMixin(TabedViewMixin):
'slug'
:
'edit'
,
'name'
:
_
(
"Edit"
),
})
tab_list
.
append
({
'url'
:
reverse
(
'core:user_prefs'
,
kwargs
=
{
'user_id'
:
self
.
object
.
id
}),
'slug'
:
'prefs'
,
'name'
:
_
(
"Preferences"
),
})
if
self
.
request
.
user
.
can_view
(
self
.
object
):
tab_list
.
append
({
'url'
:
reverse
(
'core:user_clubs'
,
kwargs
=
{
'user_id'
:
self
.
object
.
id
}),
...
...
@@ -378,6 +383,26 @@ class UserClubView(UserTabsMixin, CanViewMixin, DetailView):
template_name
=
"core/user_clubs.jinja"
current_tab
=
"clubs"
class
UserPreferencesView
(
UserTabsMixin
,
CanEditMixin
,
UpdateView
):
"""
Edit a user's preferences
"""
model
=
Preferences
pk_url_kwarg
=
"user_id"
template_name
=
"core/edit.jinja"
fields
=
[
'receive_weekmail'
]
context_object_name
=
"profile"
current_tab
=
"prefs"
def
get_object
(
self
,
queryset
=
None
):
user
=
get_object_or_404
(
User
,
pk
=
self
.
kwargs
[
'user_id'
])
try
:
return
user
.
preferences
except
:
pref
=
Preferences
(
user
=
user
)
pref
.
save
()
return
pref
class
UserUpdateGroupView
(
UserTabsMixin
,
CanEditPropMixin
,
UpdateView
):
"""
Edit a user's groups
...
...
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