From 9d014bc124a285d5cb60a1efc0242f971a79bd26 Mon Sep 17 00:00:00 2001 From: Invinceaman Date: Mon, 9 Nov 2020 13:56:29 -0500 Subject: [PATCH] add tag import script --- .../management/commands/upload_tags.py | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 studyguides/apps/courses/management/commands/upload_tags.py diff --git a/studyguides/apps/courses/management/commands/upload_tags.py b/studyguides/apps/courses/management/commands/upload_tags.py new file mode 100644 index 0000000..486c976 --- /dev/null +++ b/studyguides/apps/courses/management/commands/upload_tags.py @@ -0,0 +1,23 @@ +from django.core.management.base import BaseCommand, CommandError +import csv +from studyguides.apps.courses.models import Tag + +class Command(BaseCommand): + help = 'upload tags from csv' + + def add_arguments(self, parser): + parser.add_argument('filename') + + def handle(self,*args,**kwargs): + try: + with open(kwargs['filename']) as tagfile: + tag_reader = csv.reader(tagfile, delimiter=',') + for row in tag_reader: + name = row[0].strip() + url = row[1].strip() + tag = Tag.objects.get_or_create(name=name,url=url)[0] + tag.save() + except OSError as ex: + raise CommandError(str(ex)) from ex + +