|
| 1 | +from django.core.management.base import BaseCommand |
| 2 | +from django.db import transaction as tx |
| 3 | +import csv |
| 4 | +from website.models import Question, Answer |
| 5 | +from django.conf import settings |
| 6 | +from django.utils.html import strip_tags |
| 7 | +import uuid |
| 8 | + |
| 9 | +class Command(BaseCommand): |
| 10 | + def add_arguments(self, parser): |
| 11 | + parser.add_argument('foss', nargs='+',type=str, help='Indicates the foss list.') |
| 12 | + |
| 13 | + @tx.atomic |
| 14 | + def handle(self, *args, **options): |
| 15 | + print("Generating Question Metadata. Please wait...") |
| 16 | + meta_file_name = 'metadata_'+uuid.uuid4().hex+".csv" |
| 17 | + with open(settings.BASE_DIR +"/"+ meta_file_name, "w+", newline='') as metafile: |
| 18 | + metawriter = csv.writer(metafile, dialect='excel', delimiter='|') |
| 19 | + metawriter.writerow(["FOSS","Tutorial","Video Minute Range","Video Second Range","Question Title","Question Body","Question_Date","Question Posted By","Answer No.","Answer Body","Answer Date", "Answer Posted By"]) |
| 20 | + foss = options['foss'] |
| 21 | + for f in foss: |
| 22 | + questions = Question.objects.filter(category=f, status=1) |
| 23 | + for q in questions: |
| 24 | + metawriter.writerow([q.category,q.tutorial,q.minute_range,q.second_range,q.title,strip_tags(q.body).strip(" "),q.date_created,q.user()]) |
| 25 | + answers = Answer.objects.filter(question=q) |
| 26 | + for i, a in enumerate(answers): |
| 27 | + metawriter.writerow(["","","","","","","","",i+1, strip_tags(a.body).strip(" "),a.date_created, a.user()]) |
| 28 | + metawriter.writerow([]) |
| 29 | + print("Metadata File Generated. Please find the file at location given below.") |
| 30 | + print(metafile.name) |
| 31 | + |
0 commit comments