|
7 | 7 | from rest_framework import status |
8 | 8 | from django.db import transaction |
9 | 9 | from arkav.utils.exceptions import ArkavAPIException |
| 10 | +import csv |
| 11 | +from django.http import HttpResponse |
10 | 12 |
|
11 | 13 |
|
12 | 14 | class ArkalogicaService: |
@@ -34,6 +36,46 @@ def get_submission(self, requested_user): |
34 | 36 | submission, state = Submission.objects.get_or_create(user=requested_user) |
35 | 37 | return submission |
36 | 38 |
|
| 39 | + @transaction.atomic |
| 40 | + def get_submissions(self, session): |
| 41 | + content = [] |
| 42 | + |
| 43 | + questions_id = [] |
| 44 | + # Create header (line 1) |
| 45 | + header = [] |
| 46 | + header.append("user") |
| 47 | + if session.questions.count() > 0: |
| 48 | + for q in session.questions.all(): |
| 49 | + questions_id.append(q.id) |
| 50 | + header.append('%s' % q) |
| 51 | + |
| 52 | + content.append(header) |
| 53 | + |
| 54 | + # Create content (line 2-...) |
| 55 | + all_submissions = session.submission.all() |
| 56 | + for sub in all_submissions: |
| 57 | + user_submission = [] |
| 58 | + user_submission.append(sub.user.full_name) |
| 59 | + for q in questions_id: |
| 60 | + ans = Answer.objects.filter(submission__user__id=sub.user.id, question=q).first() |
| 61 | + if ans: |
| 62 | + user_submission.append(ans.tag) |
| 63 | + else: |
| 64 | + user_submission.append('-') |
| 65 | + content.append(user_submission) |
| 66 | + |
| 67 | + return content |
| 68 | + |
| 69 | + def create_csv(self, content, fileName): |
| 70 | + response = HttpResponse(content_type='text/csv') |
| 71 | + response['Content-Disposition'] = 'attachment; filename="%s.csv"' % fileName |
| 72 | + |
| 73 | + writer = csv.writer(response) |
| 74 | + for c in content: |
| 75 | + writer.writerow(c) |
| 76 | + |
| 77 | + return response |
| 78 | + |
37 | 79 | @transaction.atomic |
38 | 80 | def submit_answer(self, answer_data, requested_user): |
39 | 81 | session = Session.objects.first() |
|
0 commit comments