Browse Source

Updated flask to work with sqlite.

root 4 years ago
parent
commit
1076e72d39
8 changed files with 129 additions and 32 deletions
  1. 4 4
      image/Dockerfile
  2. 0 1
      image/req.txt
  3. 110 20
      messages.py
  4. 0 1
      req.txt
  5. 5 0
      static/style.css
  6. 1 0
      templates/base.html
  7. 0 4
      templates/list.html
  8. 9 2
      templates/messages.html

+ 4 - 4
image/Dockerfile

@@ -7,10 +7,10 @@ RUN python -m venv /home/python/venv
 ENV PATH="/home/python/venv/bin:$PATH"
 COPY req.txt req.txt
 RUN pip install -r req.txt
-COPY jam.h jam.h
-COPY jamlib.a jamlib.a
-COPY jamlib_build.py jamlib_build.py
-RUN python jamlib_build.py
+# COPY jam.h jam.h
+# COPY jamlib.a jamlib.a
+# COPY jamlib_build.py jamlib_build.py
+# RUN python jamlib_build.py
 RUN pip wheel --wheel-dir=/home/python/wheels -r req.txt
 RUN find .
 RUN ls -la

+ 0 - 1
image/req.txt

@@ -1,7 +1,6 @@
 appdirs==1.4.4
 attrs==19.3.0
 black==19.10b0
-cffi==1.14.0
 click==7.1.2
 Flask==1.1.2
 Flask-Caching==1.9.0

+ 110 - 20
messages.py

@@ -7,6 +7,7 @@ import subprocess
 import base64
 import os
 import textwrap
+import sys
 
 base_path = "/messagebase"
 
@@ -22,22 +23,89 @@ def format_datetime(value):
 
 # Check Configuring Flask-Caching section for more details
 # cache = Cache(app, config={"CACHE_TYPE": "filesystem", "CACHE_DIR": "cache"})
-cache = Cache(app, config={"CACHE_TYPE": "redis", "CACHE_REDIS_HOST": "redis"})
+cache = Cache(
+    app, config={"CACHE_TYPE": "redis", "CACHE_REDIS_HOST": "redis"}
+)
 # cache = Cache(app, config={"CACHE_TYPE": "redis", "CACHE_REDIS_HOST": "olympus"})
 
-import jammin
+# import jammin
+
+import sqlite3
+
+dbconnect = sqlite3.connect("db/message.sqlite3")
+dbc = dbconnect.cursor()
 
 bases = {
-    "FSXNET-General": "msgs/fsx_gen",
-    "FSXNET-BBS": "msgs/fsx_bbs",
-    "FSXNET-BOT": "msgs/fsx_bot",
-    "FSXNET-Encryption": "msgs/fsx_cry",
-    "FSXNET-Ham Radio": "msgs/fsx_ham",
-    "FSXNET-Magicka": "msgs/fsx_mag",
-    "FSXNET-Mystic": "msgs/fsx_mys",
+    "FSXNET-General": "fsx_gen",
+    "FSXNET-Ads": "fsx_ads",
+    "FSXNET-BBS": "fsx_bbs",
+    "FSXNET-BOT": "fsx_bot",
+    # "FSXNET-Encryption": "msgs/fsx_cry",
+    "FSXNET-Ham Radio": "fsx_ham",
+    # "FSXNET-Magicka": "msgs/fsx_mag",
+    "FSXNET-Magicka": "fsx_mag",
+    "FSXNET-Mystic": "fsx_mys",
+    "FSXNET-Enigma": "fsx_eng",
     # "HappyNet-General": "msgs/hpy_gen",
 }
 
+
+def bbs_get_messages(area):
+    global dbc
+
+    messages = []
+
+    for row in dbc.execute(
+        # "SELECT message_id, to_user_name, from_user_name, subject, modified_timestamp from message WHERE area_tag=?",
+        "SELECT message_id, to_user_name, from_user_name, subject, modified_timestamp from message WHERE area_tag=? ORDER BY message_id;",
+        (area,),
+    ):
+        stamp = pendulum.parse(row[4]).timestamp()
+        messages.append(
+            {
+                "MsgNum": row[0],
+                "number": row[0],
+                "to": row[1],
+                "from": row[2],
+                "subject": row[3],
+                "written": stamp,
+                # // written
+                # // received
+                # // processed
+            }
+        )
+
+    return messages
+
+
+def bbs_message(area, msgno):
+    global dbc
+
+    messages = []
+
+    dbc.execute(
+        "SELECT message_id, to_user_name, from_user_name, subject, modified_timestamp, message from message WHERE message_id=?",
+        (msgno,),
+    )
+    row = dbc.fetchone()
+    stamp = pendulum.parse(row[4]).timestamp()
+    return {
+        "MsgNum": row[0],
+        "number": row[0],
+        "to": row[1],
+        "from": row[2],
+        "subject": row[3],
+        "written": stamp,
+        "received": stamp,
+        "processed": stamp,
+        "text": row[5],  # .decode("cp437"),
+        "bytes": row[5].encode("cp437")
+        # // written
+        # // received
+        # // processed
+    }
+
+
 # bases = {"FSX_BOT": "fsx_bot"}
 
 # @cache.memoize(timeout=5 * 60, key_prefix="messages")
