<commit_before>import urllib
from django.views.generic import ListView
# from django import forms
from django.shortcuts import get_object_or_404

from trix.trix_core import models


# class TagSelectForm(forms.Form):
#     tag_choice = forms.ModelChoiceField(queryset=None)

#     def __init__(self, *args, **kwargs):
#         choices = kwargs.pop('choices')
#         super(TagSelectForm, self).__init__(*args, **kwargs)
#         self.fields['tag_choice'].queryset = choices


class CourseDetailView(ListView):
    template_name = "trix_student/course.django.html"
    paginate_by = 20
    context_object_name = 'assignment_list'

    def get(self, request, course_id):
        self.course_id = course_id
        self.course = get_object_or_404(models.Course, id=self.course_id)
        
        self.all_available_assignments = models.Assignment.objects\
            .filter_by_tag(self.course.course_tag)\
            .filter_by_tag(self.course.active_period)

        self.selected_tags = self._get_selected_tags()
        self.selectable_tags = self._get_selectable_tags()
        self.non_removeable_tags = self.get_nonremovable_tags()

        return super(CourseDetailView, self).get(request, course_id)

    def get_queryset(self):
        assignments = self.all_available_assignments
        if self.selected_tags:
            for tagstring in self.selected_tags:
                assignments = assignments.filter(tags__tag=tagstring)
        return assignments

    def _get_selectable_tags(self):
        already_selected_tags = [
            self.course.course_tag.tag,
            self.course.active_period.tag
        ] + self.selected_tags

        tags = models.Tag.objects\
            .filter(assignment__in=self.get_queryset())\
            .exclude(tag__in=already_selected_tags)\
            .order_by('tag')\
            .distinct()\
            .values_list('tag', flat=True)
        return tags

    def _get_selected_tags(self):
        tags_string = self.request.GET.get('tags', None)
        tags = []
        if tags_string:
            tags = tags_string.split(',')
            tags.sort()
        return tags

    def get_nonremovable_tags(self):
        return [self.course.active_period]

    def _get_user_is_admin(self):
        if self.request.user.is_authenticated():
            if self.request.user.is_admin:
                return True
            else:
                return self.course.admins.filter(id=self.request.user.id)
        else:
            return False

    def _get_assignments_solved_percentage(self):
        num_solved = models.AssignmentSolution.objects.filter(assignment__in=self.get_queryset()).count()
        num_total = self.get_queryset().count()
        return int(num_solved / float(num_total) * 100)

    def get_context_data(self, **kwargs):
        context = super(CourseDetailView, self).get_context_data(**kwargs)

        context['non_removeable_tags'] = [self.course.active_period]
        context['course'] = self.course
        context['selected_tags'] = self.selected_tags
        context['selectable_tags'] = self.selectable_tags
        context['user_is_admin'] = self._get_user_is_admin()
        context['urlencoded_success_url'] = urllib.urlencode({
            'success_url': self.request.get_full_path()})


        context['assignments_solved_percentage'] = self._get_assignments_solved_percentage()
        print context['assignments_solved_percentage']
        # context['tag_select_form'] = TagSelectForm(choices=self.tags)
        return context<commit_msg>trix_student: Removed print<commit_after>import urllib
from django.views.generic import ListView
# from django import forms
from django.shortcuts import get_object_or_404

from trix.trix_core import models


# class TagSelectForm(forms.Form):
#     tag_choice = forms.ModelChoiceField(queryset=None)

#     def __init__(self, *args, **kwargs):
#         choices = kwargs.pop('choices')
#         super(TagSelectForm, self).__init__(*args, **kwargs)
#         self.fields['tag_choice'].queryset = choices


class CourseDetailView(ListView):
    template_name = "trix_student/course.django.html"
    paginate_by = 20
    context_object_name = 'assignment_list'

    def get(self, request, course_id):
        self.course_id = course_id
        self.course = get_object_or_404(models.Course, id=self.course_id)
        
        self.all_available_assignments = models.Assignment.objects\
            .filter_by_tag(self.course.course_tag)\
            .filter_by_tag(self.course.active_period)

        self.selected_tags = self._get_selected_tags()
        self.selectable_tags = self._get_selectable_tags()
        self.non_removeable_tags = self.get_nonremovable_tags()

        return super(CourseDetailView, self).get(request, course_id)

    def get_queryset(self):
        assignments = self.all_available_assignments
        if self.selected_tags:
            for tagstring in self.selected_tags:
                assignments = assignments.filter(tags__tag=tagstring)
        return assignments

    def _get_selectable_tags(self):
        already_selected_tags = [
            self.course.course_tag.tag,
            self.course.active_period.tag
        ] + self.selected_tags

        tags = models.Tag.objects\
            .filter(assignment__in=self.get_queryset())\
            .exclude(tag__in=already_selected_tags)\
            .order_by('tag')\
            .distinct()\
            .values_list('tag', flat=True)
        return tags

    def _get_selected_tags(self):
        tags_string = self.request.GET.get('tags', None)
        tags = []
        if tags_string:
            tags = tags_string.split(',')
            tags.sort()
        return tags

    def get_nonremovable_tags(self):
        return [self.course.active_period]

    def _get_user_is_admin(self):
        if self.request.user.is_authenticated():
            if self.request.user.is_admin:
                return True
            else:
                return self.course.admins.filter(id=self.request.user.id)
        else:
            return False

    def _get_assignments_solved_percentage(self):
        num_solved = models.AssignmentSolution.objects.filter(assignment__in=self.get_queryset()).count()
        num_total = self.get_queryset().count()
        return int(num_solved / float(num_total) * 100)

    def get_context_data(self, **kwargs):
        context = super(CourseDetailView, self).get_context_data(**kwargs)

        context['non_removeable_tags'] = [self.course.active_period]
        context['course'] = self.course
        context['selected_tags'] = self.selected_tags
        context['selectable_tags'] = self.selectable_tags
        context['user_is_admin'] = self._get_user_is_admin()
        context['urlencoded_success_url'] = urllib.urlencode({
            'success_url': self.request.get_full_path()})


        context['assignments_solved_percentage'] = self._get_assignments_solved_percentage()
        # context['tag_select_form'] = TagSelectForm(choices=self.tags)
        return context
{'fraction_non_alphanumeric': 0.10109173401389479, 'fraction_numerical': 0.003119240039699419, 'mean_word_length': 2.810913019989195, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6626078', 'n_tokens_mistral': 2148, 'n_tokens_neox': 2081, 'n_words': 325}