Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-wiki
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
django-wiki
Commits
088c0fe5
Commit
088c0fe5
authored
Feb 21, 2013
by
benjaoming
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #115 from jdcaballero/master
django-notify email notifications
parents
e448e2b7
fbd75ae0
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
113 additions
and
2 deletions
+113
-2
django_notify/management/__init__.py
+0
-0
django_notify/management/commands/__init__.py
+0
-0
django_notify/management/commands/notifysendemails.py
+90
-0
django_notify/settings.py
+7
-2
django_notify/templates/emails/notification_email_message.txt
+14
-0
django_notify/templates/emails/notification_email_subject.txt
+2
-0
No files found.
django_notify/management/__init__.py
0 → 100644
View file @
088c0fe5
django_notify/management/commands/__init__.py
0 → 100644
View file @
088c0fe5
django_notify/management/commands/notifysendemails.py
0 → 100644
View file @
088c0fe5
import
os
import
sys
import
time
from
datetime
import
datetime
from
optparse
import
make_option
from
django.contrib.sites.models
import
Site
from
django.core
import
mail
from
django.core.management.base
import
BaseCommand
,
CommandError
from
django.template.loader
import
render_to_string
from
django.utils.translation
import
ugettext
as
_
from
django_notify
import
models
from
django_notify.settings
import
INTERVALS
,
DEFAULT_EMAIL
,
NOTIFY_SLEEP_TIME
,
EMAIL_SUBJECT
class
Command
(
BaseCommand
):
help
=
'Sends Notification emails to subscribed users taking into account the subscription interval'
option_list
=
BaseCommand
.
option_list
+
(
make_option
(
'--daemon'
,
'-d'
,
action
=
'store_true'
,
dest
=
'daemon'
,
default
=
True
,
help
=
'Go to daemon mode and exit'
),
)
def
_send_user_notifications
(
self
,
context
,
connection
):
subject
=
_
(
EMAIL_SUBJECT
)
message
=
render_to_string
(
'emails/notification_email_message.txt'
,
context
)
email
=
mail
.
EmailMessage
(
subject
,
message
,
DEFAULT_EMAIL
,
[
context
[
'user'
]
.
email
],
connection
=
connection
)
email
.
send
()
def
handle
(
self
,
*
args
,
**
options
):
daemon
=
options
[
'daemon'
]
# Run as daemon, ie. fork the process
if
daemon
:
try
:
fpid
=
os
.
fork
()
if
fpid
>
0
:
# Running as daemon now. PID is fpid
pid_file
=
file
(
'/tmp/daemon-example.pid'
,
"w"
)
pid_file
.
write
(
str
(
fpid
))
pid_file
.
close
()
sys
.
exit
(
0
)
except
OSError
,
e
:
sys
.
stderr
.
write
(
"fork failed:
%
d (
%
s)
\n
"
%
(
e
.
errno
,
e
.
strerror
))
sys
.
exit
(
1
)
# This could be /improved by looking up the last notified person
last_sent
=
None
context
=
{
'user'
:
None
,
'notifications'
:
None
,
'digest'
:
None
,
'site'
:
Site
.
objects
.
get_current
()}
#create a connection to smtp server for reuse
connection
=
mail
.
get_connection
()
while
True
:
connection
.
open
()
started_sending_at
=
datetime
.
now
()
if
last_sent
:
settings
=
models
.
Settings
.
objects
.
filter
(
interval__lte
=
((
started_sending_at
-
last_sent
)
.
seconds
//
60
)
//
60
)
.
order_by
(
'user'
)
else
:
settings
=
models
.
Settings
.
objects
.
all
()
.
order_by
(
'user'
)
for
setting
in
settings
:
context
[
'user'
]
=
setting
.
user
context
[
'notifications'
]
=
[]
#get the index of the tuple corresponding to the interval and get the string name
context
[
'digest'
]
=
INTERVALS
[[
y
[
0
]
for
y
in
INTERVALS
]
.
index
(
setting
.
interval
)][
1
]
for
subscription
in
setting
.
subscription_set
.
filter
(
send_emails
=
True
,
latest__is_emailed
=
False
):
context
[
'notifications'
]
.
append
(
subscription
.
latest
)
subscription
.
latest
.
is_emailed
=
True
subscription
.
latest
.
save
()
if
len
(
context
[
'notifications'
])
>
0
:
self
.
_send_user_notifications
(
context
,
connection
)
connection
.
close
()
last_sent
=
datetime
.
now
()
elapsed_minutes
=
((
last_sent
-
started_sending_at
)
.
seconds
)
//
60
time
.
sleep
(
max
((
min
(
INTERVALS
)[
0
]
*
60
-
elapsed_minutes
)
*
60
,
NOTIFY_SLEEP_TIME
))
django_notify/settings.py
View file @
088c0fe5
...
@@ -18,6 +18,12 @@ ENABLE_ADMIN = getattr(django_settings, "NOTIFY_ENABLE_ADMIN", False)
...
@@ -18,6 +18,12 @@ ENABLE_ADMIN = getattr(django_settings, "NOTIFY_ENABLE_ADMIN", False)
# to a proper SMTP server, just leave it off...
# to a proper SMTP server, just leave it off...
SEND_EMAILS
=
getattr
(
django_settings
,
"NOTIFY_SEND_EMAILS"
,
False
)
SEND_EMAILS
=
getattr
(
django_settings
,
"NOTIFY_SEND_EMAILS"
,
False
)
EMAIL_SUBJECT
=
"Notifications"
DEFAULT_EMAIL
=
'notifications@example.com'
#NOTIFY_SLEEP_TIME must be greater than 0 to allow for Garbage Collection
NOTIFY_SLEEP_TIME
=
10
# You can always make up more numbers... they simply identify which notifications
# You can always make up more numbers... they simply identify which notifications
# to send when invoking the script, and the number indicates how many hours
# to send when invoking the script, and the number indicates how many hours
# to minimum pass between each notification.
# to minimum pass between each notification.
...
@@ -27,12 +33,11 @@ DAILY = 24-1 # Subtract 1, because the job finishes less than 24h before the nex
...
@@ -27,12 +33,11 @@ DAILY = 24-1 # Subtract 1, because the job finishes less than 24h before the nex
WEEKLY
=
7
*
24
-
1
WEEKLY
=
7
*
24
-
1
INTERVALS
=
getattr
(
django_settings
,
"NOTIFY_INTERVALS"
,
INTERVALS
=
getattr
(
django_settings
,
"NOTIFY_INTERVALS"
,
[(
INSTANTLY
,
_
(
u'instant
ly
'
)),
[(
INSTANTLY
,
_
(
u'instant'
)),
(
DAILY
,
_
(
u'daily'
)),
(
DAILY
,
_
(
u'daily'
)),
(
WEEKLY
,
_
(
u'weekly'
))])
(
WEEKLY
,
_
(
u'weekly'
))])
INTERVALS_DEFAULT
=
INSTANTLY
INTERVALS_DEFAULT
=
INSTANTLY
# Minimum logging and digital garbage! Don't save too much crap!
# Minimum logging and digital garbage! Don't save too much crap!
# After how many days should viewed notifications be deleted?
# After how many days should viewed notifications be deleted?
...
...
django_notify/templates/emails/notification_email_message.txt
0 → 100644
View file @
088c0fe5
{% load i18n %}{% autoescape off %}{% load url from future %}
{% blocktrans with user.username as username %}Dear {{ username }},{% endblocktrans %}
{% blocktrans with site.name as site %} These are the {{ digest }} notifications from {{ site }}.{% endblocktrans %}
{% for n in notifications %}
http://{{ site.domain }}{{n.url}} - {{ n.message }}
{% endfor %}
{% trans "Thanks for using our site!" %}
{% trans "Sincerely" %},
{{ site.name }}
{% endautoescape %}
django_notify/templates/emails/notification_email_subject.txt
0 → 100644
View file @
088c0fe5
{% load i18n %}
{% blocktrans with site.name as site %} {{ digest }} Notifications {{ site }}.{% endblocktrans %}
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