Example #1
0
    def post(self):
        json_data = request.get_json(force=True)
        if not json_data:
           return {'message': 'No input data provided'}, 400
        # Validate and deserialize input
        data, errors = user_schema.load(json_data)
        if errors:
            return errors, 422
        user = Users.query.filter_by(user_name=data['user_name']).first()
        if user:
            return {'message': 'User {} does exist'.format(data['user_name'])}, 400

        #try:
        user = Users(
            user_name=json_data['user_name'],
            password=Users.generate_hash(json_data['password']),
            )
        db.session.add(user)
        db.session.commit()

        result = user_schema.dump(user).data
        access_token = create_access_token(identity=data['user_name'])
        refresh_token = create_refresh_token(identity=data['user_name'])

        return {
            "status": 'success',
            'message': 'User {} was created'.format(result['user_name']),
            'access_token': access_token,
            'refresh_token': refresh_token
                }, 201
Example #2
0
def handle_command(command, channel, user, bot):
    """
        Executes bot command if the command is known
    """
    # Regex match for google search
    bot_response = re.match(r".*?\W*(google)\s+\b(.*)", command.lower())
    bot_user = parse_direct_mention(command)[0]
    # This is where you start to implement more commands!
    query = ""
    if bot_user != bot:
        if bot_response:
            query = bot_response.group(2)
            tmp = [i for i in search(query, tld="co.in", num=10, stop=1, pause=2)]
            try:
                response = tmp[0]
            # Exception for index out of range
            except IndexError:
                response = "No Search"
        else:
            return

        # Add search into db
        print(user, bot)
        user_name = ID_Dict.get(user, 'None')
        Users.insert(user_name, query, datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
    else:
        read_all = re.match(r".*?\W*(findall)", command.lower())
        read_by_name = re.match(r".*?\W*(where)\s+\b(.*)\s+\b(.*)", command.lower())
        if read_all:
            response = Users.select('', '')
            response = str(response)
        elif read_by_name:
            try:
                query1 = read_by_name.group(2)
                query2 = read_by_name.group(3)
                response = Users.select(query1, query2)
                response = str(response)
            except ValueError:
                return
        else:
            return
    # Sends the response back to the channel
    slack_client.api_call(
        "chat.postMessage",
        channel=channel,
        text=response
    )
Example #3
0
def register():
    form = Register_Form()
    if form.validate_on_submit():
        user = Users(name=form.name.data, pwd=form.pwd.data)
        db.session.add(user)
        db.session.commit()
        flash('注册成功')
        return redirect(url_for('blog.index'))
    return render_template('register.html', form=form)
Example #4
0
def register():
    form = Register_Form()
    if form.validate_on_submit():
        user = Users.query.filter_by(name=form.name.data).first()
        if user is not None and user.name == form.name.data:
            flash('User already exist')
            return redirect(url_for('pj6.register'))
        else:
            user = Users(name=form.name.data, pwd=form.pwd.data)
            db.session.add(user)
            db.session.commit()
            flash('Register succeed')
            return redirect(url_for('pj6.index'))
    return render_template('register.html', form=form)
Example #5
0
def register():
    form = Register_Form()
    if form.validate_on_submit():
        user = Users(username=form.username.data,
                     password=form.password.data,
                     phone=form.phone.data,
                     email=form.email.data,
                     name=form.name.data,
                     organization=form.organization.data)
        db.session.add(user)
        db.session.commit()
        flash('注册成功')
        return redirect(url_for('login'))
    return render_template('register.html', form=form)
Example #6
0
def register():
    form = Register_Form()
    if form.validate_on_submit():
        if form.pwd.data != form.confirm_pwd.data:
            flash('两次密码输入不一致')
            return render_template('ssx_register.html', form=form)
        if Users.query.filter_by(name=form.name.data).first():
            flash('登录名已存在')
            return render_template('ssx_register.html', form=form)
        user = Users(name=form.name.data, pwd=form.pwd.data)
        db.session.add(user)
        db.session.commit()
        flash('注册成功')
        return redirect(url_for('login'))
    return render_template('ssx_register.html', form=form)
Example #7
0
 def post(self):
     json_data = request.get_json(force=True)
     if not json_data:
            return {'status':'fail','message': 'No input data provided'}, 200
     user = Users.query.filter_by(email=json_data['email']).first()
     if user:
         return {'status':'fail','message': 'User already exists'}, 200
     user = Users(
         firstname=json_data['firstname'],
         lastname=json_data['lastname'],
         phone=json_data['phone'],
         email=json_data['email'],
         password=generate_password_hash(json_data['password'], method='sha256'),
         privilege=json_data['privilege']
         )
     db.session.add(user)
     db.session.commit()
     result ="saved successfully"
     return { 'status': 'success','message':'register successful', 'data': result }, 200
Example #8
0
    def post(self):
        json_data = request.get_json(force=True)
        if not json_data:
           return {'message': 'No input data provided'}, 400
        # Validate and deserialize input
        data, errors = user_schema.load(json_data)
        if errors:
            return errors, 422
        user = Users.query.filter_by(user_name=data['user_name']).first()
        if not user:
            return {
                'message': 'User {} doesn\'t  exist'.format(data['user_name']),
                }, 400

        if Users.verify_hash(data['password'], user.password):
            access_token = create_access_token(identity=data['user_name'])
            refresh_token = create_refresh_token(identity=data['user_name'])
            return {
            'message': 'Logged in as {}'.format(user.user_name),
            'access_token': access_token,
            'refresh_token': refresh_token
            }
        else:
            return {'message': 'Wrong credentials'}, 400