2 from django.db import DatabaseError, transaction
4 from quotes.models import QuoteTag, Quote, Work, Author
6 def get_or_create_author(authorname, resultcontext):
7 authorname = authorname.strip()
9 return Author.objects.get(name=authorname)
10 except Author.DoesNotExist:
11 a = Author.objects.create(name=authorname,
12 pvt_notes="<p>Created during mass import</p>")
13 resultcontext["created_authors"] += [a]
16 def get_or_create_work(workname, author, resultcontext):
17 workname = workname.strip()
19 return Work.objects.get(name=workname, author=author)
20 except Work.DoesNotExist:
21 w = Work.objects.create(name=workname,
23 pvt_notes="<p>Created during mass import</p>")
24 resultcontext["created_works"] += [w]
27 def get_or_create_tag(tagname, resultcontext):
29 return QuoteTag.objects.get(tag=tagname)
30 except QuoteTag.DoesNotExist:
31 t = QuoteTag.objects.create(tag=tagname)
32 resultcontext["created_tags"] += [t]
35 def add_tags_on_quote(quote, tagline, resultcontext):
36 for tagname in tagline.split(","):
37 tagname = tagname.strip()
40 tag = get_or_create_tag(tagname, resultcontext)
43 def paragraphize(text):
45 for line in text.splitlines():
48 paragraph += "<p>%s</p>" % line
49 # rest of the HTML will be bleach.clean()'d
52 def create_quote(quotetext, authorname, workname, tagline, resultcontext):
53 author = get_or_create_author(authorname, resultcontext)
54 work = get_or_create_work(workname, author, resultcontext)
55 quotetext = paragraphize(quotetext)
56 quote = Quote.objects.create(text=quotetext, work=work)
57 add_tags_on_quote(quote, tagline, resultcontext)
58 resultcontext["created_quotes"] += [quote]
60 def create_all_quotes(allquotes, resultcontext):
61 quotesep = re.compile(r'^\s*===+.*$', re.MULTILINE)
62 elemsep = re.compile(r'^\s*---+.*$', re.MULTILINE)
64 for fullquote in quotesep.split(allquotes):
65 if not fullquote: continue
67 elements = elemsep.split(fullquote)
68 if len(elements) not in [3, 4]:
69 resultcontext["rejected"] += [fullquote]
71 quotetext, authorname, workname = elements[0:3]
72 if len(elements) == 4:
76 create_quote(quotetext, authorname, workname, tagline,
79 def domassimport(allquotes):
81 resultcontext["created_quotes"] = []
82 resultcontext["created_tags"] = []
83 resultcontext["created_works"] = []
84 resultcontext["created_authors"] = []
85 resultcontext["rejected"] = []
86 resultcontext["fatal_error"] = False
89 with transaction.atomic():
90 create_all_quotes(allquotes, resultcontext)
91 except DatabaseError as e:
92 resultcontext["fatal_error"] = True
93 resultcontext["fatal_error_message"] = e.__cause__