Edition et suppression des notes

This commit is contained in:
Justine Pelletreau 2022-03-16 23:28:00 +01:00
parent fba3ebebff
commit 818e4fb68e
6 changed files with 113 additions and 23 deletions

View File

@ -7,7 +7,6 @@ This is a *very* basic notes-taking program. It uses python with flask and pickl
![Screenshot of the application](./squipnotes.png)
## Future features, todo
* Allow for notes modification and deletion
* A squirrel ascii picture somewhere in the page :3
* Less ugly colors and font
* Better syntax coloring and markdown rendering in general

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB

After

Width:  |  Height:  |  Size: 48 KiB

View File

@ -1,13 +1,13 @@
#!/usr/bin/env python3
#coding: utf-8
from flask import Flask, render_template, Markup, request
from flask import Flask, render_template, Markup, request, redirect, url_for
import markdown
#!---------- squiNotes.py ----------
# My notes-taking app
#-----------------------------!
#CLASS
class note:
def __init__(self, createtime: int, modtime: int, title: str, text: str):
"""
@ -36,22 +36,17 @@ class note:
<hr>
<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="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)
def __str__(self):
return self.title
def catnotes(notelist: list):
"""
Concatenate a list of notes into a str.
"""
final = ""
for note in notelist:
final += note.flaskrender()
return final
#FUNC
def dumpnotes(notes):
"""
Get our notes list and save them as pickle to notes.pickle
@ -77,6 +72,46 @@ def getnotes():
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
@ -84,22 +119,49 @@ app = Flask(__name__)
@app.route('/', methods=['GET'])
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()))
#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'])
def homepage():
import time
#New note
try:
notetitle = request.form['title']
notetext = request.form['text']
rightnow = int(time.time())
newnote = note(createtime=rightnow, modtime=rightnow, title=notetitle, text=notetext)
notes = getnotes()
notes.append(newnote)
notes = sorted(notes, key=lambda note: note.modtime, reverse=True)
dumpnotes(notes)
addnote(newnote)
except:
pass
return render_template("homepage.html", nr = catnotes(getnotes()))
if __name__ == '__main__':
app.run()
app.run(host="0.0.0.0")

Binary file not shown.

View File

@ -1 +1,2 @@
flask>=2.0.3
markdown>=3.3.5

28
src/templates/edit.html Normal file
View 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>