Browse Source

Fetch legal pages from API

Rafał Pitoń 10 years ago
parent
commit
b936d9a55f

+ 2 - 0
misago/conf/defaults.py

@@ -149,6 +149,7 @@ INSTALLED_APPS = (
     'crispy_forms',
     'mptt',
     'rest_framework',
+    'corsheaders',
     'misago.admin',
     'misago.acl',
     'misago.core',
@@ -166,6 +167,7 @@ MIDDLEWARE_CLASSES = (
     'misago.users.middleware.AvatarServerMiddleware',
     'misago.users.middleware.RealIPMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',
+    'corsheaders.middleware.CorsMiddleware',
     'django.middleware.common.CommonMiddleware',
     'django.middleware.csrf.CsrfViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',

+ 8 - 0
misago/emberapp/app/models/legal-page.js

@@ -0,0 +1,8 @@
+import DS from 'ember-data';
+
+export default DS.Model.extend({
+  slug: DS.attr('slug'),
+  title: DS.attr('string'),
+  link: DS.attr('string'),
+  body: DS.attr('string')
+});

+ 4 - 0
misago/emberapp/app/routes/privacy-policy.js

@@ -1,6 +1,10 @@
 import Ember from 'ember';
 
 export default Ember.Route.extend({
+  model: function() {
+    return this.store.find('legal-page', 'privacy-policy');
+  },
+
   actions: {
     didTransition: function() {
       this.send("setTitle", this.get('settings.privacy_policy_title') || gettext("Privacy policy"));

+ 4 - 0
misago/emberapp/app/routes/terms-of-service.js

@@ -1,6 +1,10 @@
 import Ember from 'ember';
 
 export default Ember.Route.extend({
+  model: function() {
+    return this.store.find('legal-page', 'terms-of-service');
+  },
+
   actions: {
     didTransition: function() {
       this.send("setTitle", this.get('settings.terms_of_service_title') || gettext("Terms of service"));

+ 9 - 1
misago/emberapp/app/templates/privacy-policy.hbs

@@ -1 +1,9 @@
-{{outlet}}
+<div class="page-header">
+  <div class="container">
+    <h1>{{title}}</h1>
+  </div>
+</div>
+
+<div class="container">
+  {{{body}}}
+</div>

+ 9 - 1
misago/emberapp/app/templates/terms-of-service.hbs

@@ -1 +1,9 @@
-{{outlet}}
+<div class="page-header">
+  <div class="container">
+    <h1>{{title}}</h1>
+  </div>
+</div>
+
+<div class="container">
+  {{{body}}}
+</div>

+ 6 - 3
misago/emberapp/config/environment.js

@@ -5,8 +5,7 @@ module.exports = function(environment) {
     modulePrefix: 'misago',
     environment: environment,
     baseURL: '/',
-    locationType: 'trailing-history',
-    rootElement: '#main',
+    locationType: 'auto',
     EmberENV: {
       FEATURES: {
         // Here you can enable experimental features on an ember canary build
@@ -17,6 +16,7 @@ module.exports = function(environment) {
     APP: {
       // Here you can pass flags/options to your application instance
       // when it is created
+      rootElement: '#main'
     }
   };
 
@@ -31,11 +31,14 @@ module.exports = function(environment) {
       'default-src': "'none'",
       'script-src': "'self' 'unsafe-inline' https://cdn.mxpnl.com 127.0.0.1:8000", // Allow scripts from https://cdn.mxpnl.com and Django runserver
       'font-src': "'self' http://fonts.gstatic.com", // Allow fonts to be loaded from http://fonts.gstatic.com
-      'connect-src': "'self' https://api.mixpanel.com http://custom-api.local", // Allow data (ajax/websocket) from api.mixpanel.com and custom-api.local
+      'connect-src': "'self' https://api.mixpanel.com http://localhost:8000", // Allow data (ajax/websocket) from api.mixpanel.com and custom-api.local
       'img-src': "'self'",
       'style-src': "'self' 'unsafe-inline' http://fonts.googleapis.com", // Allow inline styles and loaded CSS from http://fonts.googleapis.com
       'media-src': "'self'"
     }
+
+    ENV.APP.API_HOST = 'http://localhost:8000';
+    ENV.APP.API_NAMESPACE = 'api';
   }
 
   if (environment === 'test') {

+ 15 - 0
misago/emberapp/tests/unit/models/legal-page-test.js

@@ -0,0 +1,15 @@
+import {
+  moduleForModel,
+  test
+} from 'ember-qunit';
+
+moduleForModel('legal-page', 'LegalPage', {
+  // Specify the other units that are required for this test.
+  needs: []
+});
+
+test('it exists', function() {
+  var model = this.subject();
+  // var store = this.store();
+  ok(!!model);
+});

+ 1 - 1
misago/legal/urls.py → misago/legal/urls/__init__.py

@@ -2,6 +2,6 @@ from django.conf.urls import patterns, url
 
 
 urlpatterns = patterns('misago.legal.views',
-    url(r'^terms-of-service/$', 'terms', name='terms_of_service'),
+    url(r'^terms-of-service/$', 'terms_of_service', name='terms_of_service'),
     url(r'^privacy-policy/$', 'privacy_policy', name='privacy_policy'),
 )

+ 7 - 0
misago/legal/urls/api.py

@@ -0,0 +1,7 @@
+from django.conf.urls import patterns, url
+
+
+urlpatterns = patterns('misago.legal.views',
+    url(r'^(?P<page>[\w\d-]+)/$', 'legal_page'),
+)
+

+ 41 - 8
misago/legal/views.py

@@ -4,6 +4,9 @@ from django.http import Http404
 from django.shortcuts import redirect, render
 from django.utils.translation import gettext as _
 
+from rest_framework.decorators import api_view
+from rest_framework.response import Response
+
 from misago.conf import settings
 from misago.core.cache import cache
 from misago.markup import common_flavour
@@ -30,29 +33,59 @@ def get_parsed_content(request, setting_name):
         return cached_content['parsed']
 
 
-def terms(request):
+def terms_of_service(request, return_dict=False):
     if not (settings.terms_of_service or settings.terms_of_service_link):
         raise Http404()
 
-    if settings.terms_of_service_link:
+    if not return_dict and settings.terms_of_service_link:
         return redirect(settings.terms_of_service_link)
 
     parsed_content = get_parsed_content(request, 'terms_of_service')
-    return render(request, 'misago/terms_of_service.html', {
+    response_dict = {
         'title': settings.terms_of_service_title or _("Terms of service"),
+        'link': settings.terms_of_service_link,
         'content': parsed_content,
-    })
+    }
+
+    if return_dict:
+        return response_dict
+    else:
+        return render(request, 'misago/terms_of_service.html', response_dict)
 
 
-def privacy_policy(request):
+def privacy_policy(request, return_dict=False):
     if not (settings.privacy_policy or settings.privacy_policy_link):
         raise Http404()
 
-    if settings.privacy_policy_link:
+    if not return_dict and settings.privacy_policy_link:
         return redirect(settings.privacy_policy_link)
 
     parsed_content = get_parsed_content(request, 'privacy_policy')
-    return render(request, 'misago/privacy_policy.html', {
+    response_dict = {
         'title': settings.privacy_policy_title or _("Privacy policy"),
+        'link': settings.privacy_policy_link,
         'content': parsed_content,
-    })
+    }
+
+    if return_dict:
+        return response_dict
+    else:
+        return render(request, 'misago/privacy_policy.html', response_dict)
+
+
+API_PAGES = {
+    'terms-of-service': terms_of_service,
+    'privacy-policy': privacy_policy,
+}
+
+@api_view(['GET'])
+def legal_page(request, page):
+    if page not in API_PAGES:
+        raise Http404(_("Requested page could not be found."))
+
+    page_dict = API_PAGES.get(page)(request, True)
+    page_dict['id'] = page
+    page_dict['body'] = page_dict['content']
+    del page_dict['content']
+
+    return Response(page_dict)

+ 5 - 0
misago/project_template/project_name/settings.py

@@ -31,6 +31,11 @@ MISAGO_JS_DEBUG = DEBUG
 
 ALLOWED_HOSTS = []
 
+# Cross-Origin Resource Sharing policy
+
+CORS_ORIGIN_ALLOW_ALL = DEBUG
+CORS_ORIGIN_WHITELIST = ()
+
 
 # Database
 # https://docs.djangoproject.com/en/{{ docs_version }}/ref/settings/#databases

+ 1 - 0
misago/project_template/requirements.txt

@@ -3,6 +3,7 @@ djangorestframework==3.0.2
 beautifulsoup4==4.3.2
 bleach==1.4.1
 django-debug-toolbar==1.2.1
+django-cors-headers==1.0.0
 django-crispy-forms==1.4.0
 django-htmlmin==0.7
 django-mptt==0.6.1

+ 10 - 0
misago/urls.py

@@ -22,6 +22,16 @@ urlpatterns += patterns('',
     url(r'^ui-server/$', 'misago.core.uiviews.uiserver', name="ui_server"),
 )
 
+# Register API
+api_patterns = patterns('',
+    url(r'^$', 'misago.core.views.forum_index', name='index'),
+    url(r'^legal-pages/', include('misago.legal.urls.api')),
+)
+
+urlpatterns += patterns('',
+    url(r'^api/', include(api_patterns, namespace='api')),
+)
+
 
 # Register Misago ACP
 if settings.MISAGO_ADMIN_PATH: