]> ToastFreeware Gitweb - philipp/winterrodeln/wradmin.git/blobdiff - wradmin/controllers/rodelbahn.py
Rely on wrpylib to convert JSON to sledrun.
[philipp/winterrodeln/wradmin.git] / wradmin / controllers / rodelbahn.py
index a4c2a6404a0e99ac98b3276921ee42e2ced594cb..db33cd2a7b3525ac39a5bda8bd58ea0c54103a21 100644 (file)
@@ -1,8 +1,12 @@
-from urllib.parse import quote_plus
+import json
+import subprocess
+import urllib.request, urllib.error
+from collections import OrderedDict
+from typing import Optional, Tuple, List
 
 import paginate
 import sqlalchemy as sa
-from flask import request, abort, redirect, url_for, flash, render_template, current_app
+from flask import request, abort, redirect, url_for, flash, render_template
 
 import wrpylib.mwmarkup
 import wrpylib.wrmwcache
@@ -10,6 +14,9 @@ import wrpylib.wrmwmarkup
 from wradmin.app import db
 from wradmin.model import WrSledrunCache, WrReport
 from wradmin.template_helper import PylonsHelper
+from wrpylib.wrvalidators import rodelbahnbox_to_str, LonLat, difficulty_german_from_str, \
+    avalanches_german_from_str, public_transport_german_from_str, opt_tristate_german_comment_from_str, \
+    nightlightdays_from_str
 
 
 class RodelbahnController:
@@ -36,14 +43,39 @@ class RodelbahnController:
                                   items_per_page=25)
         return render_template('rodelbahn_view.html', sledding=sledrun, paginator=paginator)
 
+    def view_wikitext(self, id: int) -> str:
+        """Displays a sled run as MediaWiki wiki text"""
+        q = db.session.query(WrSledrunCache)
+        sledrun = q.get(id)
+        if sledrun is None:
+            abort(404)
+        h = PylonsHelper()
+        sledrun_json_url = h.sledrun_json_url(sledrun.page_title)
+        sledrun_json_page = urllib.request.urlopen(sledrun_json_url)  # might raise urllib.error.HTTPError
+        sledrun_json_str = sledrun_json_page.read()
+        sledrun_json = json.loads(sledrun_json_str)
+
+        map_json = None
+        map_url = h.sledrun_map_url(sledrun.page_title)
+        try:
+            map_page = urllib.request.urlopen(map_url)
+            map_str = map_page.read()
+            map_json = json.loads(map_str)
+        except urllib.error.HTTPError as e:
+            if e.code != 404:  # accept "not found" as map not being present
+                raise
+        sledrun_wiki = wrpylib.wrmwmarkup.create_sledrun_wiki(sledrun_json=sledrun_json, map_json=map_json)
+        return render_template('rodelbahn_view_wikitext.html',
+                               sledrun_name=sledrun.page_title, sledrun_wiki=sledrun_wiki)
+
     def json_edit(self, sledrun_id):
         q = db.session.query(WrSledrunCache)
         sledrun = q.get(sledrun_id)
         if sledrun is None:
             abort(404)
-        mediawiki_index = f'{current_app.config["MEDIAWIKI_ROOT"]}/index.php'
-        schema_url = f'{mediawiki_index}?title=Winterrodeln:Datenschema/Rodelbahn/V1.json&action=raw'
-        json_url = f'{mediawiki_index}?title={quote_plus(sledrun.page_title)}/Rodelbahn.json&action=raw'
+        h = PylonsHelper()
+        schema_url = h.sledrun_json_schema_url()
+        json_url = h.sledrun_json_url(sledrun.page_title)
         return render_template('json_editor.html', schema_url=schema_url, json_url=json_url)
 
     def update(self):