コード例 #1
0
ファイル: views.py プロジェクト: michalziolecki/team-pizza
def order_options_view(request: WSGIRequest):
    logger: Logger = logging.getLogger(settings.LOGGER_NAME)
    user = request.user
    context = {'user': user}
    if request.method == 'GET' and user.is_authenticated:
        db_user = get_user_from_db(user.username)
        if db_user:
            try:
                opened_orders = Order.objects.filter(is_open=True).order_by('prediction_order_time')
                for to_close_ord in [order for order in opened_orders if order.prediction_order_time < timezone.now()]:
                    close_after_deadline(to_close_ord.hash_id)
                context['order_list'] = Order.objects.filter(is_open=True).order_by('prediction_order_time')
                context['close_order_list'] = Order.objects.filter(is_open=False).order_by('close_time')[:5]
            except DB_Error as db_err:
                logger.error(f'Getting orders from database failed ! info: {db_err.args}')
                context['bad_param'] = 'Problem with database try again'
            except BaseException as be:
                logger.error(f'Getting orders from database failed !'
                             f' Base exception cached info: {be.args}')
                context['bad_param'] = 'Problem with database try again'

        return render(request, 'OrderApp/order-options-view.html', context)
    elif not user.is_authenticated:
        return render(request, 'TeamPizza/not-authenticated.html', context, status=401)
    else:
        return render(request, 'TeamPizza/bad-method.html', context, status=400)
コード例 #2
0
ファイル: views.py プロジェクト: michalziolecki/team-pizza
def create_order(request: WSGIRequest):
    logger: Logger = logging.getLogger(settings.LOGGER_NAME)
    user = request.user
    context = {'user': user}
    if request.method == 'POST' and user.is_authenticated and user.role != 'U':
        predicted_datetime = ''
        description = ''

        try:
            post_body: QueryDict = request.POST
            params: dict = post_body.dict()
            predicted_datetime = params['predict-order-time']
            description = params['description']
        except KeyError as ke:
            logger.error(f'Key error while user try create order, probably someone change form!'
                         f'  info: {ke.args}')

        db_user: PizzaUser = get_user_from_db(user.username)
        parsed_datetime = timezone.make_aware(datetime.strptime(predicted_datetime, "%Y-%m-%dT%H:%M"))
        actual_time = timezone.now()

        if description == 'Your order description...':
            description = ''

        if actual_time < parsed_datetime and db_user:
            try:
                hash_id: str = hashlib.sha1(os.urandom(HASH_IDS_LENGTH)).hexdigest()
                logger.debug(f'Hash_id created!  "{hash_id}" in type {type(hash_id)}, length {len(hash_id)}')
                order = Order(order_owner=db_user,
                              hash_id=hash_id,
                              prediction_order_time=parsed_datetime,
                              description=description,
                              is_open=True,
                              open_time=actual_time)
                order.save()
                logger.debug(f'Order added! by user: {db_user.username}')
                return redirect('/order/options/')
            except DB_Error as db_err:
                logger.error(f'Create order by user {user.username} failed ! info: {db_err.args}')
                context['bad_param'] = 'Problem with database try again'
            except BaseException as be:
                logger.error(f'Create order by user {user.username} failed ! Base exception cached info: {be.args}')
                context['bad_param'] = 'Problem with database try again'
        else:
            context['bad_param'] = 'Date is incorrect'

        return render(request, 'OrderApp/order-options-view.html', context)

    elif not user.is_authenticated or user.role == 'U':
        return render(request, 'TeamPizza/not-authenticated.html', context, status=401)
    else:
        return render(request, 'TeamPizza/bad-method.html', context, status=400)
コード例 #3
0
ファイル: views.py プロジェクト: michalziolecki/team-pizza
def update_order_post(request: WSGIRequest, hash_id: str):
    logger: Logger = logging.getLogger(settings.LOGGER_NAME)
    user = request.user
    context = {'user', user}
    predicted_datetime = ''
    description = ''

    try:
        post_body: QueryDict = request.POST
        params: dict = post_body.dict()
        predicted_datetime = params['predict-order-time']
        description = params['description']
    except KeyError as ke:
        logger.error(f'Key error while user try create order, probably someone change form!'
                     f'  info: {ke.args}')

    # if description == 'Your order description...':
    #     description = ''
    db_user: PizzaUser = get_user_from_db(user.username)
    parsed_datetime = timezone.make_aware(datetime.strptime(predicted_datetime, "%Y-%m-%dT%H:%M"))
    actual_time = timezone.now()

    if actual_time < parsed_datetime and db_user:
        try:
            order = Order.objects.filter(hash_id=hash_id).get()
            if order and order.is_open and order.order_owner.id == user.id:
                Order.objects.filter(hash_id=hash_id).update(
                    prediction_order_time=parsed_datetime,
                    description=description,
                    open_time=actual_time)
                logger.debug(f'Order updated! by user: {db_user.username}')
                return redirect('/order/options/')
            elif not order.is_open:
                context['bad_param'] = f'Order has been closed at {order.close_time}'
                return render(request, 'TeamPizza/not-authenticated.html', context)
            else:
                return render(request, 'TeamPizza/not-authenticated.html', status=401)
        except DB_Error as db_err:
            logger.error(f'Update order by user {user.username} failed ! info: {db_err.args}')
            context['bad_param'] = 'Problem with database try again'
        except BaseException as be:
            logger.error(f'Update order by user {user.username} failed ! Base exception cached info: {be.args}')
            context['bad_param'] = 'Problem with database try again'
        return render(request, 'OrderApp/order-not-exist.html', context, status=404)
    else:
        context['bad_param'] = 'Date is incorrect'

    return render(request, 'OrderApp/order-update.html', context)
