Esempio n. 1
0
    def put(self, user_id):
        json_dict = {
            'real_name': self.json.get('real_name'),
            'username': self.json.get('username'),
            'email': self.json.get('email')
        }

        if self.json.get('password'):
            json_dict.update({
                'password':
                generate_password_hash(
                    str(self.json.get('password')).encode())
            })

        update_user = db_session.query(User).filter_by(id=user_id)
        try:
            update_user.update(json_dict)
            db_session.commit()
        except StatementError:
            return make_response(
                jsonify({
                    'type': 'error',
                    'text': 'database error'
                }), 500)

        return make_response(jsonify(_parse_user(update_user.first())), 200)
Esempio n. 2
0
    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))
Esempio n. 3
0
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)
Esempio n. 4
0
 def delete(self, user_id):
     user = db_session.query(User).get(user_id)
     if user:
         db_session.delete(user)
         db_session.commit()
         return jsonify(_parse_user(user, detailed=False))
     return make_response(jsonify({
         'type': 'error',
         'text': 'not found'
     }), 404)
Esempio n. 5
0
 def delete(self, meal_id):
     meal = db_session.query(Meal).get(meal_id)
     if meal:
         db_session.delete(meal)
         db_session.commit()
         return jsonify(_parse_meal(meal, detailed=False))
     return make_response(jsonify({
         'type': 'error',
         'text': 'not found'
     }), 404)
Esempio n. 6
0
 def delete(self, comment_id):
     comment = db_session.query(Comment).get(comment_id)
     if comment:
         db_session.delete(comment)
         db_session.commit()
         return jsonify(_parse_comment(comment, detailed=False))
     return make_response(jsonify({
         'type': 'error',
         'text': 'not found'
     }), 404)
Esempio n. 7
0
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'))
Esempio n. 8
0
    def delete(self, order_id):
        order = db_session.query(Order).get(order_id)

        if order:
            _order_date = order.order_date
            if not self._check_actual_date(_order_date.timetuple().tm_yday):
                return make_response(jsonify({'type': 'error', 'text': 'not allowed remove old order'}), 403)
            db_session.delete(order)
            db_session.commit()
            return jsonify(_parse_order(order, detailed=False))
        return make_response(jsonify({'type': 'error', 'text': 'not found'}), 404)
Esempio n. 9
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")
Esempio n. 10
0
    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))
Esempio n. 11
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()
Esempio n. 12
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")
Esempio n. 13
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()
Esempio n. 14
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))
Esempio n. 15
0
    def put(self, meal_id):
        json_dict = {
            'title': self.json.get('title'),
            'description': self.json.get('description'),
            'category': self.json.get('category'),
            'day_linked': self.json.get('day_linked'),
            'enabled': self.json.get('enabled'),
            'source_price': self.json.get('source_price'),
            'price': self.json.get('price')
        }

        json_dict.update({'timestamp_modified': datetime.datetime.utcnow()})

        update_meal = db_session.query(Meal).filter_by(id=meal_id)
        update_meal.update(json_dict)

        db_session.commit()

        return jsonify(_parse_meal(update_meal.first()))
Esempio n. 16
0
    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))
Esempio n. 17
0
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')