Skip to content

Commit

Permalink
Add API Endpoint: /notes/create to create a Note (#1357)
Browse files Browse the repository at this point in the history
  • Loading branch information
kizniche committed Feb 27, 2024
1 parent fad93c3 commit 3841b95
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This release changes the install directory from ~/Mycodo to /opt/Mycodo. This ne

- Add Output: GPIO On/Off using pinctrl (First Pi 5-compatible Output)
- Add Output: PWM MQTT Publish
- Add API Endpoint: /notes/create to create a Note ([#1357](https://github.com/kizniche/Mycodo/issues/1357))
- Add ability to switch displaying hostname with custom text
- Add Step Line Series Type to Graph (Synchronous) Widget
- Add controller_restart as client endpoint
Expand Down
1 change: 1 addition & 0 deletions mycodo/mycodo_flask/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def init_api(app):
import mycodo.mycodo_flask.api.function
import mycodo.mycodo_flask.api.input
import mycodo.mycodo_flask.api.measurement
import mycodo.mycodo_flask.api.note
import mycodo.mycodo_flask.api.output
import mycodo.mycodo_flask.api.pid
import mycodo.mycodo_flask.api.settings
Expand Down
107 changes: 107 additions & 0 deletions mycodo/mycodo_flask/api/note.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# coding=utf-8
import logging
import traceback

import flask_login
from flask_accept import accept
from flask_restx import Resource, abort, fields

from mycodo.databases.models import NoteTags
from mycodo.databases.models import Notes
from mycodo.mycodo_flask.api import api, default_responses
from mycodo.mycodo_flask.utils import utils_general

logger = logging.getLogger(__name__)

ns_note = api.namespace(
'notes', description='Note operations')

note_create_fields = ns_note.model('Note Creation Fields', {
'tags': fields.String(
description='List of tag names, separated by commas',
required=True,
example='tag1,tag2,tag3'),
'name': fields.String(
description='The note name.',
required=True,
example='Note Name'),
'note': fields.String(
description='The note text.',
required=True,
example='My Note.')
})


@ns_note.route('/create')
@ns_note.doc(
security='apikey',
responses=default_responses
)
class MeasurementsCreate(Resource):
"""Interacts with Notes in the SQL database."""

@accept('application/vnd.mycodo.v1+json')
@ns_note.expect(note_create_fields)
@flask_login.login_required
def post(self):
"""Create a note."""
if not utils_general.user_has_permission('edit_controllers'):
abort(403)

tags = None
name = None
note = None

if ns_note.payload:
if 'tags' in ns_note.payload:
tags = ns_note.payload["tags"]
if tags is not None:
try:
tags = tags.split(",")
except Exception:
abort(422, message='tags must represent comma separated tags')

if 'name' in ns_note.payload:
name = ns_note.payload["name"]
if name is not None:
try:
name = str(name)
except Exception:
abort(422, message='name must represent a string')

if 'note' in ns_note.payload:
note = ns_note.payload["note"]
if note is not None:
try:
note = str(note)
except Exception:
abort(422, message='note must represent a string')

try:
error = []
list_tags = []

new_note = Notes()

for each_tag in tags:
check_tag = NoteTags.query.filter(
NoteTags.unique_id == each_tag).first()
if not check_tag:
error.append("Invalid tag: {}".format(each_tag))
else:
list_tags.append(check_tag.unique_id)
new_note.tags = ",".join(list_tags)

if not error:
new_note.name = name
new_note.note = note
new_note.save()
else:
abort(500,
message=f'Errors: {", ".join(error)}')

return {'message': 'Success'}, 200
except Exception:
abort(500,
message='An exception occurred',
error=traceback.format_exc())

0 comments on commit 3841b95

Please sign in to comment.