コード例 #4
0
ファイル: views.py プロジェクト: michalziolecki/team-pizza
def opened_order_view(request: WSGIRequest, hash_id: str):
    logger: Logger = logging.getLogger(settings.LOGGER_NAME)
    user = request.user
    context = {'user': user}

    if request.method == 'GET' and user.is_authenticated:
        logger.debug('Get method opened_order_view')
        db_user = get_user_from_db(user.username)
        if db_user:
            try:
                order = Order.objects.filter(hash_id=hash_id).get()
                actual_time = timezone.now()
                if order.prediction_order_time < actual_time:
                    closed_order = close_after_deadline(hash_id=hash_id)
                    if closed_order:
                        order = closed_order
                contributions = ContributionOrder.objects.filter(order=order).order_by('add_contr_time')
                small_pieces = 0
                big_pieces = 0
                other_meal = 0
                logger.debug(f'contributions size: {len(contributions)}')
                for piece in [contribute for contribute in contributions if contribute.ord_type == 'B']:
                    big_pieces += piece.number
                for piece in [contribute for contribute in contributions if contribute.ord_type == 'S']:
                    small_pieces += piece.number
                for other in [contribute for contribute in contributions if contribute.ord_type == 'O']:
                    other_meal += other.number
                context['contributions'] = contributions
                context['order'] = order
                context['small_pieces'] = small_pieces
                context['big_pieces'] = big_pieces
                context['all_pieces'] = small_pieces + big_pieces
                context['other_meal'] = other_meal
            except DB_Error as db_err:
                logger.error(f'Getting order "{hash_id}" from database failed ! info: {db_err.args}')
                context['bad_param'] = 'Problem with database try again'
            except BaseException as be:
                logger.error(f'Getting order "{hash_id}" from database failed !'
                             f' Base exception cached info: {be.args}')
                context['bad_param'] = 'Problem with database try again'

        return render(request, 'OrderApp/opened-order-view.html', context)
    elif not user.is_authenticated:
        return render(request, 'TeamPizza/not-authenticated.html', context, status=401)
    else:
        return render(request, 'TeamPizza/bad-method.html', context, status=400)
コード例 #5
0
ファイル: views.py プロジェクト: michalziolecki/team-pizza
def join_order(request: WSGIRequest, hash_id: str):
    logger: Logger = logging.getLogger(settings.LOGGER_NAME)
    user = request.user
    context = {'user': user, 'hash_id': hash_id}
    if request.method == 'POST' and user.is_authenticated:
        pieces = ''
        ord_type = ''
        description = ''

        try:
            post_body: QueryDict = request.POST
            params: dict = post_body.dict()
            pieces = params['pieces']
            ord_type = params['ord_type']
            description = params['description']
        except KeyError as ke:
            logger.error(f'Key error while user try join to order, probably someone change form!'
                         f'  info: {ke.args}')

        db_user: PizzaUser = get_user_from_db(user.username)

        if description == 'Your order description...':
            description = ''

        pieces_int = 0
        if pieces:
            try:
                pieces_int = int(pieces)
            except ValueError as ve:
                logger.error(f'Value error while user try join to order, number of pieces was not integer!'
                             f'  info: {ve.args}')

        if user_max_joined(hash_id, db_user):
            context['bad_param'] = 'You have already joined to many times (10 max)'
        elif pieces_int > 0 and ord_type and db_user:
            try:
                order = Order.objects.filter(hash_id=hash_id).get()
                actual_time = timezone.now()
                if order.prediction_order_time < actual_time:
                    order = close_after_deadline(hash_id=hash_id)
                contribution = ContributionOrder(
                    contribution_owner=db_user,
                    order=order,
                    number=int(pieces),
                    ord_type=ord_type,
                    add_contr_time=timezone.now(),
                    was_updated=False,
                    description=description
                )
                if order is not None and order.is_open:
                    logger.debug(f'contribution order type before save {contribution.ord_type}')
                    contribution.save()
                    logger.debug(f'contribution order type after save {contribution.ord_type}')
                    logger.debug(f'User: {db_user.username} join to order "{hash_id}"! ')
                    return redirect(f'/order/preview-order/{hash_id}/')
                else:
                    context['bad_param'] = 'This order is closed!'
            except DB_Error as db_err:
                logger.error(f'Create order by user {user.username} failed ! info: {db_err.args}')
                context['bad_param'] = 'Problem with database try again'
            except BaseException as be:
                logger.error(f'Create order by user {user.username} failed ! Base exception cached info: {be.args}')
                context['bad_param'] = 'Problem with database try again'
        else:
            context['bad_param'] = 'Data is incorrect'
        return render(request, 'OrderApp/join-order-view.html', context)
    elif not user.is_authenticated:
        return render(request, 'TeamPizza/not-authenticated.html', context, status=401)
    else:
        return render(request, 'TeamPizza/bad-method.html', context, status=400)