def update(self): import feedparser from time import mktime from datetime import datetime from slugify import slugify from django.contrib.sites.models import Site from cms.models import Page, Title from cms.plugins.text.models import Text try: p_last = self.pages.latest('publication_date') except Page.DoesNotExist: p_last = None try: for e in feedparser.parse(self.url)['entries']: date = e.get('published_parsed') title = e.get('title') body = e.get('summary') url = e.get('link') if date and title and body: date = datetime.fromtimestamp(mktime(date)) if p_last and date <= p_last.publication_date: continue p=Page(site=Site.objects.all()[0], in_navigation=False, published=True, template='page-full.html') p.publication_date = date if self.parent_page: p.parent = self.parent_page p.save() self.pages.add(p) self.save() t=Title(language='en', title=title, slug='%s-%s' % (slugify(title), p.pk), page=p) t.save() pl=p.placeholders.get(slot='page') if url: body = u'%s<p><a href="%s">Lire la suite de l\'article…</a></p>' % (body, url) txt=Text(body=body, language='en', plugin_type='TextPlugin') txt.save() pl.cmsplugin_set.add(txt) pl.save() except: pass self.update_last = datetime.now() self.save()
def copy_pages(): pages = PagesPage.objects.order_by('tree_id', 'lft', ) # remove all the previous pages CMSPage.objects.all().delete() parents = {} for page in pages: cmspage = CMSPage() # replicate the language independent structure information # thanks to the common ancestor we can reuse exactly the same tree # information cmspage.pk = page.pk cmspage.creation_date = page.creation_date cmspage.publication_date = page.publication_date cmspage.publication_end_date = page.publication_end_date cmspage.changed_date = page.last_modification_date cmspage.template = 'django_' + page.template cmspage.level = page.level cmspage.lft = page.lft cmspage.rght = page.rght cmspage.tree_id = page.tree_id cmspage.site_id = 1 if page.parent_id in parents: cmspage.parent = parents[page.parent_id] cmspage.save(no_signals=True) cmspage.rescan_placeholders() parents[cmspage.pk] = cmspage # replicate the language dependent data for language in page.get_languages(): cmstitle = Title() cmstitle.language = language cmstitle.title = page.title(language) cmstitle.slug = page.slug(language) cmstitle.page = cmspage cmstitle.update_path() cmstitle.save() contents = page.content_by_language(language) for content in contents: if content.type in ('subtitle', 'text', 'right-column'): placeholder = cmspage.placeholders.get(slot=content.type) # For the main placeholders we need to scan whether content # is markdown or plain HTML # This is a very naif check, but does the trick in this # context if content.body.find('<p>') > -1: add_plugin(placeholder, plugin_type='TextPlugin', language=language, body=content.body) else: add_plugin(placeholder, plugin_type='MarkItUpPlugin', language=language, body=content.body) elif content.type not in ('slug', 'title'): # Fallback import placeholder = cmspage.placeholders.get(slot=content.type) add_plugin(placeholder, plugin_type='TextPlugin', language=language, body=content.body)
def create(request): if not request.user.is_admin_in_forening(request.active_forening): return render(request, 'central/admin/sites/create_disallowed.html') available_site_types = [] for t in Site.TYPE_CHOICES: if t[0] == 'mal': if not request.user.has_perm('sherpa/admin'): continue available_site_types.append(t) site_templates = Site.objects.filter( forening=Forening.DNT_CENTRAL_ID, type='mal', ).order_by('title') context = { 'available_site_types': available_site_types, 'site_templates': site_templates, 'template_types': Site.TEMPLATE_TYPE_CHOICES, } if request.method == 'GET': context['form'] = CreateSiteForm(request.user, auto_id='%s') return render(request, 'central/admin/sites/create.html', context) elif request.method == 'POST': form = CreateSiteForm(request.user, request.POST, auto_id='%s') context['form'] = form if not form.is_valid(): return render(request, 'central/admin/sites/create.html', context) if not request.POST['domain-type'] in ['fqdn', 'subdomain']: raise PermissionDenied site_forening = form.cleaned_data['forening'] type = form.cleaned_data['type'] title = form.cleaned_data['title'] template_main = form.cleaned_data['template_main'] template_type = form.cleaned_data['template_type'] template_description = form.cleaned_data['template_description'] domain = form.cleaned_data['domain'] site = Site( domain=domain, type=type, forening=site_forening, title=title, template_main=template_main, template_type=template_type, template_description=template_description, ) site.save() # If this is a main template, clear other templates of this type in case any of them were previous main if site.type == 'mal' and site.template_main: Site.objects.filter( type=site.type, template_type=site.template_type, ).exclude( id=site.id, ).update( template_main=False ) # Invalidate the forening's homepage site cache cache.delete('forening.homepage_site.%s' % site_forening.id) if 'use-template' not in request.POST: # User explicitly requested not to clone any template, just create a front page page = Page( site=site, title='Forside', published=True, created_by=request.user, created_date=datetime.now(), ) page.save() version = Version( page=page, version=1, owner=request.user, ) version.save() elif request.POST.get('template', '').strip() == '': # Sherpa-admin error; a site-template for the chosen site type doesn't exist! # This needs to be fixed. logger.error( "Sherpa-bruker opprettet en site med en mal som ikke finnes", extra={ 'request': request, 'missing_template_type': request.POST.get('missing-template-type', '<unknown>'), } ) site.delete() messages.error(request, 'invalid_template') return render(request, 'central/admin/sites/create.html', context) else: # All right, let's clone the entire template site # Note that for most objects, we'll just set the primary key to None, change the site field to the # new site, and save it, which will insert a new object. # For related fields, we'll need to save the related set in memory before saving the new object, so # that we can iterate it, clone them and re-relate them to the new object # Additionally, replace domain name references in content from the template-domain to the new one template_site = Site.get_cached_by_id(request.POST['template']) assert template_site.type == 'mal' # Menus for menu in Menu.objects.filter(site=template_site): menu.id = None menu.site = site # Replace domain references with the new site domain menu.url = re.sub(template_site.domain, site.domain, menu.url) menu.save() # Pages # Used to map old IDs to their new corresponding pages. This is needed because the parent reference can't # be duplicated, it needs to be updated with its corresponding new parent page_id_mapping = {} # Order by the left value in order to insert new nodes in a consistent way for page in Page.objects.filter(site=template_site).order_by('lft'): versions = page.versions.all() old_id = page.id page.id = None page.site = site # Reset MPTT state and let our library recreate them page.tree_id = None page.lft = None page.rght = None page.level = None # Set parent to the corresponding clone; use the old parent id to retrieve it if page.parent is not None: page.parent = page_id_mapping[page.parent.id] # Change creation to the user creating the new site and reset modification page.created_by = request.user page.created_date = datetime.now() page.modified_by = None page.modified_date = None # Insert the new node appropriately if page.parent is None: Page.objects.insert_node(page, target=None, save=True) else: Page.objects.insert_node(page, target=page.parent, position='last-child', save=True) page.save() # Remember which old id maps to this page page_id_mapping[old_id] = page for version in versions: rows = version.rows.all() version.id = None version.page = page version.save() for row in rows: columns = row.columns.all() row.id = None row.version = version row.save() for column in columns: contents = column.contents.all() column.id = None column.row = row column.save() for content in contents: content.id = None content.column = column # Replace domain references with the new site domain # Use a json dump with prepended/trailing quotes stripped to ensure the # replacement string is properly escaped if inserted into json-formatted content json_safe_domain = json.dumps(site.domain)[1:-1] content.content = re.sub(template_site.domain, json_safe_domain, content.content) # For aktiviteteslisting-widgets, force arranger-filter to the new site's related # forening if content.type == 'widget': parsed_content = json.loads(content.content) if parsed_content['widget'] == 'aktivitet_listing': # Note that the list of ids contains strings, because we forgot to convert # it to int in the widget-editor save logic, but that's not a problem since # the filter lookup will implicitly convert it. So force it to str to be # consistent parsed_content['foreninger'] = [str(site.forening.id)] content.content = json.dumps(parsed_content) content.save() # Articles for article in Article.objects.filter(site=template_site): versions = article.versions.all() article.id = None article.site = site # Change creation to the user creating the new site and reset modification article.created_by = request.user article.created_date = datetime.now() article.modified_by = None article.modified_date = None article.save() for version in versions: rows = version.rows.all() version.id = None version.article = article version.save() for row in rows: columns = row.columns.all() row.id = None row.version = version row.save() for column in columns: contents = column.contents.all() column.id = None column.row = row column.save() for content in contents: content.id = None content.column = column # Replace domain references with the new site domain # Use a json dump with prepended/trailing quotes stripped to ensure the # replacement string is properly escaped if inserted into json-formatted content json_safe_domain = json.dumps(site.domain)[1:-1] content.content = re.sub(template_site.domain, json_safe_domain, content.content) # For aktiviteteslisting-widgets, force arranger-filter to the new site's related # forening if content.type == 'widget': parsed_content = json.loads(content.content) if parsed_content['widget'] == 'aktivitet_listing': # Note that the list of ids contains strings, because we forgot to convert # it to int in the widget-editor save logic, but that's not a problem since # the filter lookup will implicitly convert it. So force it to str to be # consistent parsed_content['foreninger'] = [str(site.forening.id)] content.content = json.dumps(parsed_content) content.save() # Campaigns for campaign in Campaign.objects.filter(site=template_site): campaign_texts = campaign.text.all() campaign.id = None campaign.site = site # Replace domain references with the new site domain campaign.button_anchor = re.sub(template_site.domain, site.domain, campaign.button_anchor) campaign.save() for campaign_text in campaign_texts: campaign_text.id = None campaign_text.campaign = campaign campaign_text.save() # Add default language menu options LanguageMenuOption.objects.create( site=site, label="English", destination="https://english.dnt.no/", sort=1 ) LanguageMenuOption.objects.create( site=site, label="Deutsch", destination="https://deutsch.dnt.no/", sort=2 ) LanguageMenuOption.objects.create( site=site, label="Français", destination="https://www.dnt.no/francais/", sort=3 ) request.session.modified = True messages.info(request, 'site_created') return redirect('admin:sites.show', site.id)
def copy_pages(): pages = PagesPage.objects.order_by( 'tree_id', 'lft', ) # remove all the previous pages CMSPage.objects.all().delete() parents = {} for page in pages: cmspage = CMSPage() # replicate the language independent structure information # thanks to the common ancestor we can reuse exactly the same tree # information cmspage.pk = page.pk cmspage.creation_date = page.creation_date cmspage.publication_date = page.publication_date cmspage.publication_end_date = page.publication_end_date cmspage.changed_date = page.last_modification_date cmspage.template = 'django_' + page.template cmspage.level = page.level cmspage.lft = page.lft cmspage.rght = page.rght cmspage.tree_id = page.tree_id cmspage.site_id = 1 if page.parent_id in parents: cmspage.parent = parents[page.parent_id] cmspage.save(no_signals=True) cmspage.rescan_placeholders() parents[cmspage.pk] = cmspage # replicate the language dependent data for language in page.get_languages(): cmstitle = Title() cmstitle.language = language cmstitle.title = page.title(language) cmstitle.slug = page.slug(language) cmstitle.page = cmspage cmstitle.update_path() cmstitle.save() contents = page.content_by_language(language) for content in contents: if content.type in ('subtitle', 'text', 'right-column'): placeholder = cmspage.placeholders.get(slot=content.type) # For the main placeholders we need to scan whether content # is markdown or plain HTML # This is a very naif check, but does the trick in this # context if content.body.find('<p>') > -1: add_plugin(placeholder, plugin_type='TextPlugin', language=language, body=content.body) else: add_plugin(placeholder, plugin_type='MarkItUpPlugin', language=language, body=content.body) elif content.type not in ('slug', 'title'): # Fallback import placeholder = cmspage.placeholders.get(slot=content.type) add_plugin(placeholder, plugin_type='TextPlugin', language=language, body=content.body)