def browse_one_course(request, course_query, school): """ View for viewing notes from a fuzzy course search :course_query: unicode url match, to be type parsed """ # TODO: combine this function with `b_school_course` response = nav_helper(request) try: course_query = int(course_query) except ValueError: # cant be cast as an int, so we will search for it as a string pass course, files = Course.get_notes(course_query, school) response['course'], response['files'] = course, files # get the users who are members of the course response['profiles'] = course.userprofile_set.all() # get the karma events associated with the course response['events'] = course.reputationevent_set.order_by('-timestamp').all() # FIXME: possibly order-by response['viewed_files'] = request.user.get_profile().files.all() # FIXME: I don't like this logic one bit, either annotate the db query or fix the schema to NEVER do this response['thanked_files'] = [] response['flagged_files'] = [] for file in files: _vote = file.votes.filter(user=request.user) if _vote.exists(): if _vote[0].up: # assuming only one vote result response['thanked_files'].append(file.id) else: response['flagged_files'].append(file.id) return render(request, 'browse_one_course.html', response)
def karma_events(request): """ Shows a time sorted log of your events that affect your karma score positively or negatively. """ # navigation.html response = nav_helper(request) response['events'] = request.user.get_profile().reputationEvents.order_by('-timestamp').all() return render(request, 'karma-events.html', response)
def file(request, note_pk, action=None): """ View Note HTML Args: request: Django request object note_pk: file_pk int action: last url segment string indicating initial state. i.e: 'edit' """ # Check that user has permission to read #profile = request.user.get_profile() response = nav_helper(request) user = request.user try: profile = user.get_profile() except: raise Http404 # If the user does not have read permission, and the # Requested files is not theirs ''' if not profile.can_read and not userCanView(request.user, File.objects.get(pk=note_pk)): #file_denied(request, note_pk) pass ''' try: file = File.objects.get(pk=note_pk) except: raise Http404 # Increment note view count file.viewCount += 1 file.save() # If this file is not in the user's collection, karmic purchase occurs #if file not in profile.files.all(): if(not userCanView(user, File.objects.get(pk=note_pk))): # Buy Note viewing privelege for karma # awardKarma will handle deducting appropriate karma profile.awardKarma('view-file', school=profile.school, course=file.course, file=file) # Add 'purchased' file to user's collection profile.files.add(file) profile.save() # This is ugly, but is needed to be able to get the note type full name file_type = [t[1] for t in file.FILE_TYPES if t[0] == file.type][0] url = iri_to_uri(file.file.url) response['owns_file'] = (file.owner == request.user) response['file'] = file response['file_type'] = file_type response['url'] = url if action == 'edit': #print 'ACTION EDIT' response['editing_file'] = True else: response['editing_file'] = False #print 'ACTION NONE' return render(request, 'view-file.html', response)
def getting_started(request): """ View for introducing a user to the site and asking them to accomplish intro tasks """ response = nav_helper(request) response['tasks'] = [] for task in tasks: t = {} t['message'] = task().message t['status'] = task().check(request.user.get_profile()) t['karma'] = task().karma response['tasks'].append(t) return render(request, 'getting-started.html', response)
def browse_schools(request): """ Server-side templated browsing of notes by school, course and note """ response = nav_helper(request) # make this order by the most notes in a school response['title'] = u"Schools" response['schools'] = School.objects.annotate(num_course=Count('course'))\ .order_by('num_course').reverse().filter(num_course__gt=0).all() if request.user.get_profile().school not in response['schools']: # create a new list of the user's school, extend it with the current list of schools # this prepends the user's school to the top of the school list response['schools'] = [request.user.get_profile().school] + list(response['schools']) # this converts the QuerySet into a list, so this is not typesafe, but django templates do not care return render(request, 'browse_schools.html', response)
def browse_courses(request, school_query): """ View for courses beloging to :school_query: :school_query: comes as unicode, if can be int, pass as int """ response = nav_helper(request) try: school_query = int(school_query) except ValueError: # cant be cast as an int, so we will search for it as a string pass courses = School.get_courses(school_query) if isinstance(courses, tuple): # FIXME response['school'], response['courses'] = courses return render(request, 'browse_courses.html', response) else: raise Http404
def profile(request): """ User Profile """ response = nav_helper(request) response['course_json_url'] = '/jq_course' # FIXME: replace this with a reverse urls.py query response['your_files'] = File.objects.filter(owner=request.user).all() return render(request, 'navigation.html', response)
def e404(request): response = nav_helper(request) return render(request, '404.html', response)