root / cloudcms / forms.py @ d240ebcb
History | View | Annotate | Download (10.3 kB)
1 |
# -*- coding: utf-8 -*-
|
---|---|
2 |
|
3 |
import zipfile |
4 |
import tempfile |
5 |
import os |
6 |
import glob |
7 |
import logging |
8 |
import StringIO |
9 |
|
10 |
from django import forms |
11 |
from django.conf import settings |
12 |
from django.db import transaction |
13 |
from django.core.files import File |
14 |
|
15 |
from feincms.module.medialibrary.models import Category as MediaCategory, \ |
16 |
MediaFile
|
17 |
|
18 |
from cloudcmsguide.models import UserGuideEntry |
19 |
from cloudcmsfaq.models import Category as FaqCategory, Question |
20 |
from cloudcms.rstutils import generate_rst_contents_from_dir |
21 |
from cloudcms.models import Service, ServiceTranslation, Application |
22 |
from feincms.content.raw.models import RawContent |
23 |
|
24 |
logger = logging.getLogger('cloudcms.rstimport')
|
25 |
|
26 |
# base filename to service slug map
|
27 |
DEFAULT_SERVICE_MAP = { |
28 |
'cyclades':'cyclades', |
29 |
'okeanos':'okeanos', |
30 |
'pithos':'pithos' |
31 |
} |
32 |
|
33 |
DEFAULT_REGION_MAP = { |
34 |
'faq':'main', |
35 |
'userguide':'main', |
36 |
} |
37 |
|
38 |
DEFAULT_RESIZE_GEOMETRY = (400, 400) |
39 |
|
40 |
SERVICE_MAP = getattr(settings, 'CMS_RST_IMPORT_SERVICE_FILE_MAP', |
41 |
DEFAULT_SERVICE_MAP) |
42 |
REGIONS_MAP = getattr(settings, 'CMS_RST_IMPORT_REGIONS_MAP', |
43 |
DEFAULT_REGION_MAP) |
44 |
RESIZE_GEOMETRY = getattr(settings, 'CMS_RST_IMPORT_RESIZE_GEOMETRY', |
45 |
DEFAULT_RESIZE_GEOMETRY) |
46 |
|
47 |
def service_from_filename(rst): |
48 |
fname = os.path.basename(rst).replace(".rst","") |
49 |
service_slug = DEFAULT_SERVICE_MAP.get(fname, None)
|
50 |
if not service_slug: |
51 |
return None |
52 |
|
53 |
try:
|
54 |
return Service.objects.filter(translations__slug=service_slug)[0] |
55 |
except IndexError: |
56 |
return None |
57 |
except Service.DoesNotExist:
|
58 |
return None |
59 |
|
60 |
return None |
61 |
|
62 |
|
63 |
def get_media_category(slug): |
64 |
return MediaCategory.objects.get(slug=slug)
|
65 |
|
66 |
|
67 |
CATEGORIES_CHOICES = (('faq', 'FAQs'), ('user-guide', 'User guide')) |
68 |
|
69 |
class RstZipImportForm(forms.Form): |
70 |
|
71 |
FAQ_MEDIA_CATEGORY = 'faq-images'
|
72 |
GUIDE_MEDIA_CATEGORY = 'user-guide-images'
|
73 |
|
74 |
clean_data = forms.BooleanField(initial=False, required=False) |
75 |
dry_run = forms.BooleanField(initial=True, required=False, |
76 |
widget=forms.HiddenInput) |
77 |
import_file = forms.FileField(required=True)
|
78 |
|
79 |
def __init__(self, *args, **kwargs): |
80 |
super(RstZipImportForm, self).__init__(*args, **kwargs) |
81 |
self.log_data = "" |
82 |
|
83 |
def log(self, msg): |
84 |
self.log_data += "\n" + msg |
85 |
|
86 |
def get_tmp_file(self, f): |
87 |
tmp = tempfile.mktemp('cloudcms-sphinx-import')
|
88 |
f.file.reset() |
89 |
fd = file(tmp, 'w') |
90 |
fd.write(f.read()) |
91 |
fd.close() |
92 |
return tmp
|
93 |
|
94 |
def clean_import_file(self): |
95 |
f = self.cleaned_data['import_file'] |
96 |
tmpfile = self.get_tmp_file(f)
|
97 |
if not zipfile.is_zipfile(tmpfile): |
98 |
raise forms.ValidationError("Invalid zip file") |
99 |
return f
|
100 |
|
101 |
def clean(self, *args, **kwargs): |
102 |
data = super(RstZipImportForm, self).clean(*args, **kwargs) |
103 |
return data
|
104 |
|
105 |
def clean_existing_data(self): |
106 |
logger.warning("Removing all FAQ questions")
|
107 |
Question.objects.all().delete() |
108 |
logger.warning("Removing all User Guide entries")
|
109 |
UserGuideEntry.objects.all().delete() |
110 |
logger.warning("Removing all media files in categories %s", [self.FAQ_MEDIA_CATEGORY, |
111 |
self.GUIDE_MEDIA_CATEGORY])
|
112 |
MediaFile.objects.filter(categories__slug__in=[self.FAQ_MEDIA_CATEGORY, \
|
113 |
self.GUIDE_MEDIA_CATEGORY]).delete()
|
114 |
|
115 |
def save(self, user, use_dir=None): |
116 |
stream = StringIO.StringIO() |
117 |
stream_handler = logging.StreamHandler(stream) |
118 |
stream_handler.setFormatter(logging.Formatter('<div class="log-entry %(levelname)s"><em>%(levelname)s</em>'
|
119 |
'<pre>%(message)s</pre></div>'))
|
120 |
logger.addHandler(stream_handler) |
121 |
old_level = logger.level |
122 |
logger.setLevel(logging.DEBUG) |
123 |
|
124 |
dry_run = self.cleaned_data.get('dry_run') |
125 |
clean_data = self.cleaned_data.get('clean_data') |
126 |
import_file = self.cleaned_data.get('import_file') |
127 |
|
128 |
if not use_dir: |
129 |
zipdir = tempfile.mkdtemp('cloudcms-sphinx-exports')
|
130 |
zipdatafile = self.get_tmp_file(import_file)
|
131 |
zipf = zipfile.ZipFile(file(zipdatafile))
|
132 |
zipf.extractall(zipdir) |
133 |
else:
|
134 |
zipdir = use_dir |
135 |
|
136 |
subdirs = os.listdir(zipdir) |
137 |
if len(subdirs) == 1 and os.path.isdir(os.path.join(zipdir, \ |
138 |
subdirs[0])) and subdirs[0] != 'source': |
139 |
zipdir = os.path.join(zipdir, subdirs[0])
|
140 |
|
141 |
sid = transaction.savepoint() |
142 |
|
143 |
if clean_data:
|
144 |
try:
|
145 |
logger.warning("Removing exising entries")
|
146 |
self.clean_existing_data()
|
147 |
except Exception, e: |
148 |
transaction.savepoint_rollback(sid) |
149 |
logger.exception("Failed to clean existing data")
|
150 |
logger.removeHandler(stream_handler) |
151 |
logger.setLevel(old_level) |
152 |
return False, stream.getvalue() |
153 |
|
154 |
ret = ""
|
155 |
try:
|
156 |
logger.warning("Parsing contents of '%s'", zipdir)
|
157 |
for data_type, rst, category, slug, title, html_content, \
|
158 |
images, stderr in generate_rst_contents_from_dir(zipdir):
|
159 |
|
160 |
if stderr:
|
161 |
logger.error("Docutils error output for '%s'\n: %s" % (rst, stderr, ))
|
162 |
|
163 |
service = service_from_filename(rst) |
164 |
if not service: |
165 |
logger.info("Skipping entry for file '%s'. No category found" % rst)
|
166 |
continue
|
167 |
|
168 |
logger.info("Processing, '%s'" % (rst, ))
|
169 |
|
170 |
|
171 |
# first save media files
|
172 |
cat = False
|
173 |
newimages = [] |
174 |
if data_type == 'userguide': |
175 |
cat = get_media_category(self.GUIDE_MEDIA_CATEGORY)
|
176 |
if data_type == 'faq': |
177 |
cat = get_media_category(self.FAQ_MEDIA_CATEGORY)
|
178 |
|
179 |
if not cat: |
180 |
logger.info("Skipping %s, no media category found for '%s'",
|
181 |
rst, data_type) |
182 |
continue
|
183 |
|
184 |
for imgname, imgpath, imgabspath in images: |
185 |
logger.debug("Checking image (%s, %s, %s)", imgname, imgpath, imgabspath)
|
186 |
newalt, newpath = create_or_update_media_file(cat, \ |
187 |
imgname, imgabspath) |
188 |
|
189 |
html_content = html_content.replace(imgpath, newpath) |
190 |
|
191 |
# now html contains correct image links, we are ready to save
|
192 |
# the faq/guide content
|
193 |
if data_type == 'faq': |
194 |
logger.info('Processing FAQ entry, %s, %s, %s', service, slug, title)
|
195 |
cat = add_or_update_faq_category(category[0], category[1]) |
196 |
question = add_or_update_faq_question(user, service, cat, slug, \ |
197 |
title, html_content) |
198 |
|
199 |
if data_type == 'userguide': |
200 |
logger.info('Processing USER GUIDE entry, %s, %s, %s', service, slug, title)
|
201 |
guide_entry = add_or_update_guide_entry(user, service, slug, \ |
202 |
title, html_content) |
203 |
|
204 |
|
205 |
except Exception, e: |
206 |
logger.exception("RST import failed")
|
207 |
logger.removeHandler(stream_handler) |
208 |
logger.setLevel(old_level) |
209 |
transaction.savepoint_rollback(sid) |
210 |
return False, stream.getvalue() |
211 |
|
212 |
if dry_run:
|
213 |
transaction.savepoint_rollback(sid) |
214 |
else:
|
215 |
transaction.savepoint_commit(sid) |
216 |
|
217 |
return True, stream.getvalue() |
218 |
|
219 |
|
220 |
def create_or_update_media_file(category, name, path): |
221 |
logger.info("Create or update media file, %s, %s, [category: %s]", name,
|
222 |
path, category) |
223 |
name = category.title + " " + name
|
224 |
|
225 |
try:
|
226 |
# TODO: Check language ?????
|
227 |
mf = MediaFile.objects.get(categories__in=[category], translations__caption=name) |
228 |
logger.info("Media file found")
|
229 |
except MediaFile.DoesNotExist:
|
230 |
logger.info("Media file not found, creating...")
|
231 |
mf = MediaFile() |
232 |
mf.file = File(open(path))
|
233 |
mf.save() |
234 |
mf.translations.create(caption=name) |
235 |
mf.categories.clear() |
236 |
mf.categories = [category] |
237 |
mf.save() |
238 |
|
239 |
# TODO: Check language ?????
|
240 |
return mf.translations.all()[0].caption, mf.get_absolute_url() |
241 |
|
242 |
|
243 |
def add_or_update_faq_category(slug, name): |
244 |
logger.info("Create or update faq subcategory, %s, %s", slug,
|
245 |
name) |
246 |
try:
|
247 |
category = FaqCategory.objects.get(translations__slug=slug) |
248 |
logger.info("FAQ category found")
|
249 |
except FaqCategory.DoesNotExist:
|
250 |
logger.info("FAQ category not found, creating...")
|
251 |
category = FaqCategory() |
252 |
category.save() |
253 |
category.translations.create(slug=slug, title=name) |
254 |
|
255 |
return category
|
256 |
|
257 |
|
258 |
def add_or_update_faq_question(author, service, category, slug,\ |
259 |
title, html_content): |
260 |
logger.info("Create or update faq question, %s, %s, %s, %s", service,
|
261 |
category, slug, title) |
262 |
|
263 |
try:
|
264 |
q = Question.objects.get(slug=slug) |
265 |
logger.info("Question found, updating...")
|
266 |
except:
|
267 |
logger.info("Question not found, creating...")
|
268 |
q = Question() |
269 |
|
270 |
q.author = author |
271 |
q.is_active = True
|
272 |
q.category = category |
273 |
q.service = service |
274 |
q.slug = slug |
275 |
q.title = title |
276 |
q.save() |
277 |
q.application = [Application.current()] |
278 |
q.save() |
279 |
|
280 |
RawContentModel = Question.content_type_for(RawContent) |
281 |
try:
|
282 |
content = q.rawcontent_set.filter()[0]
|
283 |
except:
|
284 |
content = q.rawcontent_set.create(region=REGIONS_MAP['faq'])
|
285 |
|
286 |
content.text = html_content |
287 |
content.save() |
288 |
|
289 |
return q
|
290 |
|
291 |
def add_or_update_guide_entry(author, service, slug, title, html_content): |
292 |
logger.info("Create or update user guide entry, %s, %s, %s", service,
|
293 |
slug, title) |
294 |
try:
|
295 |
logger.info("Guide entry found, updating...")
|
296 |
guide = UserGuideEntry.objects.get(slug=slug) |
297 |
except:
|
298 |
logger.info("Guide entry not found, creating...")
|
299 |
guide = UserGuideEntry() |
300 |
|
301 |
guide.author = author |
302 |
guide.is_active = True
|
303 |
guide.service = service |
304 |
guide.slug = slug |
305 |
guide.title = title |
306 |
guide.save() |
307 |
|
308 |
RawContentModel = UserGuideEntry.content_type_for(RawContent) |
309 |
try:
|
310 |
content = guide.rawcontent_set.filter()[0]
|
311 |
except:
|
312 |
content = guide.rawcontent_set.create(region=REGIONS_MAP['userguide'])
|
313 |
|
314 |
content.text = html_content |
315 |
content.save() |
316 |
|
317 |
return guide
|
318 |
|
319 |
|