예제 #1
0
파일: web.py 프로젝트: nyddle/snitch-django
def index(app=None):
    db_manager = SqlrMongoManager(
        host=current_app.config['MONGO_HOST'],
        port=current_app.config['MONGO_PORT'],
        db=current_app.config['DB'])
    if request.method == 'POST':
        #request.args
        filters = {}
        if app is not None and app != 'All':
            filters['app'] = app
        if 'from' in request.args:
            filters['from'] = request.args['from']
        if 'to' in request.args:
            filters['to'] = request.args['to']
        if 'type' in request.args:
            filters['etype'] = request.args['type']
        events_list = db_manager.get_events(session['user']['token'],
                                            **filters)
        types_list = db_manager.get_types(session['user']['token'])
        apps_list = db_manager.get_apps(session['user']['token'])
        return jsonify({
            'events': events_list,
            'apps': apps_list,
            'types': types_list
        })

    # todo: refactor for use one mongo session
    return render_template('index.html')
예제 #2
0
파일: api.py 프로젝트: nyddle/snitch-django
def auth():
    if request.json is None or not 'email' in request.json \
            or not 'password' in request.json:
        return jsonify({'result': False, 'reason': 'wrong request'})

    db_manager = SqlrMongoManager(host=current_app.config['MONGO_HOST'],
                                  port=current_app.config['MONGO_PORT'],
                                  db=current_app.config['DB'])
    user = db_manager.check_user(request.json['email'],
                                 request.json['password'], hashed=True)
    if not user:
        return jsonify({'result': False, 'reason': 'Email not found'})

    return jsonify({'token': user['token'], 'result': True})
예제 #3
0
파일: web.py 프로젝트: nyddle/snitch-django
def signup():
    if 'user' in session:
        return redirect(url_for('web.index'))
    db_manager = SqlrMongoManager(host=current_app.config['MONGO_HOST'],
                                  port=current_app.config['MONGO_PORT'],
                                  db=current_app.config['DB'])
    form = SignUpForm()
    if form.validate_on_submit():
        try:
            db_manager.create_user(form.data['email'], form.data['password'])
        except DuplicateEntry:
            flash('User already exists')
        except Exception, e:
            print e
        return redirect(url_for('web.login'))
예제 #4
0
파일: web.py 프로젝트: nyddle/snitch-django
def login():
    if 'user' in session:
        return redirect(url_for('web.index'))
    db_manager = SqlrMongoManager(host=current_app.config['MONGO_HOST'],
                                  port=current_app.config['MONGO_PORT'],
                                  db=current_app.config['DB'])
    form = LoginForm()
    if form.validate_on_submit():
        user = db_manager.check_user(form.data['email'], form.data['password'],
                                     hashed=False)
        if user:
            user.pop('_id')
            session['user'] = user
            return redirect(url_for('web.index'))
        else:
            flash('User not found')
    return render_template('login.html', form=form)
예제 #5
0
파일: api.py 프로젝트: nyddle/snitch-django
def create_user():
    # todo: move to decorator
    # todo: validate email
    # todo: send a letter
    if request.json is None or not 'email' in request.json \
            or not 'password' in request.json:
        return jsonify({'result': False, 'reason': 'wrong request'})
    db_manager = SqlrMongoManager(host=current_app.config['MONGO_HOST'],
                                  port=current_app.config['MONGO_PORT'],
                                  db=current_app.config['DB'])
    try:
        token = db_manager.create_user(request.json['email'],
                                       request.json['password'], hashed=True)
    except DuplicateEntry:
        return jsonify({'result': False, 'reason': 'User already exists'})
    if not token:
        return jsonify({'result': False, 'reason': 'Wrong credentials'})
    return jsonify({'result': True, 'token': token})
예제 #6
0
파일: api.py 프로젝트: nyddle/snitch-django
def post():
    """
    Required: token, message
    Optional args: app, type, emails
    """
    # todo: move default project/event to settings

    if request.json is None or not 'token' in request.json \
            or not 'message' in request.json:
        return jsonify({'result': False, 'reason': 'wrong request'})
    db_manager = SqlrMongoManager(host=current_app.config['MONGO_HOST'],
                                  port=current_app.config['MONGO_PORT'],
                                  db=current_app.config['DB'])
    user = db_manager.validate_token(request.json['token'])
    if not user:
        return jsonify({'result': False, 'reason': 'Wrong token'})

    params = {}
    if 'app' in request.json:
        params['app'] = request.json['app']

    if 'type' in request.json:
        params['etype'] = request.json['type']

    if 'emails' in request.json:
        params['usernames'] = request.json['emails'] \
            if isinstance(request.json['emails'], list) \
            else [request.json['emails']]
        if not user['email'] in params['usernames']:
            params['usernames'].append(user['email'])
    else:
        params['usernames'] = [user['email']]

    event = db_manager.create_event(request.json['token'], request.json['message'], **params)
    if event:
        return jsonify({'result': True})
    return jsonify({'result': False, 'reason': 'Something wrong'})
예제 #7
0
파일: api.py 프로젝트: nyddle/snitch-django
def get():
    """
    JSON params:
    Required:
    token - string
    Optional:
    app - str or list
    date_interval - list with to elems. 0 is min, 1 is max. If you don't need
    max - set it to 0;
    type - string.

    Response:
    result: bool;
    reason: string(returned if result is False;
    events: list; returned if result is True;
    """
    db_manager = SqlrMongoManager(host=current_app.config['MONGO_HOST'],
                                  port=current_app.config['MONGO_PORT'],
                                  db=current_app.config['DB'])
    user = db_manager.validate_token(request.json['token'])
    if not user:
        return jsonify({'result': False, 'reason': 'Wrong token'})

    params = {'app': ['app:default'] if not 'app' in request.json
              else request.json['app']
              if isinstance(request.json['app'], list)
              else [request.json['app']],

              'date_interval': tuple(request.json['date_interval'])
              if 'date_interval' in request.json
              and isinstance(request.json['data_interval'], list) else (),
              'etype': request.json['type'] if 'type' in request.json else None
              }
    # todo: return id
    events = db_manager.get_events(request.json['token'], **params)
    events = list(events)
    return jsonify({'result': True, 'events': events})