def get(self): args = parser.parse_args() if args['level']: moods = Mood.select().where(Mood.level == args['level']) else: moods = Mood.select() serialized = MoodSerializer(list(moods), many=True) return jsonify({"moods": serialized.data})
def get_all_moods(): with sqlite3.connect("./daily_journal.db") as conn: conn.row_factory =sqlite3.Row db_cursor = conn.cursor() db_cursor = conn.cursor() db_cursor.execute(""" SELECT m.id, m.label FROM moods m """) moods = [] dataset = db_cursor.fetchall() for row in dataset: mood = Mood(row['id'], row['label']) moods.append(mood.__dict__) return json.dumps(moods)
def get_all_entries(): with sqlite3.connect("./daily-journal.db") as conn: conn.row_factory = sqlite3.Row db_cursor = conn.cursor() db_cursor.execute(""" SELECT e.id, e.concept, e.entry, e.date, e.moodId, m.id, m.label FROM entries e JOIN moods m ON m.id = e.moodId """) entries = [] dataset = db_cursor.fetchall() for row in dataset: entry = Entry(row['id'], row['concept'], row['entry'], row['date'], row['moodId']) mood = Mood(row['id'], row['label']) entry.mood = mood.__dict__ entries.append(entry.__dict__) return json.dumps(entries)
def get_single_entry(id): with sqlite3.connect("./daily-journal.db") as conn: conn.row_factory = sqlite3.Row db_cursor = conn.cursor() db_cursor.execute( """ SELECT e.id, e.concept, e.entry, e.date, e.moodId, m.id, m.label FROM entries e JOIN moods m ON m.id = e.moodId WHERE e.id = ? """, (id, )) data = db_cursor.fetchone() entry = Entry(data['id'], data['concept'], data['entry'], data['date'], data['moodId']) mood = Mood(data['id'], data['label']) entry.mood = mood.__dict__ return json.dumps(entry.__dict__)
def get_all_moods(): # Open a connection to the database with sqlite3.connect("./dailyjournal.db") as conn: # Just use these. It's a Black Box. conn.row_factory = sqlite3.Row db_cursor = conn.cursor() # Write the SQL query to get the information you want db_cursor.execute(""" SELECT m.id, m.label FROM Moods m """) # Initialize an empty list to hold all mood representations moods = [] # Convert rows of data into a Python list dataset = db_cursor.fetchall() # Iterate list of data returned from database for row in dataset: # Create an mood instance from the current row. # Note that the database fields are specified in # exact order of the parameters defined in the # mood class above. mood = Mood(row['id'], row['label']) moods.append(mood.__dict__) # Use `json` package to properly serialize list as JSON return json.dumps(moods)
def get_single_mood(id): with sqlite3.connect("./daily_journal.db") as conn: conn.row_factory = sqlite3.Row db_cursor = conn.cursor() db_cursor.execute(""" SELECT m.id, m.label FROM moods m WHERE m.id = ? """, (id, )) data = db_cursor.fetchone() mood = Mood(data['id'], data['label']) return json.dumps(mood.__dict__)
def get_single_mood(id): with sqlite3.connect("./dailyjournal.db") as conn: conn.row_factory = sqlite3.Row db_cursor = conn.cursor() # Use a ? parameter to inject a variable's value # into the SQL statement. db_cursor.execute( """ SELECT a.id, a.label FROM Moods a WHERE a.id = ? """, (id, )) # Load the single result into memory data = db_cursor.fetchone() # Create an mood instance from the current row mood = Mood(data['id'], data['label']) return json.dumps(mood.__dict__)
def API_add_mood(): user_id = int(verify_token(request.form["token"])) mood = Mood(user_id=user_id, mood=request.form["mood_id"]) push_model(mood) return json.dumps({"state": "ok"})
def get(self, year, month, day): moods = Mood.select().where(Mood.created.between( datetime.date(year, month, day), datetime.date(year, month, day) + datetime.timedelta(days=1))) serialized = MoodSerializer(list(moods), many=True) return jsonify({"moods": serialized.data})
def post(self): args = parser.parse_args() mood = Mood.create(level=args['level']) serialized = MoodSerializer(mood) return jsonify(serialized.data)