Upgrade to Pro — share decks privately, control downloads, hide ads and more …

django CMS + Channels + DRF = ♥

django CMS + Channels + DRF = ♥

Given at PyCon Uk 2016 17 September 2016

Video: https://www.youtube.com/watch?v=IW7mi5BIN6U

Iacopo Spalletti

September 17, 2016
Tweet

More Decks by Iacopo Spalletti

Other Decks in Programming

Transcript

  1. HELLO, I AM IACOPO Founder and CTO @NephilaIt django CMS

    core developer @yakkys https://github.com/yakky
  2. SPA ARE COMMONPLACE user interacts with the browser-side interface layer

    interface reads/writes data from a backend outcome is shown seamlessly
  3. WHAT IF YOU MIX SPA AND CMS? Desktop notifications Real

    time content updates Frictionless interaction
  4. I focused on this topics in the past months Released

    some code as a result I'm going to show you my findings
  5. WHAT'S DJANGO CMS One of the mostly widely used CMS

    for Django Very simple frontend content editing Template-based page types Load of features available for integration in third- party apps
  6. WHAT'S CHANNELS Websockets for django \o/ (but it's actually more)

    At most-once queue system Bring async to sync django code
  7. DESKTOP NOTIFICATIONS! @receiver(post_publish) def notify_page_publish(**kwargs): page = kwargs.get('instance') language =

    kwargs.get('language') knock = { 'title': 'Page update', 'message': page.get_title(language), 'icon': '/static/img/favicon.ico', 'url': page.get_absolute_url(language), 'language': language } group = Group('knocker-{0}'.format(knock['language'])) group.send({'text': json.dumps(knock)})
  8. DESKTOP NOTIFICATIONS! BEHIND THE SCENES clients are added to a

    per-language group as they connect simple js read messages from websocket and display as notifications
  9. DESKTOP NOTIFICATIONS! @channel_session def ws_connect(message): prefix, language = message['path'].strip('/').split('/') gr

    = Group('knocker-{0}'.format(language)) gr.add(message.reply_channel) message.channel_session['knocker'] = language
  10. DESKTOP NOTIFICATIONS! notifications.onmessage = function (message) { var data =

    JSON.parse(message.data); data = { body: notification.message, icon: notification.icon, tag: 'notifications_' + notification.language, url: notification.url }; var note = new Notification(notification.title, data); note.onclick = function () { document.location = notification.url; }};
  11. LIVEBLOG! def save(self, *args, **kwargs): super(Liveblog, self).save(*args, **kwargs) notification =

    { 'id': self.pk, 'content': self.render(), 'creation_date': self.creation_date, 'changed_date': self.changed_date, } Group(self.liveblog_group).send({ 'text': json.dumps(notification), })
  12. LIVEBLOG! var data = JSON.parse(message.data); var node = document.querySelectorAll( "div[data-post-id*='"

    + data.id + "']" ); var item = document.createElement('div'); item.innerHTML = data.content; if (node.length > 0) { node[0].parentNode.replaceChild(item.children[0], node[0]); } else { document.getElementById("liveblog-posts").insertBefore( item.children[0], document.getElementById( "liveblog-posts").children[0]); }
  13. DJANGO CMS + DRF LET'S GO ONE STEP BEYOND The

    goal: Providing a unified API for web, SPAs, applications Integrating content in non-web environment Preserving the ease of use of a CMS
  14. DJANGO CMS + DRF VIEWSETS : full pages list providing

    pages metadata : full dump + rendered placeholders content + sekizai context : menu structure pages/ pages/<page-id>/ pages/menu
  15. DJANGO CMS + DRF SERIALIZERS Currently very dumb Just map

    the model attributes Provide rendered HTML for each page placeholder (really dumb) Discover sekizai resources in plugins
  16. DJANGO CMS + DRF NEAT FEATURES Provides the sekizai context

    ⬇ Client load JS/CSS on demand Provides the page template name ⬇ Client can use a custom template for each page
  17. DJANGO CMS + DRF CUSTOM TEMPLATES <div class="rest-header" ng-bind-html="content_page.placeholders.header |

    <article class="body"> <div ng-bind-html="content_page.placeholders.content | safe"></div> </article>
  18. DJANGO CMS + DRF SCOPE Very limited now Does not

    map every django CMS feature: form submission dynamic apphooks (have to implement custom DRF for external apps) Used by a few projects in production
  19. DJANGO CMS + DRF TAKE-HOME Still road to go Major

    hurdle: sensible and simple plugin serialization Benefits are huge Stay tuned!
  20. CHANNELS Channels is maturing quickly Easy integration ➡ tons of

    new applications Easily fits in CMS flow ➡ great potential to invent new things
  21. DRF by 2016 - complete experimental phase from 2017 -

    let's build REST-powered django CMS platforms