def show_note(cfg: ConfigParser, note_id: int): """Show the note `note_id`""" auth = HTTPBasicAuth(cfg["basic_auth_user"], cfg["basic_auth_password"]) if has_auth_infos(cfg) is True else None response = requests.get(f"{cfg['url']}:{cfg['port']}/api/notes/{note_id}", auth=auth) if response.status_code == 200: tmp = response.json() n = note.Note(tmp["id"], tmp["title"], tmp["content"], tmp["creation_date"], tmp["update_date"]) print(f"{n.title} :\n---") print(f"{n.content}")
def list_notes(cfg: ConfigParser): """List all notes""" auth = HTTPBasicAuth(cfg["basic_auth_user"], cfg["basic_auth_password"]) if has_auth_infos(cfg) is True else None response = requests.get(f"{cfg['url']}:{cfg['port']}/api/notes", auth=auth) if response.status_code == 200: notes: List[Dict] = response.json() print(f"{len(notes)} Note{'' if len(notes) == 1 else 's'} :") for d in notes: n = note.Note(d["id"], d["title"], d["content"], d["creation_date"], d["update_date"]) print(f"\t- {n.title} (id {n.id})")
def create_note(self, title: str, content: str) -> note.Note: """Create a note with `title`""" self.conn = sqlite3.connect(self.db_name) cursor = self.conn.cursor() current_date = int(time.time()) cursor.execute( f"INSERT INTO {self.table_name}(title, content, creation_date, update_date) VALUES('{title}', '{content}', {current_date}, {current_date})" ) note_id = cursor.lastrowid self.conn.commit() self.conn.close() return note.Note(note_id, title, None, current_date, current_date)
def get_note(self, note_id: int) -> note.Note: """Returns a single note given an id or None""" self.conn = sqlite3.connect(self.db_name) cursor = self.conn.cursor() cursor.execute(f"SELECT * FROM {self.table_name} WHERE id = {note_id}") tmp = cursor.fetchone() self.conn.commit() self.conn.close() if tmp: return note.Note(int(tmp[0]), tmp[1], tmp[2], int(tmp[3]), int(tmp[4])) return None
def publish(): """ Jump to the publish page default """ if request.method == 'POST': content = request.form.get('content') currentDT = datetime.datetime.now() msg = n.Note(note=content, publish_date=currentDT, user_id=0, visits=0) session = db.get_session() if session: session.add(msg) session.commit() db.put_session() return redirect(url_for('home.index')) return render_template('note/publish.html')
def get_notes(self) -> List[note.Note]: """Returns a list of all notes""" self.conn = sqlite3.connect(self.db_name) cursor = self.conn.cursor() cursor.execute(f"SELECT * FROM {self.table_name}") rows = cursor.fetchall() notes: List[note.Note] = [] for row in rows: n = note.Note(int(row[0]), row[1], row[2], int(row[3]), int(row[4])) notes.append(n) self.conn.commit() self.conn.close() return notes