Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
MOOC packages by hacklab
django-courses
Commits
08b818ab
Commit
08b818ab
authored
Sep 16, 2020
by
Laury Bueno
Browse files
[Certification] Add a management script to create missing CourseCertifications
parent
6df26efa
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
62 additions
and
0 deletions
+62
-0
courses/certification/management/__init__.py
courses/certification/management/__init__.py
+0
-0
courses/certification/management/commands/__init__.py
courses/certification/management/commands/__init__.py
+0
-0
courses/certification/management/commands/generate_coursecertifications.py
...tion/management/commands/generate_coursecertifications.py
+62
-0
No files found.
courses/certification/management/__init__.py
0 → 100644
View file @
08b818ab
courses/certification/management/commands/__init__.py
0 → 100644
View file @
08b818ab
courses/certification/management/commands/generate_coursecertifications.py
0 → 100644
View file @
08b818ab
from
django.core.management.base
import
BaseCommand
,
CommandError
from
django.contrib.auth
import
get_user_model
from
django.db
import
transaction
from
base64
import
urlsafe_b64encode
as
ub64
from
hashlib
import
sha1
from
time
import
time
from
courses.models
import
CourseStudent
from
courses.certification.models
import
CourseCertification
,
CertificateTemplate
User
=
get_user_model
()
class
Command
(
BaseCommand
):
help
=
'Create CourseCertitification objects for every CourseStudent that doesn
\'
t yet have one'
def
add_arguments
(
self
,
parser
):
parser
.
add_argument
(
'--certificate'
,
action
=
'store_true'
,
help
=
'Certificate any users that have already earned a valid receipt'
)
parser
.
add_argument
(
'--dry-run'
,
action
=
'store_true'
,
help
=
'Don
\'
t save any changes, just print results'
)
@
transaction
.
atomic
def
handle
(
self
,
*
args
,
**
options
):
# Create any missing CourseCertification object
created_cc
=
0
for
cs
in
CourseStudent
.
objects
.
all
():
if
CourseCertification
.
objects
.
filter
(
course_student
=
cs
).
exists
():
continue
link_hash
=
ub64
(
sha1
((
str
(
time
())
+
cs
.
user
.
last_name
).
encode
(
'utf-8'
)).
digest
()[
0
:
6
])
receipt
=
CourseCertification
(
course_student
=
cs
,
type
=
CourseCertification
.
TYPES
[
0
][
0
],
is_valid
=
True
,
link_hash
=
link_hash
.
decode
())
if
not
options
[
'dry_run'
]:
receipt
.
save
()
created_cc
+=
1
# If '--certificate' was passed, transform any earned receipt in a certificate
certificates_earned
=
0
if
options
[
'certificate'
]:
for
cc
in
CourseCertification
.
objects
.
all
():
if
cc
.
course_student
.
can_emmit_receipt
()
and
cc
.
type
==
CourseCertification
.
TYPES
[
0
][
0
]:
cc
.
type
=
CourseCertification
.
TYPES
[
1
][
0
]
if
not
options
[
'dry_run'
]:
cc
.
save
()
certificates_earned
+=
1
print
(
f
'CourseCertification objects created:
{
created_cc
}
'
)
if
options
[
'certificate'
]:
print
(
f
'Certificates earned:
{
certificates_earned
}
'
)
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