diff --git a/src/Dockerfile b/Dockerfile similarity index 74% rename from src/Dockerfile rename to Dockerfile index e784cff..4f211c7 100644 --- a/src/Dockerfile +++ b/Dockerfile @@ -1,10 +1,10 @@ FROM python:3-slim-bullseye WORKDIR /app -COPY requirements.txt /app/requirements.txt +COPY ./src/requirements.txt /app/requirements.txt RUN pip install --no-cache-dir --upgrade pip RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt -VOLUME ["/app/notes.pickle"] -COPY . /app +VOLUME ["/app/data"] +COPY ./src /app EXPOSE 8080 ENV FLASK_APP=app CMD [ "python", "-m" , "flask", "run", "--host=0.0.0.0", "--port=8080" ] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..0fb2252 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,20 @@ +version: '3' +networks: + traefik_traefik: + external: true + +services: + sqnotes: + build: . + image: sqnotes + volumes: + - ./data:/app/data + restart: always + #ports: + #- 8080:8080 + labels: + - "traefik.http.routers.gitea.rule=Host(`notes.sq.lan`)" + - "traefik.http.services.gitea.loadbalancer.server.port=8080" + - "traefik.docker.network=traefik_traefik" + networks: + traefik_traefik: diff --git a/src/__pycache__/app.cpython-39.pyc b/src/__pycache__/app.cpython-39.pyc new file mode 100644 index 0000000..ebcb052 Binary files /dev/null and b/src/__pycache__/app.cpython-39.pyc differ diff --git a/src/__pycache__/classes.cpython-39.pyc b/src/__pycache__/classes.cpython-39.pyc new file mode 100644 index 0000000..168291d Binary files /dev/null and b/src/__pycache__/classes.cpython-39.pyc differ diff --git a/src/__pycache__/funcs.cpython-39.pyc b/src/__pycache__/funcs.cpython-39.pyc new file mode 100644 index 0000000..d66397d Binary files /dev/null and b/src/__pycache__/funcs.cpython-39.pyc differ diff --git a/src/app.py b/src/app.py index 6c48820..b23151f 100755 --- a/src/app.py +++ b/src/app.py @@ -1,122 +1,17 @@ #!/usr/bin/env python3 #coding: utf-8 from flask import Flask, render_template, Markup, request, redirect, url_for -import markdown +from classes import note +from funcs import dumpnotes, getnotes, catnotes, delnote, findnote, addnote, mknotedir #!---------- squiNotes.py ---------- # My notes-taking app #-----------------------------! -#CLASS -class note: - def __init__(self, createtime: int, modtime: int, title: str, text: str): - """ - createtime, modtime : epoch time of note writing / modfying - title and text are str - """ - self.modtime = modtime - self.createtime = createtime - self.title = title - self.text = text - def rendertime(self, pretimestamp: int): - """ - Render the given timestamp as dd/mm/yyyy-hh:mm - """ - import datetime - timestamp = datetime.datetime.fromtimestamp(pretimestamp) - timestamp = timestamp.strftime("%d/%m/%Y-%H:%M:%S") - return timestamp - - def flaskrender(self): - """ - Render the note as html for flask, using flask.Markup - """ - rendered = f""" -
-
{Markup.escape(self.title)}
-
- | -
-
Created : {self.rendertime(self.createtime)} -
Modified : {self.rendertime(self.modtime)}

-
{markdown.markdown(self.text, extensions=['fenced_code', 'codehilite', 'nl2br', 'smarty'])}

