Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
beat
beat.web
Commits
97f67db3
Commit
97f67db3
authored
Sep 09, 2020
by
Samuel GAIST
Committed by
Flavio TARSETTI
Sep 14, 2020
Browse files
[reports][management] Pre-commit cleanup
parent
ea81206d
Changes
1
Hide whitespace changes
Inline
Side-by-side
beat/web/reports/management/commands/send_report_cleanup_warning_and_cleanup.py
View file @
97f67db3
...
...
@@ -27,49 +27,66 @@
###############################################################################
from
django.core.management
import
call_command
from
django.core.management.base
import
BaseCommand
,
CommandError
from
django.core.mail
import
send_mail
from
django.template.loader
import
render_to_string
from
datetime
import
date
from
datetime
import
datetime
from
datetime
import
time
from
datetime
import
timedelta
from
django.conf
import
settings
from
django.contrib.sites.models
import
Site
from
django.core.mail
import
send_mail
from
django.core.management
import
call_command
from
django.core.management.base
import
BaseCommand
from
django.template.loader
import
render_to_string
from
datetime
import
datetime
,
time
,
date
,
timedelta
from
...models
import
Report
from
....
import
__version__
from
...models
import
Report
import
sys
class
Command
(
BaseCommand
):
help
=
'
Send email warning for reports about to expire and cleanup old reports
'
help
=
"
Send email warning for reports about to expire and cleanup old reports
"
def
handle
(
self
,
*
args
,
**
options
):
for
expiration_reminder
in
settings
.
EXPIRATION_REMINDERS
:
expiration_date
=
date
.
today
()
+
timedelta
(
days
=
expiration_reminder
)
reports_about_to_expire
=
Report
.
objects
.
filter
(
status
=
Report
.
LOCKED
,
expiration_date__range
=
(
datetime
.
combine
(
expiration_date
,
time
.
min
),
datetime
.
combine
(
expiration_date
,
time
.
max
)))
reports_about_to_expire
=
Report
.
objects
.
filter
(
status
=
Report
.
LOCKED
,
expiration_date__range
=
(
datetime
.
combine
(
expiration_date
,
time
.
min
),
datetime
.
combine
(
expiration_date
,
time
.
max
),
),
)
if
reports_about_to_expire
:
current_site
=
Site
.
objects
.
get_current
()
template_path
=
'
reports/report_about_to_expire_email.txt
'
template_path
=
"
reports/report_about_to_expire_email.txt
"
for
report
in
reports_about_to_expire
:
subject
=
"Report %s is about to expire"
%
\
report
.
name
subject
=
"Report %s is about to expire"
%
report
.
name
send_mail
(
subject
,
render_to_string
(
template_path
,
{
'report'
:
report
,
'beat_version'
:
__version__
,
'site'
:
current_site
,
}
),
settings
.
DEFAULT_FROM_EMAIL
,
[
report
.
author
.
email
])
self
.
stdout
.
write
(
'{} report(s) about to expire'
.
format
(
reports_about_to_expire
.
count
()))
send_mail
(
subject
,
render_to_string
(
template_path
,
{
"report"
:
report
,
"beat_version"
:
__version__
,
"site"
:
current_site
,
},
),
settings
.
DEFAULT_FROM_EMAIL
,
[
report
.
author
.
email
],
)
self
.
stdout
.
write
(
"{} report(s) about to expire"
.
format
(
reports_about_to_expire
.
count
()
)
)
else
:
self
.
stdout
.
write
(
'No report(s) about to expire in {} day(s)'
.
format
(
expiration_reminder
))
self
.
stdout
.
write
(
"No report(s) about to expire in {} day(s)"
.
format
(
expiration_reminder
)
)
call_command
(
'
clean_report
'
,
'
--noinput
'
)
call_command
(
"
clean_report
"
,
"
--noinput
"
)
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