示例#1
0
文件: models.py 项目: mammique/opst
    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()
示例#2
0
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)
示例#3
0
文件: api.py 项目: yakky/pycon_site
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)