コード例 #1
0
ファイル: order.py プロジェクト: ollar/canteen2
    def post(self):
        if not self._check_order():
            return make_response(
                jsonify({
                    'type': 'error',
                    'text': 'This meal is already in your order'
                }), 403)

        _order_date = datetime.datetime.strptime(self.json.get('order_date'),
                                                 "%Y-%m-%d").date()

        if not self._check_actual_date(_order_date.timetuple().tm_yday):
            return make_response(
                jsonify({
                    'type':
                    'error',
                    'text':
                    'You are trying to create an outdated order. That is not allowed'
                }), 403)

        new_order = Order(order_date=_order_date,
                          meal_id=self.json.get('meal_id'),
                          user_id=self.json.get('user_id'),
                          quantity=self.json.get('quantity'))

        db_session.add(new_order)
        db_session.commit()

        return jsonify(_parse_order(new_order, detailed=False))
コード例 #2
0
ファイル: user.py プロジェクト: ollar/canteen2
def login():
    json = request.json
    user = db_session.query(User).filter_by(
        username=json.get('username').strip()).first()
    if not user:
        return make_response(
            jsonify({
                'type': 'error',
                'text': 'no users with such username'
            }), 401)
    elif check_password_hash(user.password, json.get('password').strip()):
        token = {}
        tokens = db_session.query(Token).filter_by(user_id=user.id).all()
        is_expired_list = list(map(lambda t: t.is_expired(), tokens))
        all_expired = all(is_expired_list)

        if all_expired:
            token = Token(user_id=user.id)
            db_session.add(token)
            db_session.commit()
        else:
            token = [t for t in tokens if not t.is_expired()].pop()

        logged_user = _parse_user(user)
        logged_user.update({'token': token.token})

        return make_response(jsonify(logged_user), 200)
    else:
        return make_response(
            jsonify({
                'type': 'error',
                'text': 'password incorrect'
            }), 401)
コード例 #3
0
ファイル: views.py プロジェクト: deadpriest88/wind_walker
def add_entry():
    if not session.get('logged_in'):
        abort(401)

    u = User(request.form['name'], request.form['email'])
    db_session.add(u)
    db_session.commit()

    flash('New entry was successfully posted')
    return redirect(url_for('show_entries'))
コード例 #4
0
    def run(self, num):
        for user in self.users:
            for meal in self.meals:
                new_comment = Comment(meal_id=meal.id,
                                      user_id=user.id,
                                      content=self._compose_comment(num))

                db_session.add(new_comment)
            print('Created comments for:', user)
            db_session.commit()
        print("Comments creation complete")
コード例 #5
0
    def run(self, num):
        for i in range(num):
            with open('./words', 'r') as f:
                word = random.choice(f.read().splitlines())

            new_user = User(real_name=word,
                            username=word,
                            password=word,
                            email=word + '@gmail.com')

            db_session.add(new_user)
            print('Created user:', new_user)
        db_session.commit()
コード例 #6
0
ファイル: meal.py プロジェクト: ollar/app-docked
    def post(self):
        new_meal = Meal(title=self.json.get('title'),
                        description=self.json.get('description'),
                        category=self.json.get('category'),
                        day_linked=self.json.get('day_linked'),
                        source_price=self.json.get('source_price'),
                        price=self.json.get('price'),
                        enabled=self.json.get('enabled'))

        db_session.add(new_meal)
        db_session.commit()

        return jsonify(_parse_meal(new_meal))
コード例 #7
0
    def run(self, num):
        for user in self.users:
            for date in self._get_month_dates(num):
                _meals = self._get_day_meals(date)

                for _m in _meals:
                    new_order = Order(order_date=date,
                                      meal_id=_m.id,
                                      user_id=user.id,
                                      quantity=random.randint(1, 10))

                    db_session.add(new_order)
            print('Created orders for:', user)
            db_session.commit()
        print("Orders creation complete")
コード例 #8
0
    def run(self):
        with open('./demo_meals.json', 'r') as f:
            meals = json.load(f)

            for meal in meals:
                new_meal = Meal(title=meal.get('title'),
                                description=meal.get('description'),
                                category=meal.get('category'),
                                day_linked=meal.get('day_linked'),
                                source_price=meal.get('source_price'),
                                price=meal.get('price'),
                                enabled=meal.get('enabled'),
                                timestamp_modified=datetime.datetime.utcnow())

                db_session.add(new_meal)
                print('Created meal:', new_meal)
            db_session.commit()
コード例 #9
0
    def post(self):
        check_comment = db_session.query(Comment).filter_by(
            user_id=g.user.id, meal_id=self.json.get('meal_id')).first()
        if check_comment:
            return make_response(
                jsonify({
                    'type': 'error',
                    'text': 'you have already commented this meal'
                }))
        new_comment = Comment(user_id=g.user.id,
                              meal_id=self.json.get('meal_id'),
                              content=self.json.get('content', ''))

        db_session.add(new_comment)
        db_session.commit()

        return jsonify(_parse_comment(new_comment, detailed=False))
コード例 #10
0
ファイル: user.py プロジェクト: ollar/app-docked
    def post(self):
        assert self.json.get('username'), 'username is required'
        assert self.json.get('password'), 'password is required'

        new_user = User(real_name=self.json.get('real_name').strip(),
                        username=self.json.get('username').strip(),
                        password=self.json.get('password').strip(),
                        email=self.json.get('email').strip().lower())

        db_session.add(new_user)
        try:
            db_session.commit()
        except IntegrityError as e:
            return make_response(
                jsonify({
                    'type': 'error',
                    'text': 'username not unique'
                }), 403)

        return jsonify(_parse_user(new_user))
コード例 #11
0
ファイル: setup-app.py プロジェクト: deadpriest88/wind_walker
from main.database import init_db, db_session
from main.model.auth import User

if __name__ == '__main__':
    print('Installazione di base')
    print("Creo le tabelle del database")
    init_db()

    # Se non e' presente faccio il primo inserimento dell'utente admin
    if User.query.filter(User.first_name == 'admin').count() == 0:
        print("Inserisco l'utente admin")
        u = User(first_name='admin', email='*****@*****.**', displayed_name='Administrator', password='******')
        db_session.add(u)
        db_session.commit()


    else:
        print("L'utente admin e' gia' presente nel database")
    print('Fine Installazione di base')