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 + +