mirror of
https://github.com/tjsga/studyguides.git
synced 2025-04-05 03:40:16 -04:00
add tag import script
This commit is contained in:
parent
1342505783
commit
9d014bc124
23
studyguides/apps/courses/management/commands/upload_tags.py
Normal file
23
studyguides/apps/courses/management/commands/upload_tags.py
Normal file
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user