Skip to content

Commit

Permalink
Merge pull request #11 from ccnmtl/optimize
Browse files Browse the repository at this point in the history
Optimize
  • Loading branch information
thraxil committed Aug 26, 2015
2 parents 3b430b6 + 11e38a0 commit f60da4c
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2.0.3
=====
* Minor optimizations

2.0.2
=====
* Remove user from course - remove from both the student group and faculty group
Expand Down
14 changes: 7 additions & 7 deletions courseaffils/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ def users_in_course(course):
return User.objects.filter(groups=course.group)


def in_course(user, group_or_course):
def in_course(username, group_or_course):
group = getattr(group_or_course, 'group', group_or_course)
try:
return group.user_set.get(username=user)
except:
return group.user_set.get(username=username)
except User.DoesNotExist:
return False


def in_course_or_404(user, group_or_course):
def in_course_or_404(username, group_or_course):
"Supports either the course-group or course as second arg"
group = getattr(group_or_course, 'group', group_or_course)
try:
return group.user_set.get(username=user)
except:
return group.user_set.get(username=username)
except User.DoesNotExist:
template = get_template('not_in_course.html')
context = Context(
{
'user': user,
'user': username,
'course': group,
})
response_body = template.render(context)
Expand Down
4 changes: 2 additions & 2 deletions courseaffils/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ def decorate_request(request, course):
if requested_view in AUTO_COURSE_SELECT:
chosen_course = AUTO_COURSE_SELECT[requested_view](
*view_args, **view_kwargs)
elif len(available_courses) == 1 and not request.user.is_staff:
chosen_course = available_courses[0]
elif available_courses.count() == 1 and not request.user.is_staff:
chosen_course = available_courses.first()

if (chosen_course and
(chosen_course in available_courses or request.user.is_staff)):
Expand Down
23 changes: 12 additions & 11 deletions courseaffils/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# is installed.

from django.db import models
from django.contrib.auth.models import Group, User
from django.contrib.auth.models import Group
import re
from django.conf import settings

Expand Down Expand Up @@ -39,13 +39,13 @@ def students(self):
if not self.faculty_group:
return members
else:
faculty = self.faculty_group.user_set.all()
return [m for m in members if m not in faculty]
ids = self.faculty_group.user_set.values_list('id', flat=True)
return members.exclude(id__in=ids)

@property
def faculty(self):
if self.faculty_group:
return self.faculty_group.user_set.all()
return self.faculty_group.user_set.all().select_related('user')
else:
return tuple()

Expand All @@ -64,17 +64,18 @@ def faculty_filter(self):
) & models.Q(course=self)

def is_faculty(self, user):
return (user.is_staff or user in self.faculty)
return (user.is_staff or
self.faculty_group.user_set.filter(id=user.id).count() > 0)

def is_member(self, user):
return (user.is_staff or user in self.members)
return (user.is_staff or
self.group.user_set.filter(id=user.id).count() > 0)

def is_true_member(self, user):
try:
self.group.user_set.get(id=user.id)
return True
except User.DoesNotExist:
return False
return self.group.user_set.filter(id=user.id).count() > 0

def is_true_faculty(self, user):
return self.faculty_group.user_set.filter(id=user.id).count() > 0

is_course = True

Expand Down
9 changes: 5 additions & 4 deletions courseaffils/templatetags/coursetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,13 @@ def __init__(self, varname, user, course):
def execute_query(self, user, course):
if not course:
return "no-course"
if user not in course.members:
return "non-member"
elif user in course.faculty:
elif course.is_true_faculty(user):
return "instructor"
else:
elif course.is_true_member(user):
return "student"
else:
return "non-member"


register.tag('course_role', CourseRole.process_tag)

Expand Down
7 changes: 7 additions & 0 deletions runtests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
""" run tests for coursaffils
$ virtualenv ve
$ ./ve/bin/pip install -r test_reqs.txt
$ ./ve/bin/python runtests.py
"""

from django.conf import settings
from django.core.management import call_command
from courseaffils.columbia import CourseStringMapper
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

version = '2.0.2'
version = '2.0.3'


setup(name='django-courseaffils',
Expand Down

0 comments on commit f60da4c

Please sign in to comment.