Beispiel #1
0
def _req_field_exists_in_db(model, field_name, field_name_in_db, field_type):
    field = get_val_from_req(field_name)
    if not field_type:
        res = dbutils.exists(model, {field_name_in_db: field})
    else:
        res = dbutils.exists(model, {field_name_in_db: field_type(field)})
    logger.debug('Checking if {}:{}:{} exists: {}'.format(
        model.__name__, field_name_in_db, field, res))
    return res
Beispiel #2
0
def create_booking(request):
    try:
        house_id = request.POST["house_id"]
        user_id = get_current_user_id(request)
        date_begin = request.POST["date_begin"]
        date_end = request.POST["date_end"]
    except KeyError as e:
        return JsonResponse(
            {
                "success": 0,
                "msg": error_msg.MSG_400 + ": {}".format(e)
            },
            status=400)

    if isinstance(date_begin, str):
        date_begin = str_to_datetime(date_begin)

    if isinstance(date_end, str):
        date_end = str_to_datetime(date_end)

    house = House.objects.get(pk=int(house_id))

    if exists(
            Booking, **{
                "house": house,
                "date_begin": date_begin,
                "date_end": date_end
            }):
        return JsonResponse({
            "success": 0,
            "msg": error_msg.HAS_ALREADY_BOOKED
        },
                            status=404)
    else:
        book = Booking(house=house,
                       user_id=user_id,
                       date_begin=date_begin,
                       date_end=date_end)
        if not book.date_is_valid():
            return JsonResponse(
                {
                    "success": 0,
                    "msg": error_msg.WRONG_DATE_BEGIN_END
                },
                status=400)

        if not check_if_this_time_can_book(house, date_begin, date_end):
            return JsonResponse(
                {
                    "success": 0,
                    "msg": error_msg.HAS_ALREADY_BOOKED
                },
                status=404)

        try:
            book.save()
        except ValidationError as e:
            return JsonResponse({"success": 0, "mgs": str(e)}, status=400)
        return JsonResponse({"success": 1, "info": {"book_id": str(book.pk)}})
Beispiel #3
0
 def wrapper(request, *args, **kwargs):
     s = request.session
     if 'user' not in s \
         or 'email' not in s['user'] \
         or not exists(User, email=s['user']['email']):
         return JsonResponse({
             "success": 0,
             'msg': error_msg.MSG_403
         },
                             status=403)
     return f(request, *args, **kwargs)
Beispiel #4
0
 def check():
     user = get_current_user()
     query = {'course_id': ObjectId(get_val_from_req('course_id'))}
     if user.type == 'teacher':
         query['teacher_id'] = user._id
         model = CourseToTeacher
         e = error_msg.TEACHER_NOT_IN_CLASS
     elif user.type == 'student':
         query['student_id'] = user._id
         model = CourseToStudent
         e = error_msg.STUDENT_NOT_IN_CLASS
     if not dbutils.exists(model, query):
         return jsonify({'success': 0, 'msg': e}), 403
     return f()
Beispiel #5
0
def register(request):
    email, password = request.POST["email"], request.POST["password"]
    if exists(User, **{"email": email}):
        logger.warning(
            "Failed to add new user: email {} already exists".format(email))
        return JsonResponse({
            "success": 0,
            "msg": error_msg.DUPLICATE_EMAIL
        },
                            status=409)

    try:
        user = User(email=email, password=password, type='user')
        user.full_clean()
        user.save()
    except ValidationError as e:
        return JsonResponse({
            'success': 0,
            'msg': error_msg.ILLEGAL_ARGUMENT
        },
                            status=400)

    return JsonResponse({"success": 1, "user": {"email": user.email}})
Beispiel #6
0
def create(request):
    try:
        name = request.POST["house_name"]
        place_id = request.POST["place_id"]
        address = request.POST["house_address"]
        city = request.POST["house_city"]
        province = request.POST["house_province"]
        postcode = request.POST["house_postcode"]
        longitude = request.POST["longitude"]
        latitude = request.POST["latitude"]
        coordinate = [float(longitude), float(latitude)]
        date_begin = request.POST["date_begin"]
        date_end = request.POST["date_end"]
        number_of_beds = request.POST["number_of_beds"]
        description = request.POST["description"]
        price = request.POST["price"]
    except KeyError as e:
        return JsonResponse(
            {
                "success": 0,
                "msg": error_msg.MSG_400 + ': {}'.format(e)
            },
            status=400)
    if exists(
            House, **{
                "name": name,
                "place_id": place_id,
                "address": address,
                "date_begin": date_begin,
                "date_end": date_end
            }):
        logger.warning(
            "Failed to add new house, house with name: {}, place_id: {}, address:{} and date range {} to {} already exists."
            .format(name, place_id, address, date_begin, date_end))
        return JsonResponse({"success": 0, "msg": error_msg.DUPLICATE_HOUSE})
    else:
        house = House(name=name,
                      place_id=place_id,
                      address=address,
                      city=city,
                      province=province,
                      postcode=postcode,
                      date_begin=date_begin,
                      date_end=date_end,
                      number_of_beds=number_of_beds,
                      description=description,
                      price=price)
        if not house.date_is_valid():
            return JsonResponse(
                {
                    "success": 0,
                    "msg": error_msg.WRONG_DATE_BEGIN_END
                },
                status=400)
        try:
            house.save()
        except ValidationError as e:
            return JsonResponse({"success": 0, "msg": str(e)}, status=400)
        res = geo_info_save(db, house.pk, place_id, coordinate)
        if isinstance(res, ObjectId):
            return JsonResponse({
                "success": 1,
                "info": {
                    "house_id": str(house.pk),
                    "geo_object_id": str(res)
                }
            })
        else:
            logger.error(
                "Fail to save geo info of house: {}. Something wrong with MongoDB."
                .format(name))
            return JsonResponse({"success": 0}, status=500)