Edition et suppression des notes
This commit is contained in:
parent
fba3ebebff
commit
818e4fb68e
@ -7,7 +7,6 @@ This is a *very* basic notes-taking program. It uses python with flask and pickl
|
|||||||

|

|
||||||
|
|
||||||
## Future features, todo
|
## Future features, todo
|
||||||
* Allow for notes modification and deletion
|
|
||||||
* A squirrel ascii picture somewhere in the page :3
|
* A squirrel ascii picture somewhere in the page :3
|
||||||
* Less ugly colors and font
|
* Less ugly colors and font
|
||||||
* Better syntax coloring and markdown rendering in general
|
* Better syntax coloring and markdown rendering in general
|
||||||
|
BIN
squipnotes.png
BIN
squipnotes.png
Binary file not shown.
Before Width: | Height: | Size: 40 KiB After Width: | Height: | Size: 48 KiB |
106
src/app.py
106
src/app.py
@ -1,13 +1,13 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
#coding: utf-8
|
#coding: utf-8
|
||||||
from flask import Flask, render_template, Markup, request
|
from flask import Flask, render_template, Markup, request, redirect, url_for
|
||||||
import markdown
|
import markdown
|
||||||
|
|
||||||
#!---------- squiNotes.py ----------
|
#!---------- squiNotes.py ----------
|
||||||
# My notes-taking app
|
# My notes-taking app
|
||||||
#-----------------------------!
|
#-----------------------------!
|
||||||
|
|
||||||
|
#CLASS
|
||||||
class note:
|
class note:
|
||||||
def __init__(self, createtime: int, modtime: int, title: str, text: str):
|
def __init__(self, createtime: int, modtime: int, title: str, text: str):
|
||||||
"""
|
"""
|
||||||
@ -36,22 +36,17 @@ class note:
|
|||||||
<hr>
|
<hr>
|
||||||
<div class="notetitle">{Markup.escape(self.title)}</div><br>
|
<div class="notetitle">{Markup.escape(self.title)}</div><br>
|
||||||
<div class="notetime">Created : {self.rendertime(self.createtime)} Modified : {self.rendertime(self.modtime)}</div><br>
|
<div class="notetime">Created : {self.rendertime(self.createtime)} Modified : {self.rendertime(self.modtime)}</div><br>
|
||||||
<div class="notetext">{markdown.markdown(self.text)}</div><br>"""
|
<div class="notetext">{markdown.markdown(self.text)}</div><br>
|
||||||
|
<form action="." method="GET" name="{self.createtime}">
|
||||||
|
<button type="submit" name="delete" value="{self.createtime}" class="delbutton">Delete this</button><button type="submit" name="edit" value="{self.createtime}" class="editbutton">Edit this</button>
|
||||||
|
</form><br>
|
||||||
|
"""
|
||||||
return Markup(rendered)
|
return Markup(rendered)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.title
|
return self.title
|
||||||
|
|
||||||
def catnotes(notelist: list):
|
#FUNC
|
||||||
"""
|
|
||||||
Concatenate a list of notes into a str.
|
|
||||||
"""
|
|
||||||
final = ""
|
|
||||||
for note in notelist:
|
|
||||||
final += note.flaskrender()
|
|
||||||
|
|
||||||
return final
|
|
||||||
|
|
||||||
def dumpnotes(notes):
|
def dumpnotes(notes):
|
||||||
"""
|
"""
|
||||||
Get our notes list and save them as pickle to notes.pickle
|
Get our notes list and save them as pickle to notes.pickle
|
||||||
@ -77,6 +72,46 @@ def getnotes():
|
|||||||
|
|
||||||
return 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
|
#----------! MAIN
|
||||||
@ -84,22 +119,49 @@ app = Flask(__name__)
|
|||||||
|
|
||||||
@app.route('/', methods=['GET'])
|
@app.route('/', methods=['GET'])
|
||||||
def render():
|
def render():
|
||||||
|
#Delete has been clicked
|
||||||
|
try:
|
||||||
|
todelete = request.args.get("delete")
|
||||||
|
delnote(int(todelete))
|
||||||
|
except Exception as e:
|
||||||
|
pass
|
||||||
|
|
||||||
|
#Edit has been clicked
|
||||||
|
try:
|
||||||
|
toedit = request.args.get("edit")
|
||||||
|
if toedit is not None:
|
||||||
|
return redirect(url_for('edit', notenumber=toedit))
|
||||||
|
except Exception as e:
|
||||||
|
pass
|
||||||
|
|
||||||
return render_template("homepage.html", nr = catnotes(getnotes()))
|
return render_template("homepage.html", nr = catnotes(getnotes()))
|
||||||
|
|
||||||
|
#Edition mode
|
||||||
|
@app.route('/edit', methods=['GET', 'POST'])
|
||||||
|
def edit():
|
||||||
|
import time
|
||||||
|
if request.method == "GET":
|
||||||
|
notenumber = request.args.get("notenumber")
|
||||||
|
mynote = findnote(int(notenumber))
|
||||||
|
delnote(int(notenumber))
|
||||||
|
return render_template("edit.html", notenumber=notenumber, ntitle=mynote.title, ntext=mynote.text)
|
||||||
|
|
||||||
|
#Basic route, allows note creation
|
||||||
@app.route('/', methods=['POST'])
|
@app.route('/', methods=['POST'])
|
||||||
def homepage():
|
def homepage():
|
||||||
import time
|
import time
|
||||||
notetitle = request.form['title']
|
#New note
|
||||||
notetext = request.form['text']
|
try:
|
||||||
rightnow = int(time.time())
|
notetitle = request.form['title']
|
||||||
newnote = note(createtime=rightnow, modtime=rightnow, title=notetitle, text=notetext)
|
notetext = request.form['text']
|
||||||
notes = getnotes()
|
rightnow = int(time.time())
|
||||||
notes.append(newnote)
|
newnote = note(createtime=rightnow, modtime=rightnow, title=notetitle, text=notetext)
|
||||||
notes = sorted(notes, key=lambda note: note.modtime, reverse=True)
|
addnote(newnote)
|
||||||
dumpnotes(notes)
|
except:
|
||||||
|
pass
|
||||||
return render_template("homepage.html", nr = catnotes(getnotes()))
|
return render_template("homepage.html", nr = catnotes(getnotes()))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run()
|
app.run(host="0.0.0.0")
|
||||||
|
BIN
src/notes.pickle
BIN
src/notes.pickle
Binary file not shown.
@ -1 +1,2 @@
|
|||||||
flask>=2.0.3
|
flask>=2.0.3
|
||||||
|
markdown>=3.3.5
|
||||||
|
28
src/templates/edit.html
Normal file
28
src/templates/edit.html
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<link rel="stylesheet" href="{{ url_for('static', filename='styles/main.css') }}" type="text/css" />
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
||||||
|
<link href='https://fonts.googleapis.com/css?family=Source Sans Pro' rel='stylesheet'>
|
||||||
|
<title>squip_notes</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<h1>$ ~/squip_notes</h3>
|
||||||
|
<div class="div1">
|
||||||
|
<form action="." method="POST">
|
||||||
|
<textarea type="text" name="title" class="title">{{ ntitle }}</textarea><br>
|
||||||
|
<textarea type="text" name="text" rows = "5" cols = "60*" class="text">{{ ntext }}</textarea><br>
|
||||||
|
<button type="submit" name="submit">OK</button>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
You are editing {{ notenumber }} <br>
|
||||||
|
<!--Its title is {{ ntitle }} <br>
|
||||||
|
Its text says {{ ntext }} <br>-->
|
||||||
|
<br>
|
||||||
|
{{ nr }}
|
||||||
|
<div>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</meta>
|
||||||
|
</html>
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user