Skip to content

Commit

Permalink
pep8 & pylint
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Rossi authored and cewing committed Apr 6, 2015
1 parent 40b1e01 commit 396fa09
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 24 deletions.
1 change: 1 addition & 0 deletions lms/djangoapps/pocs/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ class PocMembershipFactory(DjangoModelFactory):
FACTORY_FOR = PocMembership
active = False


class PocFutureMembershipFactory(DjangoModelFactory):
FACTORY_FOR = PocFutureMembership
1 change: 0 additions & 1 deletion lms/djangoapps/pocs/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,6 @@ def test_enroll_non_user_no_email(self):
# ensure there are still no emails in the outbox now
self.assertEqual(len(self.outbox), 0)


def test_enroll_non_member_no_email(self):
"""register a non-member but send no email"""
self.create_user()
Expand Down
4 changes: 3 additions & 1 deletion lms/djangoapps/pocs/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ def test_student_progress(self):
self.assertEqual(response.status_code, 200)
grades = response.mako_context['grade_summary']
self.assertEqual(grades['percent'], 0.5)
self.assertEqual( grades['grade_breakdown'][0]['percent'], 0.5)
self.assertEqual(grades['grade_breakdown'][0]['percent'], 0.5)
self.assertEqual(len(grades['section_breakdown']), 4)


Expand All @@ -524,8 +524,10 @@ def visit(block):
yield descendant
return visit(course)


def visible_children(block):
block_get_children = block.get_children

def get_children():
def iter_children():
for child in block_get_children():
Expand Down
67 changes: 45 additions & 22 deletions lms/djangoapps/pocs/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
"""
Views related to the Personal Online Courses feature.
"""
import csv
import datetime
import functools
Expand Down Expand Up @@ -40,7 +43,7 @@
from .utils import enroll_email, unenroll_email

log = logging.getLogger(__name__)
today = datetime.datetime.today # for patching in tests
TODAY = datetime.datetime.today # for patching in tests


def coach_dashboard(view):
Expand All @@ -51,6 +54,10 @@ def coach_dashboard(view):
"""
@functools.wraps(view)
def wrapper(request, course_id):
"""
Wraps the view function, performing access check, loading the course,
and modifying the view's call signature.
"""
course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)
role = CoursePocCoachRole(course_key)
if not role.has_user(request.user):
Expand Down Expand Up @@ -79,7 +86,7 @@ def dashboard(request, course):
'gradebook_url': reverse('poc_gradebook',
kwargs={'course_id': course.id}),
'grades_csv_url': reverse('poc_grades_csv',
kwargs={'course_id': course.id}),
kwargs={'course_id': course.id}),
}
if not poc:
context['create_poc_url'] = reverse(
Expand All @@ -102,7 +109,7 @@ def create_poc(request, course):
poc.save()

# Make sure start/due are overridden for entire course
start = today().replace(tzinfo=pytz.UTC)
start = TODAY().replace(tzinfo=pytz.UTC)
override_field_for_poc(poc, course, 'start', start)
override_field_for_poc(poc, course, 'due', None)

Expand All @@ -124,11 +131,16 @@ def create_poc(request, course):
@coach_dashboard
def save_poc(request, course):
"""
Save changes to POC
Save changes to POC.
"""
poc = get_poc_for_coach(course, request.user)

def override_fields(parent, data, earliest=None):
"""
Recursively apply POC schedule data to POC by overriding the
`visible_to_staff_only`, `start` and `due` fields for units in the
course.
"""
blocks = {
str(child.location): child
for child in parent.get_children()}
Expand Down Expand Up @@ -163,16 +175,17 @@ def override_fields(parent, data, earliest=None):
content_type='application/json')


def parse_date(s):
if s:
try:
date, time = s.split(' ')
year, month, day = map(int, date.split('-'))
hour, minute = map(int, time.split(':'))
return datetime.datetime(
year, month, day, hour, minute, tzinfo=pytz.UTC)
except:
log.warn("Unable to parse date: " + s)
def parse_date(datestring):
"""
Generate a UTC datetime.datetime object from a string of the form
'YYYY-MM-DD HH:MM'. If string is empty or `None`, returns `None`.
"""
if datestring:
date, time = datestring.split(' ')
year, month, day = map(int, date.split('-'))
hour, minute = map(int, time.split(':'))
return datetime.datetime(
year, month, day, hour, minute, tzinfo=pytz.UTC)

return None

Expand All @@ -192,8 +205,12 @@ def get_poc_for_coach(course, coach):

def get_poc_schedule(course, poc):
"""
Generate a JSON serializable POC schedule.
"""
def visit(node, depth=1):
"""
Recursive generator function which yields POC schedule nodes.
"""
for child in node.get_children():
start = get_override_for_poc(poc, child, 'start', None)
if start:
Expand Down Expand Up @@ -301,8 +318,11 @@ def poc_gradebook(request, course):

poc = get_poc_for_coach(course, request.user)
with poc_context(poc):
course._field_data_cache = {}
course.set_grading_policy(course.grading_policy) # this is so awful
# The grading policy for the MOOC is probably already cached. We need
# to make sure we have the POC grading policy loaded.
course._field_data_cache = {} # pylint: disable=protected-access
course.set_grading_policy(course.grading_policy)

enrolled_students = User.objects.filter(
pocmembership__poc=poc,
pocmembership__active=1
Expand Down Expand Up @@ -343,8 +363,11 @@ def poc_grades_csv(request, course):

poc = get_poc_for_coach(course, request.user)
with poc_context(poc):
course._field_data_cache = {}
course.set_grading_policy(course.grading_policy) # this is so awful
# The grading policy for the MOOC is probably already cached. We need
# to make sure we have the POC grading policy loaded.
course._field_data_cache = {} # pylint: disable=protected-access
course.set_grading_policy(course.grading_policy)

enrolled_students = User.objects.filter(
pocmembership__poc=poc,
pocmembership__active=1
Expand All @@ -353,7 +376,7 @@ def poc_grades_csv(request, course):

header = None
rows = []
for student, gradeset, err_msg in grades:
for student, gradeset, __ in grades:
if gradeset:
# We were able to successfully grade this student for this
# course.
Expand All @@ -374,9 +397,9 @@ def poc_grades_csv(request, course):
rows.append([student.id, student.email, student.username,
gradeset['percent']] + row_percents)

buffer = StringIO()
writer = csv.writer(buffer)
buf = StringIO()
writer = csv.writer(buf)
for row in rows:
writer.writerow(row)

return HttpResponse(buffer.getvalue(), content_type='text/plain')
return HttpResponse(buf.getvalue(), content_type='text/plain')

0 comments on commit 396fa09

Please sign in to comment.