Exemplo n.º 1
0
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'})
Exemplo n.º 2
0
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()