]> gitweb.fperrin.net Git - djsite.git/blob - quotes/massimport.py
ade6f3899879bdbc5a76a9d10e12b0bc71ac8b02
[djsite.git] / quotes / massimport.py
1 import re
2 from django.db import DatabaseError, transaction
3
4 from quotes.models import QuoteTag, Quote, Work, Author
5
6 def get_or_create_author(authorname, resultcontext):
7     authorname = authorname.strip()
8     try:
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]
14         return a
15
16 def get_or_create_work(workname, author, resultcontext):
17     workname = workname.strip()
18     try:
19         return Work.objects.get(name=workname, author=author)
20     except Work.DoesNotExist:
21         w = Work.objects.create(name=workname,
22                                 author=author,
23                                 pvt_notes="<p>Created during mass import</p>")
24         resultcontext["created_works"] += [w]
25         return w
26
27 def get_or_create_tag(tagname, resultcontext):
28     try:
29         return QuoteTag.objects.get(tag=tagname)
30     except QuoteTag.DoesNotExist:
31         t = QuoteTag.objects.create(tag=tagname)
32         resultcontext["created_tags"] += [t]
33         return t
34     
35 def add_tags_on_quote(quote, tagline, resultcontext):
36     for tagname in tagline.split(","):
37         tagname = tagname.strip()
38         if not tagname:
39             continue
40         tag = get_or_create_tag(tagname, resultcontext)
41         quote.tags.add(tag)
42
43 def paragraphize(text):
44     paragraph = ""
45     for line in text.splitlines():
46         line = line.strip()
47         if not line: continue
48         paragraph += "<p>%s</p>" % line
49         # rest of the HTML will be bleach.clean()'d
50     return paragraph
51         
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]
59
60 def create_all_quotes(allquotes, resultcontext):
61     quotesep = re.compile(r'^\s*===+.*$', re.MULTILINE)
62     elemsep = re.compile(r'^\s*---+.*$', re.MULTILINE)
63     
64     for fullquote in quotesep.split(allquotes):
65         if not fullquote: continue
66
67         elements = elemsep.split(fullquote)
68         if len(elements) not in [3, 4]:
69             resultcontext["rejected"] += [fullquote]
70             continue
71         quotetext, authorname, workname = elements[0:3]
72         if len(elements) == 4:
73             tagline = elements[3]
74         else:
75             tagline = ''
76         create_quote(quotetext, authorname, workname, tagline,
77                      resultcontext)
78
79 def domassimport(allquotes):
80     resultcontext = {}
81     resultcontext["created_quotes"] = []
82     resultcontext["created_tags"] = []
83     resultcontext["created_works"] = []
84     resultcontext["created_authors"] = []
85     resultcontext["rejected"] = []
86     resultcontext["fatal_error"] = False
87
88     try:
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__
94
95     return resultcontext