@@ -45,14 +113,15 @@ bases = {
 
 @cache.memoize(timeout=5 * 60)
 def get_messages(base):
-    messages = jammin.get_messages(base)
+    messages = bbs_get_messages(base)
     messages.reverse()
     return messages
 
 
 @cache.memoize(timeout=60)
 def get_message(base, msgno):
-    message = jammin.read_message(base, msgno)
+    # message = jammin.read_message(base, msgno)
+    message = bbs_message(base, msgno)
     return message
 
 
@@ -142,7 +211,9 @@ def display_ansi(area, msgno):
         return "RATS", 404
     if not "text" in message:
         return "RATS", 404
-    png = ansi_to_png(message["bytes"].replace(b"\r", b"\n"), msgno)
+    # png = ansi_to_png(message["bytes"].replace(b"\r", b"\n"), msgno)
+    png = ansi_to_png(message["bytes"], msgno)
+    # png = ansi_to_png(message["bytes"].replace("\r", "\n"), msgno)
     response = make_response(png)
     response.headers.set("Content-Type", "image/png")
     return response
@@ -156,7 +227,7 @@ def display_message(area, msgno):
             "missing-area.html", base_path=base_path, title="Missing Area"
         )
 
-# message = jammin.read_message(bases[area], msgno)
+    # message = jammin.read_message(bases[area], msgno)
     message = get_message(bases[area], msgno)
 
     if not message:
@@ -168,13 +239,26 @@ def display_message(area, msgno):
         )
 
     messages = get_messages(bases[area])
-    total = len(messages)
+    # prevmsg and nextmsg are completely different now.
+
     prevmsg = None
     nextmsg = None
-    if (msgno > 1):
-        prevmsg = msgno - 1
-    if (msgno < total):
-        nextmsg = msgno + 1
+    total = len(messages)
+
+    for idx, msg in enumerate(messages):
+        if msg["MsgNum"] == msgno:
+            # Ok, found what we're looking for
+            if idx > 0:
+                prevmsg = messages[idx - 1]["MsgNum"]
+            if idx + 1 < total:
+                nextmsg = messages[idx + 1]["MsgNum"]
+
+    # prevmsg = None
+    # nextmsg = None
+    # if msgno > 1:
+    #     prevmsg = msgno - 1
+    # if msgno < total:
+    #    nextmsg = msgno + 1
 
     if "text" in message:
         if "\x1b" in message["text"]:
@@ -186,8 +270,14 @@ def display_message(area, msgno):
         else:
             text = message["text"].replace("\r", "\n")
             # Ok, latest changes aren't doing word-wrap for us, so do it here.
-            text = "\n".join( [ textwrap.fill(txt, width=78, replace_whitespace=False) for txt in text.splitlines()] )
-            message["text"] = text; # message["text"].replace("\r", "\n")  # <br >\n")
+            text = "\n".join(
+                [
+                    textwrap.fill(txt, width=78, replace_whitespace=False)
+                    for txt in text.splitlines()
+                ]
+            )
+            message["text"] = text
+            # message["text"].replace("\r", "\n")  # <br >\n")
 
     return render_template(
         "message.html",

+ 0 - 1
req.txt

@@ -1,7 +1,6 @@
 appdirs==1.4.4
 attrs==19.3.0
 black==19.10b0
-cffi==1.14.0
 click==7.1.2
 Flask==1.1.2
 Flask-Caching==1.9.0

+ 5 - 0
static/style.css

@@ -22,4 +22,9 @@
   font: 0.9rem Inconsolata, monospace;
 }
 
+.head {
+  background: lightblue;
+  color: black;
+}
+
 </style>

+ 1 - 0
templates/base.html

@@ -1,3 +1,4 @@
+<!DOCTYPE html>
 <html class="no-js" lang="en" dir="ltr">
   <head>
     <meta charset="utf-8">

+ 0 - 4
templates/list.html

@@ -10,8 +10,4 @@
 <div>&nbsp;</div>
 {% endfor %}
 
-<p>Sorry, there's a "bug" in how the messages are on the BBS.
-Every Sunday messages are packed, and the message counter gets reset to 1.
-This results in links to messages breaking after Sunday's packing.</p>
-<p>Sorry.</p>
 {% endblock %}

+ 9 - 2
templates/messages.html

@@ -20,9 +20,16 @@
 </div>
 </div>
 
-{% for msg in messages %}
 <div class="grid-x padding-x">
+<div class="small-2 medium-1 large-1 cell head">Msg #</div>
+<div class="hide-for-small-only medium-3 large-2 cell head">Written</div>
+<div class="small-2 medium-2 large-2 cell head">From</div>
+<div class="small-2 medium-2 large-2 cell head">To</div>
+<div class="small-4 medium-4 large-5 cell head">Subject</div>
+</div>
 
+{% for msg in messages %}
+<div class="grid-x padding-x">
 <div class="small-2 medium-1 large-1 cell"><a href="{{ url_for('display_message', area=area, msgno=msg.MsgNum) }}">{{ msg.MsgNum }}</a></div>
 <div class="hide-for-small-only medium-3 large-2 cell">{{ msg.written|datefmt }}</div>
 <div class="small-2 medium-2 large-2 cell">{{ msg.from }}</div>
@@ -36,4 +43,4 @@
 
 </div>
 
-{% endblock %}
+{% endblock %}