Ejemplo n.º 1
0
    def post(self, channel_name):

        channel_id = ChannelModel.find_id_by_name(channel_name)

        if channel_id:
            random_data = randomData()
            message_id = hashData(str(random_data))

            if MessageModel.find_msg_by_channel_id_msg_id(
                    channel_id, message_id):
                return {'message': 'Bad message id'}, 400

            data = Message.parser.parse_args()
            date_now = datetime.utcnow()

            user_id = get_jwt_identity()
            user = UserModel.find_by_id(user_id)
            print(user.username)
            message = MessageModel(message_id, channel_id, data['content'],
                                   date_now, user.username,
                                   UserModel.get_avatar(user.username))

            message.save_to_db()
            return message.json(), 201
        else:

            return {'message': 'Channel not exist.'}, 404
Ejemplo n.º 2
0
def add_schedule():
    """ Schedule a message for a group """
    if request.method == 'GET':
        groups = list(map(lambda x: x.json(), GroupModel.query.all()))
        if len(groups) == 0:

            flash(
                "It seems you have not added the @ConsumerSurveyorBot to any group of any channel. Please, add the the bot to any group to schedule the message for the same."
            )
            return redirect(url_for('dashboard.index'))
        return render_template('dashboard/add_schedule.html', groups=groups)

    if request.method == 'POST':

        error = None
        schedule = parse(request.form['schedule'] + '+05:30')
        if schedule < datetime.datetime.now(pytz.timezone('Asia/Kolkata')):
            error = 'You can not schedule a message in past'
        if error is not None:
            flash(error)
        else:
            print(request.form)
            job = schedule_msg(request.form['message'], schedule,
                               request.form['group_id'])
            message = MessageModel(job.id, request.form['name'],
                                   request.form['message'],
                                   request.form['schedule'] + '+05:30',
                                   request.form['group_id'])
            message.save_to_db()
            return redirect(url_for('dashboard.index'))
    return render_template('dashboard/add_schedule.html')
Ejemplo n.º 3
0
 def post(self):
     data = _message_parser.parse_args()
     data['sender'] = UserModel.find_by_id(get_jwt_identity()).username
     if UserModel.find_by_username(data['receiver']) is None:
         return {"error": f"The user {data['receiver']} does not exist."}
     message = MessageModel(data['sender'], data['receiver'],
                            data['message'], data['subject'])
     message.save_to_db()
     return message.json()
Ejemplo n.º 4
0
 def post(self):
     user = current_identity
     request_data = Message.parser.parse_args()
     message = MessageModel(user.id, **request_data)
     try:
         message.save_to_db()
     except:
         return {"message": "Internal error occurred."}, 500
     return message.json(), 201
Ejemplo n.º 5
0
    def post(self):
        data = Message.parser.parse_args()

        messageing = MessageModel(**data)

        try:
            messageing.save_to_db()
        except:
            return {"message": "An error occurred inserting the message."}, 500

        return messageing.json(), 201
Ejemplo n.º 6
0
 def post(self, team_id):
     # Set data for the message before it gets placed into the database
     data = Message.parser.parse_args()
     created_at = time.time()
     created_by_user = current_identity.username
     msg_to_save = MessageModel(self.random_id(13), data['message'],
                                data['team_id'], created_at,
                                created_by_user)
     try:
         msg_to_save.save_to_db()
     except Exception as e:
         print e
         return {"message": "An error occurred inserting the message."}, 500
     #Will desplay message
     return msg_to_save.json(), 201
Ejemplo n.º 7
0
    def post(self):
        data = self.parser.parse_args()
        # since put it will update regardless
        # TODO delete
        if MessageModel.search_for_message(**data):
            return {'message': "A message like this already exists."}, 400

        message = MessageModel(**data)
        try:
            message.save_to_db()
        except Exception as e:
            return {
                "message": "An error occurred while saving the item.",
                "error": "{}".format(e)
            }, 500
        return redirect(url_for("home"))
Ejemplo n.º 8
0
    def post(self, name):
        if MessageModel.find_by_name(name):
            return {
                'message':
                "An item with name '{}' already exists.".format(name)
            }, 400

        data = Message.parser.parse_args()

        message = MessageModel(name, data['avatar'], data['date'],
                               data['header'], data['content'])

        try:
            message.save_to_db()
        except:
            return {"message": "An error occurred inserting the item."}, 500

        return message.json(), 201
Ejemplo n.º 9
0
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument(consts.RECEIVER_ID,
                            type=str,
                            required=True,
                            help="This field cannot be blank.")
        parser.add_argument(consts.CONTENT,
                            type=str,
                            required=True,
                            help="This field cannot be blank.")

        data = parser.parse_args()

        message = MessageModel(get_jwt_identity(), data[consts.RECEIVER_ID],
                               data[consts.CONTENT], datetime.utcnow())
        message.save_to_db()

        return message.json(), 201