def get_channels(): name = request.args.get('name') if name is None: return to_error_json("Name is required to find Channel") channel = Channel.query.filter(Channel.name == name).first() if channel is not None: return jsonify(channel.dict(False)) else: return to_error_json("Channel {} does not exist".format(name))
def register(): name = request.form.get('name') icon = request.form.get('icon') if not name: return to_error_json("name is required") elif Channel.query.filter(Channel.name == name).first() is not None: return to_error_json("name {} is already in use".format(name)) channel = Channel(name=name, icon=icon) db.session.add(channel) db.session.commit() return jsonify(channel.dict(True))
def get_all_devices(): uuid = request.form.get('uuid') device = Device.query.filter(Device.uuid == uuid).first if device is None: return to_error_json("No Devices Registered") return jsonify(device.dict())
def delete_channels(): name = request.form.get('name') admin_key = request.form.get('admin_key') if name is None: return to_error_json("name is missing") elif admin_key is None: return to_error_json("admin key is missing") channel = Channel.query.filter(Channel.name == name, Channel.admin_key == admin_key).first() if channel is not None: Channel.delete(channel) return to_success_json("Channel Deleted") else: return to_error_json("Channel or admin key mismatch")
def get_messages(): uuid = request.args.get('uuid') if not uuid: return to_error_json("uuid is required") stored_messages = MessageQueue.query.filter_by(uuid=uuid).all() if stored_messages is None: return to_error_json("No messages for your device") message_list = [] for a_message in stored_messages: mess = Message.query.filter_by(id=a_message.message_id).first() message_list.append(mess.dict()) print('Test') return jsonify(message_list)
def get_device(): uuid = request.args.get('uuid') error = None if not uuid: error = 'uuid is required.' if error is None: device = Device.query.filter(Device.uuid == uuid).first() if not device: return to_error_json("Device {} does not exist".format(uuid)) return jsonify(device.dict())
def register(): uuid = request.form.get('uuid') channel_key = request.form.get('channel_key') if not uuid: return to_error_json("uuid is required") elif not channel_key: return to_error_json("channel_key is required") sub = Subscription.query.filter_by(uuid=uuid).filter_by( channel_key=channel_key).first() if sub is None: channel_name = Channel.query.with_entities( Channel.name).filter_by(listen_key=channel_key).first() subscription = Subscription(uuid=uuid, channel_key=channel_key, channel_name=channel_name[0]) db.session.add(subscription) db.session.commit() return jsonify(subscription.dict()) else: return to_error_json("Subscription already exists")
def dispatch_message(): channel_name = request.form.get('channel_name') admin_key = request.form.get('admin_key') title = request.form.get('title') body = request.form.get('message') if not channel_name: return to_error_json("channel_name is required") elif not title: return to_error_json("title is required") elif not body: return to_error_json("message is required") if Channel.query.filter(Channel.name == channel_name).first() is None: return to_error_json("Channel {} does not exist".format(channel_name)) if Channel.query.filter(Channel.admin_key == admin_key).first() is None: return to_error_json("Admin Key does not match any channel") message = Message(channel_name=channel_name, title=title, message=body) db.session.add(message) db.session.commit() send_message(message=message, channel_name=channel_name, admin_key=admin_key) return jsonify(message.dict())