Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
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
edx
edx-platform
Commits
516c41f3
Commit
516c41f3
authored
Aug 22, 2012
by
Carlos Andrés Rocha
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added html view of serial numbers. Refactored code.
parent
b2de8199
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
99 additions
and
54 deletions
+99
-54
lms/djangoapps/licenses/management/commands/import_serial_numbers.py
+6
-4
lms/djangoapps/licenses/models.py
+61
-3
lms/djangoapps/licenses/views.py
+22
-47
lms/templates/licenses/serial_numbers.html
+10
-0
No files found.
lms/djangoapps/licenses/management/commands/import_serial_numbers.py
View file @
516c41f3
...
...
@@ -9,15 +9,18 @@ from xmodule.modulestore.django import modulestore
from
licenses.models
import
CourseSoftware
,
UserLicense
class
Command
(
BaseCommand
):
help
=
"""Imports serial numbers for software used in a course.
Usage: import_serial_numbers <course_id> <software_name> <file
name
>
Usage: import_serial_numbers <course_id> <software_name> <file>
serial_file
is a text file that list one available serial number per line.
<file>
is a text file that list one available serial number per line.
Example:
django-admin.py import_serial_numbers MITx/6.002x/2012_Fall matlab /tmp/matlab-serials.txt
import_serial_numbers MITx/6.002x/2012_Fall matlab serials.txt
"""
args
=
"course_id software_id serial_file"
...
...
@@ -49,7 +52,6 @@ class Command(BaseCommand):
return
course_id
,
software_name
,
filename
def
_import_serials
(
self
,
software
,
filename
):
print
"Importing serial numbers for {0}."
.
format
(
software
)
...
...
lms/djangoapps/licenses/models.py
View file @
516c41f3
"""
"""
from
django.db
import
models
import
logging
from
django.db
import
models
,
transaction
from
student.models
import
User
log
=
logging
.
getLogger
(
"mitx.licenses"
)
class
CourseSoftware
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
255
)
...
...
@@ -18,3 +21,58 @@ class UserLicense(models.Model):
software
=
models
.
ForeignKey
(
CourseSoftware
,
db_index
=
True
)
user
=
models
.
ForeignKey
(
User
,
null
=
True
)
serial
=
models
.
CharField
(
max_length
=
255
)
def
get_courses_licenses
(
user
,
courses
):
course_ids
=
set
(
course
.
id
for
course
in
courses
)
all_software
=
CourseSoftware
.
objects
.
filter
(
course_id__in
=
course_ids
)
assigned_licenses
=
UserLicense
.
objects
.
filter
(
software__in
=
all_software
,
user
=
user
)
licenses
=
dict
.
fromkeys
(
all_software
,
None
)
for
license
in
assigned_licenses
:
licenses
[
license
.
software
]
=
license
log
.
info
(
assigned_licenses
)
log
.
info
(
licenses
)
return
licenses
def
get_license
(
user
,
software
):
try
:
license
=
UserLicense
.
objects
.
get
(
user
=
user
,
software
=
software
)
except
UserLicense
.
DoesNotExist
:
license
=
None
return
license
def
get_or_create_license
(
user
,
software
):
license
=
get_license
(
user
,
software
)
if
license
is
None
:
license
=
_create_license
(
user
,
software
)
return
license
def
_create_license
(
user
,
software
):
license
=
None
try
:
# find one license that has not been assigned, locking the
# table/rows with select_for_update to prevent race conditions
with
transaction
.
commit_on_success
():
selected
=
UserLicense
.
objects
.
select_for_update
()
license
=
selected
.
filter
(
user__isnull
=
True
)[
0
]
license
.
user
=
user
license
.
save
()
except
IndexError
:
# there are no free licenses
log
.
error
(
'No serial numbers available for {0}'
,
software
)
license
=
None
# TODO [rocha]look if someone has unenrolled from the class
# and already has a serial number
return
license
lms/djangoapps/licenses/views.py
View file @
516c41f3
import
logging
from
itertools
import
groupby
from
collections
import
Iterable
from
collections
import
namedtuple
,
defaultdict
from
django.db.models
import
Q
from
mitxmako.shortcuts
import
render_to_string
from
models
import
CourseSoftware
,
UserL
icense
from
models
import
get_courses_licenses
,
get_or_create_l
icense
log
=
logging
.
getLogger
(
"mitx.licenses"
)
def
get_or_create_courses_licenses
(
user
,
courses
):
user_licenses
=
get_courses_licenses
(
user
,
courses
)
for
software
,
license
in
user_licenses
.
iteritems
():
if
license
is
None
:
user_licenses
[
software
]
=
get_or_create_user_license
(
user
,
software
)
log
.
info
(
user_licenses
)
return
user_licenses
def
get_courses_licenses
(
user
,
courses
):
course_ids
=
set
(
course
.
id
for
course
in
courses
)
all_software
=
CourseSoftware
.
objects
.
filter
(
course_id__in
=
course_ids
)
user_licenses
=
dict
.
fromkeys
(
all_software
,
None
)
assigned_licenses
=
UserLicense
.
objects
.
filter
(
software__in
=
all_software
,
user
=
user
)
assigned_by_software
=
{
lic
.
software
:
lic
for
lic
in
assigned_licenses
}
for
software
,
license
in
assigned_by_software
.
iteritems
():
user_licenses
[
software
]
=
license
return
user_licenses
log
=
logging
.
getLogger
(
"mitx.licenses"
)
def
get_or_create_user_license
(
user
,
software
):
license
=
None
try
:
# Find a licenses associated with the user or with no user
# associated.
query
=
(
Q
(
user__isnull
=
True
)
|
Q
(
user
=
user
))
&
Q
(
software
=
software
)
License
=
namedtuple
(
'License'
,
'software serial'
)
# TODO fix a race condition in this code when more than one
# user is getting a license assigned
license
=
UserLicense
.
objects
.
filter
(
query
)[
0
]
def
get_licenses_by_course
(
user
,
courses
):
licenses
=
get_courses_licenses
(
user
,
courses
)
licenses_by_course
=
defaultdict
(
list
)
if
license
.
user
is
not
user
:
license
.
user
=
user
license
.
save
()
# create missing licenses and group by course_id
for
software
,
license
in
licenses
.
iteritems
():
if
license
is
None
:
licenses
[
software
]
=
get_or_create_license
(
user
,
software
)
except
IndexError
:
# TODO look if someone has unenrolled from the class and already has a serial number
l
og
.
error
(
'No serial numbers available for {0}'
,
software
)
course_id
=
software
.
course_id
serial
=
license
.
serial
if
license
else
None
l
icenses_by_course
[
course_id
]
.
append
(
License
(
software
,
serial
)
)
# render elements
data_by_course
=
{}
for
course_id
,
licenses
in
licenses_by_course
.
iteritems
():
context
=
{
'licenses'
:
licenses
}
template
=
'licenses/serial_numbers.html'
data_by_course
[
course_id
]
=
render_to_string
(
template
,
context
)
return
licen
se
return
data_by_cour
se
lms/templates/licenses/serial_numbers.html
0 → 100644
View file @
516c41f3
<dl>
% for license in licenses:
<dt>
${license.software.name}:
</dt>
% if license.serial:
<dd>
${license.serial}
</dd>
% else:
<dd>
None Available
</dd>
% endif
% endfor
</dl>
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