]> 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 345cbc113e27d9ec679d8c4a47b556dcae57a6fd..db33cd2a7b3525ac39a5bda8bd58ea0c54103a21 100644 (file)
@@ -1,14 +1,22 @@
-from flask import request, abort, redirect, url_for, flash, render_template
+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
 
-import wrpylib.wrmwmarkup
-import wrpylib.wrmwcache
 import wrpylib.mwmarkup
-
+import wrpylib.wrmwcache
+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:
@@ -35,17 +43,40 @@ 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)
         h = PylonsHelper()
-        return render_template('json_editor.html',
-                               schema_url=url_for('static', filename='schema_v1.json'),
-                               #json_url=url_for('static', filename='latschenhuette.json')
-                               json_url=h.wiki(sledrun.page_title)
-                               )
+        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):
         """Updates the wrsledruncache table from the wiki"""