- """ - return Markup(rendered) - - def __str__(self): - return self.title - -#FUNC -def dumpnotes(notes): - """ - Get our notes list and save them as pickle to notes.pickle - """ - import pickle - with open('notes.pickle', 'wb') as mpf: - pickle.dump(notes, mpf) - - return True - -def getnotes(): - """ - Get our notes from the file notes.pickle - """ - from os.path import exists - import pickle - - if exists("./notes.pickle"): - with open('notes.pickle', 'rb') as mpf: - notes = pickle.load(mpf) - else: - notes = [] - - return notes - -def catnotes(notelist: list): - """ - Concatenate a list of notes into a str. - """ - final = "" - for note in notelist: - final += note.flaskrender() - - return final - -def delnote(timestamp: int): - """ - Delete note in our pickle file for which the createtime corresponds to timestamp - """ - notes = getnotes() - for note in notes: - if int(note.createtime) == int(timestamp): - notes.remove(note) - dumpnotes(notes) - return True - return False - -def findnote(createtime: int): - """ - Find a note in our pickle file of notes by its createtime - """ - notes = getnotes() - for note in notes: - if note.createtime == createtime: - return note - -def addnote(mynote: note): - """ - Add a note to our notes pickle file (and sort it). - """ - notes = getnotes() - notes.append(mynote) - notes = sorted(notes, key=lambda note: note.modtime, reverse=True) - dumpnotes(notes) - - - #----------! MAIN app = Flask(__name__) +mknotedir() @app.route('/', methods=['GET']) def render(): diff --git a/src/classes.py b/src/classes.py new file mode 100644 index 0000000..dd7e7ab --- /dev/null +++ b/src/classes.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 +#coding: utf-8 + +#!---------- squiNotes.py ---------- +# My notes-taking app +#-----------------------------! + +#CLASS +class note: + def __init__(self, createtime: int, modtime: int, title: str, text: str): + """ + createtime, modtime : epoch time of note writing / modfying + title and text are str + """ + self.modtime = modtime + self.createtime = createtime + self.title = title + self.text = text + + def rendertime(self, pretimestamp: int): + """ + Render the given timestamp as dd/mm/yyyy-hh:mm + """ + import datetime + timestamp = datetime.datetime.fromtimestamp(pretimestamp) + timestamp = timestamp.strftime("%d/%m/%Y-%H:%M:%S") + return timestamp + + def flaskrender(self): + """ + Render the note as html for flask, using flask.Markup + """ + import markdown + from flask import Markup + + rendered = f""" +
+
{Markup.escape(self.title)}
+
+ | +
+
Created : {self.rendertime(self.createtime)} +
Modified : {self.rendertime(self.modtime)}

+
{markdown.markdown(self.text, extensions=['fenced_code', 'codehilite', 'nl2br', 'smarty'])}

+ """ + return Markup(rendered) + + def __str__(self): + return self.title diff --git a/src/funcs.py b/src/funcs.py new file mode 100644 index 0000000..2251222 --- /dev/null +++ b/src/funcs.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python3 +#coding: utf-8 +from classes import note + +def mknotedir(): + """ + Create our data directory if does not exist. + """ + from os.path import exists + from os import mkdir + + if not exists("./data"): + mkdir("./data") + +def dumpnotes(notes): + """ + Get our notes list and save them as pickle to notes.pickle + """ + import pickle + with open('./data/notes.pickle', 'wb') as mpf: + pickle.dump(notes, mpf) + + return True + +def getnotes(): + """ + Get our notes from the file notes.pickle + """ + from os.path import exists + import pickle + + if exists("./data/notes.pickle"): + with open('./data/notes.pickle', 'rb') as mpf: + notes = pickle.load(mpf) + else: + notes = [] + + return notes + +def catnotes(notelist: list): + """ + Concatenate a list of notes into a str. + """ + final = "" + for note in notelist: + final += note.flaskrender() + + return final + +def delnote(timestamp: int): + """ + Delete note in our pickle file for which the createtime corresponds to timestamp + """ + notes = getnotes() + for note in notes: + if int(note.createtime) == int(timestamp): + notes.remove(note) + dumpnotes(notes) + return True + return False + +def findnote(createtime: int): + """ + Find a note in our pickle file of notes by its createtime + """ + notes = getnotes() + for note in notes: + if note.createtime == createtime: + return note + +def addnote(mynote: note): + """ + Add a note to our notes pickle file (and sort it). + """ + notes = getnotes() + notes.append(mynote) + notes = sorted(notes, key=lambda note: note.modtime, reverse=True) + dumpnotes(notes) diff --git a/src/notes.pickle b/src/notes.pickle deleted file mode 100644 index 4ccd392..0000000 Binary files a/src/notes.pickle and /dev/null differ