def clean_file(self): """File cleaning, validates it using Page.valid_xml()""" file = self.cleaned_data['file'] try: Page.valid_xml(file.read()) except forms.ValidationError, e: raise e
def migrate(cls, template, tipocms_page): """Migrates TipoCMS pages to CMS. This method can be removed once all TipoCMS pages are migrated.""" from tcms.models import Path, Page, Value, LIVE from tipocms.models import CmsTextContent, CmsImageContent http_path = tipocms_page.http_path.strip() if not http_path.startswith('/'): # avoid invalid pages return if not http_path.endswith('/'): http_path += '/' http_path = cls.fix_path_hacks(http_path) path, created = Path.objects.get_or_create(path=http_path, locale=tipocms_page.locale) if not created: # path already, check page entry if Page.objects.filter(path=path, state=LIVE).count() > 0: return http_path = cls.fix_path_hacks(http_path) path, created = Path.objects.get_or_create(path=http_path, locale=tipocms_page.locale) if not created: # path already, check page entry if Page.objects.filter(path=path, state=LIVE).count() > 0: return page = Page(path=path, template=template, description=tipocms_page.description) # add meta data for meta in tipocms_page.cmspagemetacontent_set.all(): if meta.name == 'title': page.meta_title = meta.text elif meta.name == 'description': page.meta_description = meta.text elif meta.name == 'keywords': page.meta_keywords = meta.text page.save() # save new page def try_save(name, value): for matcher, new_name, Type in cls.migration_names: match = re.match(r'^%s$' % matcher, name) if match: groups = match.groups() if groups: new_name = new_name % groups # try to get method to post-process migration value # from template instance migrate = getattr(cls, '%s_migrate' % Type.name(), lambda p, v: v) page.values.add(Value(name=new_name, type=Type.name(), value=migrate(page, value))) break # save values for f in tipocms_page.cmsfragment_set.select_related('template').all(): # avoid empty fragments if f.template.template_path == 'cms/fragment/empty.html': continue names = [] for c in f.cmscontentmapping_set.all(): obj = c.object if isinstance(obj, (CmsTextContent, CmsImageContent)): name = mkbasename(f.name, c.name) if name in names: # do not process repeated values continue names.append(name) try_save(name, obj.value()) # try to save images alt/credits values by matching # value name adding /alt and /credits endings if isinstance(obj, CmsImageContent): alt, credits = obj.alt, obj.credits if alt: try_save(mkbasename(name, 'alt'), alt.strip()) if credits: try_save(mkbasename(name, 'credits'), credits.strip()) page.publish() return page
def save(self): return Page.from_xml(self.cleaned_data['file'].read())