Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from puput import PUPUT_APPS
WAGTAIL_SITE_NAME = 'Puput blog'
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = 'changemepliz'
DEBUG = True
ALLOWED_HOSTS = "*"
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
INSTALLED_APPS += PUPUT_APPS
MIDDLEWARE = (
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'wagtail.core.middleware.SiteMiddleware',
'wagtail.contrib.redirects.middleware.RedirectMiddleware',
)
ROOT_URLCONF = 'tests.testapp.urls'
TEMPLATES = [
def get_by_path(self, blog_path):
# Look for the blog checking all the path
from .models import BlogPage
blogs = BlogPage.objects.filter(slug=blog_path.split("/")[-1])
for blog in blogs:
if strip_prefix_and_ending_slash(blog.specific.last_url_part) == blog_path:
return blog.specific
return
if not request.site:
raise Http404
if request.resolver_match.url_name == 'entry_page_serve_slug':
# Splitting the request path and obtaining the path_components
# this way allows you to place the blog at the level you want on
# your sitemap.
# Example:
# splited_path = ['es', 'blog', '2016', '06', '23', 'blog-entry']
# slicing this way you obtain:
# path_components = ['es', 'blog', 'blog-entry']
# with the oldest solution you'll get ['es', 'blog-entry']
# and a 404 will be raised
splited_path = strip_prefix_and_ending_slash(request.path).split("/")
path_components = splited_path[:-4] + splited_path[-1:]
else:
path_components = [strip_prefix_and_ending_slash(request.path).split('/')[-1]]
page, args, kwargs = request.site.root_page.specific.route(request, path_components)
for fn in hooks.get_hooks('before_serve_page'):
result = fn(page, request, args, kwargs)
if isinstance(result, HttpResponse):
return result
return page.serve(request, *args, **kwargs)
def get_by_path(self, blog_path):
# Look for the blog checking all the path
from .models import BlogPage
blogs = BlogPage.objects.filter(slug=blog_path.split("/")[-1])
for blog in blogs:
if strip_prefix_and_ending_slash(blog.specific.last_url_part) == blog_path:
return blog.specific
return
def get_context(self, request, *args, **kwargs):
context = super(EntryPage, self).get_context(request, *args, **kwargs)
context['blog_page'] = self.blog_page
return context
body = StreamField(MyStreamBlock(), blank=True)
content_panels = Page.content_panels + [
StreamFieldPanel('body'),
]
class LeftSidebarPage(MenuPage):
body = StreamField(MyStreamBlock(), blank=True)
content_panels = Page.content_panels + [
StreamFieldPanel('body'),
]
class StreamFieldEntryPage(EntryPage):
streamfield = StreamField(MyStreamBlock(), blank=True)
content_panels = EntryPage.content_panels + [
StreamFieldPanel('streamfield')
]
BlogPage.subpage_types.append(StreamFieldEntryPage)
# Let everyone know when a new page is published
def send_to_discord(sender, **kwargs):
instance = kwargs['instance']
# Only post new post. No updates.
if instance.first_published_at != instance.last_published_at:
return
if settings.DEBUG:
return
def post(self, request, entry_page_id, *args, **kwargs):
try:
entry_page = EntryPage.objects.get(pk=entry_page_id)
blog_page = entry_page.blog_page
num_comments = 0
if blog_page.disqus_api_secret:
num_comments = get_num_comments_with_disqus(blog_page, entry_page)
entry_page.num_comments = num_comments
entry_page.save()
return HttpResponse()
except EntryPage.DoesNotExist:
raise Http404
content = content.decode('UTF-8')
excerpt = strip_tags(item_node.find(u'{{{0:s}excerpt/}}encoded'.format(self.WP_NS)).text or '')
if not excerpt and content:
excerpt = Truncator(content).words(50)
slug = slugify(title)[:255] or u'post-{0:s}'.format(item_node.find(u'{{{0:s}}}post_id'.format(self.WP_NS)).text)
creator = item_node.find('{http://purl.org/dc/elements/1.1/}creator').text
try:
entry_date = datetime.strptime(item_node.find(u'{{{0:s}}}post_date_gmt'.format(self.WP_NS)).text,
'%Y-%m-%d %H:%M:%S')
except ValueError:
entry_date = datetime.strptime(item_node.find(u'{{{0:s}}}post_date'.format(self.WP_NS)).text,
'%Y-%m-%d %H:%M:%S')
# Create page
try:
page = EntryPage.objects.get(slug=slug)
except EntryPage.DoesNotExist:
page = EntryPage(
title=title,
body=content,
excerpt=strip_tags(excerpt),
slug=slug,
go_live_at=entry_date,
first_published_at=creation_date,
date=creation_date,
owner=self.authors.get(creator),
seo_title=title,
search_description=excerpt,
live=item_node.find(u'{{{0:s}}}status'.format(self.WP_NS)).text == 'publish')
self.blogpage.add_child(instance=page)
revision = self.blogpage.save_revision()
revision.publish()
self.import_entry_tags(item_node.findall('category'), page)
)
# I tryed to convert excerpt to markdown using tomd without success
values = {
"content": "Breaking news on OSR website !",
"embeds": [{
"title": instance.title,
"url": "https://openstudyroom.org" + instance.url,
"description": excerpt,
}]
}
r = requests.post(discord_url, json=values)
r.raise_for_status()
# Register two receivers
page_published.connect(send_to_discord, sender=EntryPage)
page_published.connect(send_to_discord, sender=StreamFieldEntryPage)
@receiver(post_save, sender=ForumPost)
def forum_post_to_discord(sender, instance, **kwargs):
# don't announce edits
instance.refresh_from_db()
if instance.updates_count > 0:
return
# don't announce private admins forums forum.
# This should be properly handled with a test if anonymous user can read forum.
parent_id = instance.topic.forum.parent
if parent_id is not None and parent_id.pk == 12:
return
if settings.DEBUG:
return
def post(self, request, entry_page_id, *args, **kwargs):
try:
entry_page = EntryPage.objects.get(pk=entry_page_id)
blog_page = entry_page.blog_page
num_comments = 0
if blog_page.disqus_api_secret:
num_comments = get_num_comments_with_disqus(blog_page, entry_page)
entry_page.num_comments = num_comments
entry_page.save()
return HttpResponse()
except EntryPage.DoesNotExist:
raise Http404