diff options
author | adityacp | 2016-04-15 11:40:25 +0530 |
---|---|---|
committer | adityacp | 2016-05-04 22:47:34 +0530 |
commit | d53feb82cdd74e25045171acf70e513729ba993c (patch) | |
tree | 70af917cb9909adea89367461ca8c6212b7440df | |
parent | 8841e5ed4f8f79b7067ddb3523f4a3ec50f362b3 (diff) | |
download | online_test-d53feb82cdd74e25045171acf70e513729ba993c.tar.gz online_test-d53feb82cdd74e25045171acf70e513729ba993c.tar.bz2 online_test-d53feb82cdd74e25045171acf70e513729ba993c.zip |
rebase changes
-rw-r--r-- | yaksh/forms.py | 11 | ||||
-rw-r--r-- | yaksh/models.py | 3 | ||||
-rw-r--r-- | yaksh/templates/yaksh/showquestions.html | 13 | ||||
-rw-r--r-- | yaksh/urls.py | 5 | ||||
-rw-r--r-- | yaksh/views.py | 90 |
5 files changed, 113 insertions, 9 deletions
diff --git a/yaksh/forms.py b/yaksh/forms.py index 16f82fb..37741b9 100644 --- a/yaksh/forms.py +++ b/yaksh/forms.py @@ -152,7 +152,7 @@ class QuestionForm(forms.ModelForm): class Meta: model = Question - fields = '__all__' + exclude = ['user'] class RandomQuestionForm(forms.Form): @@ -165,8 +165,9 @@ class RandomQuestionForm(forms.Form): class QuestionFilterForm(forms.Form): def __init__(self, *args, **kwargs): + user = kwargs.pop("user") super(QuestionFilterForm, self).__init__(*args, **kwargs) - questions = Question.objects.all() + questions = Question.objects.filter(user_id=user) points_list = questions.values_list('points', flat=True).distinct() points_options = [('select', 'Select Marks')] points_options.extend([(point, point) for point in points_list]) @@ -188,6 +189,7 @@ class CourseForm(forms.ModelForm): model = Course fields = ['name', 'active', 'enrollment'] + class ProfileForm(forms.ModelForm): """ profile form for students and moderators """ @@ -205,3 +207,8 @@ class ProfileForm(forms.ModelForm): super(ProfileForm, self).__init__(*args, **kwargs) self.fields['first_name'].initial = user.first_name self.fields['last_name'].initial = user.last_name + + +class UploadFileForm(forms.Form): + file = forms.FileField() + diff --git a/yaksh/models.py b/yaksh/models.py index 561b334..c76d655 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -173,6 +173,9 @@ class Question(models.Model): # Tags for the Question. tags = TaggableManager(blank=True) + # user for particular question + user = models.ForeignKey(User, related_name="user") + def consolidate_answer_data(self, test_cases, user_answer): test_case_data_dict = [] question_info_dict = {} diff --git a/yaksh/templates/yaksh/showquestions.html b/yaksh/templates/yaksh/showquestions.html index ca51d1a..ba96728 100644 --- a/yaksh/templates/yaksh/showquestions.html +++ b/yaksh/templates/yaksh/showquestions.html @@ -10,6 +10,18 @@ {% endblock %} {% block manage %} + +{% if questions %} +<a href="{{URL_ROOT}}/exam/manage/download_questions/">Download Questions</a><br> +{% endif %} + +<h4>Upload csv file for adding questions</h4> +<form action="{{URL_ROOT}}/exam/manage/upload_questions/" method="post" enctype="multipart/form-data"> +{% csrf_token %} +{{ upload_form.as_p }} +<button class="btn" type="submit">Upload File</button> +</form> + <form name=frm action="" method="post"> {% csrf_token %} <div class="row" id="selectors"> @@ -31,6 +43,7 @@ </div> </div> <br> + <div id="filtered-questions"> {% for i in questions %} <input type="checkbox" name="question" value="{{ i.id }}"> <a href="{{URL_ROOT}}/exam/manage/addquestion/{{ i.id }}">{{ i }}</a><br> diff --git a/yaksh/urls.py b/yaksh/urls.py index b32bc36..4e72084 100644 --- a/yaksh/urls.py +++ b/yaksh/urls.py @@ -74,6 +74,7 @@ urlpatterns += [ views.enroll, {'was_rejected': True}), url(r'manage/reject/(?P<course_id>\d+)/(?P<user_id>\d+)/$', views.reject), url(r'manage/enrolled/reject/(?P<course_id>\d+)/(?P<user_id>\d+)/$', + views.reject, {'was_enrolled': True}), url(r'manage/toggle_status/(?P<course_id>\d+)/$', views.toggle_course_status), url(r'^ajax/questionpaper/(?P<query>.+)/$', views.ajax_questionpaper), @@ -89,4 +90,8 @@ urlpatterns += [ url(r'^manage/addteacher/(?P<course_id>\d+)/$', views.add_teacher), url(r'^manage/allotted_course/$', views.allotted_courses), url(r'^manage/remove_teachers/(?P<course_id>\d+)/$', views.remove_teachers) + url(r'^manage/download_questions/$', views.download_questions), + url(r'^manage/upload_questions/$', views.upload_questions) ] + + diff --git a/yaksh/views.py b/yaksh/views.py index a91da29..1d79a4c 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -23,7 +23,7 @@ from yaksh.models import Quiz, Question, QuestionPaper, QuestionSet, Course from yaksh.models import Profile, Answer, AnswerPaper, User, TestCase from yaksh.forms import UserRegisterForm, UserLoginForm, QuizForm,\ QuestionForm, RandomQuestionForm, TestCaseFormSet,\ - QuestionFilterForm, CourseForm, ProfileForm + QuestionFilterForm, CourseForm, ProfileForm, UploadFileForm from yaksh.xmlrpc_clients import code_server from settings import URL_ROOT from yaksh.models import AssignmentUpload @@ -166,6 +166,8 @@ def add_question(request, question_id=None): test_case_formset = add_or_delete_test_form(request.POST, form.save(commit=False)) if 'save_question' in request.POST: qtn = form.save(commit=False) + qtn.user = user + qtn.save() test_case_formset = TestCaseFormSet(request.POST, prefix='test', instance=qtn) form.save() question = Question.objects.order_by("-id")[0] @@ -780,6 +782,7 @@ def show_all_users(request): def ajax_questions_filter(request): """Ajax call made when filtering displayed questions.""" + user = request.user filter_dict = {} question_type = request.POST.get('question_type') marks = request.POST.get('marks') @@ -794,7 +797,7 @@ def ajax_questions_filter(request): if language != "select": filter_dict['language'] = str(language) - questions = list(Question.objects.filter(**filter_dict)) + questions = list(Question.objects.filter(**filter_dict).filter(user_id=user.id)) return my_render_to_response('yaksh/ajax_question_filter.html', {'questions': questions}) @@ -814,12 +817,14 @@ def show_all_questions(request): if data is not None: for i in data: question = Question.objects.get(id=i).delete() - questions = Question.objects.all() - form = QuestionFilterForm() + questions = Question.objects.filter(user_id=user.id) + form = QuestionFilterForm(user=user) + upload_form = UploadFileForm() context = {'papers': [], 'question': None, 'questions': questions, - 'form': form + 'form': form, + 'upload_form': upload_form } return my_render_to_response('yaksh/showquestions.html', context, context_instance=ci) @@ -944,9 +949,11 @@ def ajax_questionpaper(request, query): """ During question paper creation, ajax call made to get question details. """ + + user = request.user if query == 'marks': question_type = request.POST.get('question_type') - questions = Question.objects.filter(type=question_type) + questions = Question.objects.filter(type=question_type, user=user) marks = questions.values_list('points').distinct() return my_render_to_response('yaksh/ajax_marks.html', {'marks': marks}) elif query == 'questions': @@ -958,7 +965,7 @@ def ajax_questionpaper(request, query): random_question_list = ",".join(random_questions).split(',') question_list = fixed_question_list + random_question_list questions = list(Question.objects.filter(type=question_type, - points=marks_selected)) + points=marks_selected, user=user)) questions = [question for question in questions \ if not str(question.id) in question_list] return my_render_to_response('yaksh/ajax_questions.html', @@ -1151,3 +1158,72 @@ def remove_teachers(request, course_id): teachers = User.objects.filter(id__in=teacher_ids) course.remove_teachers(*teachers) return my_redirect('/exam/manage/courses') + + +@login_required +def download_questions(request): + user = request.user + if not is_moderator(user): + raise Http404('You are not allowed to view this page!') + questions = Question.objects.filter(user_id = user.id) + response = HttpResponse(content_type='text/csv') + response['Content-Disposition'] = 'attachment; filename="{0}_Questions.csv"'.format(user) + writer = csv.writer(response) + header = [ + 'summary', + 'description', + 'points', + 'test', + 'ref_code_path', + 'options', + 'language', + 'type', + 'active', + 'snippet' + ] + writer.writerow(header) + for que in questions: + row = [ + que.summary, + que.description, + que.points, + que.test, + que.ref_code_path, + que.options, + que.language, + que.type, + que.active, + que.snippet + ] + writer.writerow(row) + return response + + +@login_required +def upload_questions(request): + user = request.user + ci = RequestContext(request) + + if not is_moderator(user): + raise Http404('You are not allowed to view this page!') + + if request.method == 'POST': + form = UploadFileForm(request.POST, request.FILES) + if form.is_valid(): + csvfile = request.FILES['file'] + if csvfile.name.split('.')[1] == "csv": + reader = csv.DictReader(csvfile, delimiter=',') + for row in reader: + Question.objects.get_or_create(summary=row['summary'], + description=row['description'], points=row['points'], + test=row['test'], ref_code_path=row['ref_code_path'], + options=row['options'], language=row['language'], + type=row['type'], active=row['active'], + snippet=row['snippet'], user=user) + return my_redirect('/exam/manage/questions') + else: + raise Http404('Please Upload a csv file') + else: + return my_redirect('/exam/manage/questions') + else: + return my_redirect('/exam/manage/questions') |