def get_message_names(): messages = MessagePreset.select() output = [] for message in messages: output.append({'name': message.name, 'uuid': str(message.uuid)}) return json.dumps(output)
def get_message(m_uuid): message = MessagePreset.get(MessagePreset.uuid == m_uuid) message = model_to_dict(message) del message['id'] del message['created_at'] message['uuid'] = str(message['uuid']) return json.dumps(message)
def message(m_uuid): # GET: select: message, full, default (full) if request.method == 'GET': message = MessagePreset.get(uuid=m_uuid) data, code = get_data(required=MESSAGE_GET, restrictive=True, hardmode=True) if code == 400: return data_formatting(400) selector = data['select'] collection = {} for selected in selector: if selected == 'full': output = model_to_dict(message) del output['id'] del output['created_at'] output['uuid'] = str(output['uuid']) else: output = {'message': message.message} if len(selector) > 1: collection[selected] = output if len(collection.keys()) != 0: output = collection return data_formatting(data=output) else: data, code = get_data(required=MESSAGE_POST, restrictive=True) if code == 400: return data_formatting(400) preset = MessagePreset.get(uuid=m_uuid) preset.message = data['message'] preset.name = data['heading'] preset.save() if data['person']: plant = Plant.get(uuid=data['plant']) plant.person.preset = preset plant.person.save() MeshDedicatedDispatch().update('message', preset.uuid) return data_formatting()
def update_notification_message(): data = request.form preset, _ = MessagePreset.get_or_create(name=data['name'], defaults={'message': data['text']}) preset.message = data['text'] preset.save() if data['responsible'] is True: plant = Plant.get(Plant.uuid == data['plant']) plant.person.preset = preset plant.person.save() MeshDedicatedDispatch().update('message', preset.uuid) return json.dumps({'info': 'success'})
def create_message_preset(): data = deepcopy(request.form) data['heading'] = data['heading'].lower() for message in MessagePreset.select(): if message.name == data['heading']: return {'info': 'failed, not unique'} msg = MessagePreset() msg.name = data['heading'] msg.message = data['message'] msg.save() return json.dumps({'info': 'success'})
def messages(): if request.method == 'GET': # GET: select: minimal, normal, detailed, extensive, default (normal) # GET: dict: Boolean data, code = get_data(required=MESSAGES_GET, restrictive=True, hardmode=True) if code == 400: return data_formatting(400) mode = data['dict'] selector = data['select'] selectable = ['minimal', 'normal', 'detailed', 'extensive'] messages = MessagePreset.select().dicts() collection = {} for selected in selector: output = [] for message in list(messages): used = [] if selected in selectable: used.append('uuid') if selected in selectable[1:]: used.append('name') if selected in selectable[2:]: used.append('created_at') if selected in selectable[3:]: used.append('message') data = [] if not mode else {} for use in used: if isinstance(message[use], UUID): message[use] = str(message[use]) if not mode: data.append(message[use]) else: data[use] = message[use] output.append(data) if len(selector) > 1: collection[selected] = output if len(collection.keys()) != 0: output = collection return data_formatting(data=output) else: data, code = get_data(required=MESSAGES_PUT, restrictive=True) if code == 400: return data_formatting(400) for message in MessagePreset.select(): if message.name == data['heading']: return data_formatting(304) msg = MessagePreset() msg.name = data['heading'] msg.message = data['message'] msg.save() if data['person']: plant = Plant.get(uuid=data['plant']) plant.person.preset = msg plant.person.save() MeshDedicatedDispatch().update('message', msg.uuid) return data_formatting()
def get_message_content(m_uuid): message = MessagePreset.get(MessagePreset.uuid == m_uuid) return json.dumps(message.message)
from models.sensor import SensorData from models.plant import Person from models.plant import MessagePreset # from models.message import MessagePreset from sensor_scripts.extensions.mailer import PlantMailer # msg = MessagePreset() # msg.name = 'en' # msg.message = '[date] | [time (12h)]\n[plant] - [sensor]\n[current][unit]\n[warning_min] - [warning_max]' # msg.default = True # msg.save() # wizard = Person.get(Person.wizard == True) # wizard.preset = MessagePreset.get(MessagePreset.name == 'en') # wizard.save() msg = MessagePreset.get(MessagePreset.name == 'en') msg.message = '[date] | [time (12h)]\n[plant] - [sensor]\nvalue: [current][unit]\nwarning: [warning_min] - [warning_max]' msg.save() messages = SensorData.select().order_by( SensorData.created_at.desc()).offset(10).limit(10) mailer = PlantMailer() mailer.format_messages(messages) # t = SensorSatisfactionLevel.get(SensorSatisfactionLevel.label == 'threat') # for data in SensorData.select().order_by(SensorData.created_at.desc()).limit(10): # message = SensorDangerMessage() # message.plant = data.plant # message.sensor = data.sensor # message.level = t