Exemplo n.º 1
0
def update_profile(request):
    if request.method == 'POST':
        if request.body is None or len(request.body.decode('utf-8')) == 0:
            return JsonResponse(data=create_resp_dict(False,
                                                      INCORRECT_REQUEST),
                                safe=False,
                                status=HTTPStatus.BAD_REQUEST)
        else:
            try:
                print(client)
                body_data = json.loads(request.body.decode('utf-8'))
                # todo verify auth token
                auth_token = body_data['auth_token']
                user_id = body_data['user_id']
                user_data = body_data['user']
                user = User.objects(id=user_id).first()
                print(user.mobile)
                for key in user_data:
                    user[str(key)] = user_data[str(key)]
                user.save()
                return JsonResponse(data=create_resp_dict(True, USER_UPDATED),
                                    safe=False,
                                    status=HTTPStatus.OK)
            except Exception as e:
                return JsonResponse(data=create_resp_dict(False, e),
                                    safe=False,
                                    status=HTTPStatus.OK)
Exemplo n.º 2
0
def profile(request):
    if request.method == 'POST':
        if request.body is None or len(request.body.decode('utf-8')) == 0:
            return JsonResponse(data=create_resp_dict(False,
                                                      INCORRECT_REQUEST),
                                safe=False,
                                status=HTTPStatus.BAD_REQUEST)
        else:
            try:
                body_data = json.loads(request.body.decode('utf-8'))
                # todo verify auth token
                auth_token = body_data['auth_token']

                user_id = body_data['user_id']
                user = User.objects(id=user_id).first()
                user_details = create_user_dict(user)
                resp = create_resp_dict(True, USER_FETCHED)
                resp['user'] = user_details
                return JsonResponse(data=resp,
                                    safe=False,
                                    status=HTTPStatus.OK)
            except Exception as e:
                return JsonResponse(data=create_resp_dict(False, e),
                                    safe=False,
                                    status=HTTPStatus.INTERNAL_SERVER_ERROR)
Exemplo n.º 3
0
def handle_super_category(request):
    if request.method == 'POST':
        if request.body is None or len(request.body.decode('utf-8')) == 0:
            return JsonResponse(data=create_resp_dict(False,
                                                      INCORRECT_REQUEST),
                                safe=False,
                                status=HTTPStatus.BAD_REQUEST)
        else:
            try:
                print(client)
                body_data = json.loads(request.body.decode('utf-8'))
                name = body_data['name']
                description = body_data['description']
                default_view_count = body_data['default_view_count']
                category = SuperCategory()
                for i in name:
                    category.name[i] = name[i]
                for j in description:
                    category.description[j] = description[j]
                category.default_view_count = default_view_count
                category.save()
                resp = create_resp_dict(True, CATEGORY_ADDED)
                return JsonResponse(data=resp,
                                    safe=False,
                                    status=HTTPStatus.OK)
            except Exception as e:
                return JsonResponse(data=create_resp_dict(False, e),
                                    safe=False,
                                    status=HTTPStatus.OK)
Exemplo n.º 4
0
def job(request):
    if request.method == 'POST':
        if request.body is None or len(request.body.decode('utf-8')) == 0:
            return JsonResponse(data=create_resp_dict(False, INCORRECT_REQUEST), safe=False,
                                status=HTTPStatus.BAD_REQUEST)
        else:
            try:
                print(client)
                body_data = json.loads(request.body.decode('utf-8'))
                # todo verify auth token
                auth_token = body_data['auth_token']
                user_id = body_data['user_id']
                comment = body_data['comment']
                radius = body_data['radius']
                location = body_data['location']
                category_id = body_data['category_id']
                customer = User.objects(id=user_id).first()
                location = create_point_dict(location['latitude'], location['longitude'])
                created_at = datetime.utcnow()
                request = Request(category_id=category_id, location=location, user_id=user_id,
                                  created_at=created_at, mobile=customer.mobile, radius=radius)
                if comment is not None:
                    request['comment'] = comment
                request.save()
                resp_data = create_resp_dict(True, REQUEST_CREATED)
                resp_data['requestId'] = str(request.id)
                return JsonResponse(data=resp_data, safe=False, status=HTTPStatus.OK)
            except Exception as e:
                return JsonResponse(data=create_resp_dict(False, e), safe=False,
                                    status=HTTPStatus.INTERNAL_SERVER_ERROR)
Exemplo n.º 5
0
def work_requests(request):
    if request.method == 'POST':
        if request.body is None or len(request.body.decode('utf-8')) == 0:
            return JsonResponse(data=create_resp_dict(False, INCORRECT_REQUEST), safe=False,
                                status=HTTPStatus.BAD_REQUEST)
        else:
            try:
                print(client)
                body_data = json.loads(request.body.decode('utf-8'))
                # todo verify auth token
                auth_token = body_data['auth_token']
                user_id = body_data['user_id']
                location = body_data['location']
                radius_in_km = body_data['radius']
                radius_in_radian = radius_in_km/6378
                # print(radius_in_radian)
                resp_data = create_resp_dict(True, WORK_REQUEST_FETCHED)
                resp_data['workrequests'] = []
                user = User.objects(id=user_id).first()
                # print(user.work_category)
                workrequests = Request.objects(
                    location__geo_within_center=[(location['latitude'], location['longitude']), radius_in_radian], category_id=user.work_category)
                for i in workrequests:
                    user = User.objects(id=i.user_id).first()
                    workrequest = request_json_for_workrequests(i)
                    workrequest['title'] = 'Request from {}'.format(user.name)
                    resp_data['workrequests'].append(workrequest)
                resp_data['location_text'] = LOCATIONS_TEXT
                resp_data['location_subtext'] = LOCATIONS_SUBTEXT
                return JsonResponse(data=resp_data, safe=False, status=HTTPStatus.OK)
            except Exception as e:
                return JsonResponse(data=create_resp_dict(False, e), safe=False,
                                    status=HTTPStatus.INTERNAL_SERVER_ERROR)
Exemplo n.º 6
0
def handle_category(request):
    if request.method == 'POST':
        if request.body is None or len(request.body.decode('utf-8')) == 0:
            return JsonResponse(data=create_resp_dict(False,
                                                      INCORRECT_REQUEST),
                                safe=False,
                                status=HTTPStatus.BAD_REQUEST)
        else:
            try:
                body_data = json.loads(request.body.decode('utf-8'))
                category_id = body_data['category_id']
                name = body_data['name']
                description = body_data['description']
                icon_url = body_data['icon_url']
                has_questions = body_data['has_questions']
                new_category = Category(icon_url=icon_url,
                                        has_questions=has_questions)
                for i in name:
                    new_category.name[i] = name[i]
                for j in description:
                    new_category.description[j] = description[j]
                new_category.save()
                return JsonResponse(data=create_resp_dict(
                    True, SUBCATEGORY_ADDED),
                                    safe=False,
                                    status=HTTPStatus.OK)
            except Exception as e:
                return JsonResponse(data=create_resp_dict(False, e),
                                    safe=False,
                                    status=HTTPStatus.OK)
Exemplo n.º 7
0
def super_category_list(request):
    if request.method == 'POST':
        if request.body is None or len(request.body.decode('utf-8')) == 0:
            return JsonResponse(data=create_resp_dict(False,
                                                      INCORRECT_REQUEST),
                                safe=False,
                                status=HTTPStatus.BAD_REQUEST)
        else:
            try:
                body_data = json.loads(request.body.decode('utf-8'))
                user_language = body_data['user_language'].lower()
                user_type = body_data['user_type'].lower()
                # if (user_type=='consumer'):
                list = SuperCategory.objects
                super_categories = []
                for i in list:
                    temp = request_json(request=i, user_language=user_language)
                    super_categories.append(temp)
                resp = create_resp_dict(True, DATA_FETCHED)
                resp['super_categories'] = super_categories
                return JsonResponse(data=resp, safe=True, status=HTTPStatus.OK)
            except Exception as e:
                return JsonResponse(data=create_resp_dict(False, e),
                                    safe=False,
                                    status=HTTPStatus.OK)
Exemplo n.º 8
0
def add_language(request):
    if request.method == 'POST':
        if request.body is None or len(request.body.decode('utf-8')) == 0:
            return JsonResponse(data=create_resp_dict(False, INCORRECT_REQUEST), safe=False,
                                status=HTTPStatus.BAD_REQUEST)
        else:
            try:
                print(client)
                body_data = json.loads(request.body.decode('utf-8'))
                language_data = body_data['language_data']
                for data in language_data:
                    lang = Language.objects(text=data['text']).first()
                    lang.update(**data).save()
                return JsonResponse(data=create_resp_dict(True, LANGUAGE_ADDED), safe=False, status=HTTPStatus.OK)
            except Exception as e:
                return JsonResponse(data=create_resp_dict(False, e), safe=False, status=HTTPStatus.OK)
Exemplo n.º 9
0
def auth(request):
    if request.method == 'POST':
        if request.body is None or len(request.body.decode('utf-8')) == 0:
            return JsonResponse(data=create_resp_dict(False,
                                                      INCORRECT_REQUEST),
                                safe=False,
                                status=HTTPStatus.BAD_REQUEST)
        else:
            try:
                body_data = json.loads(request.body.decode('utf-8'))
                mobile = body_data['mobile']
                otp = body_data['otp']
                name = body_data['name']
                user_type = body_data['user_type']
                user_language = body_data['user_language']
                user = User.objects(mobile=mobile)[0]
                print(mobile)
                if user.otp == otp:
                    # user = json.loads(user)
                    auth_token = jwt.encode(payload={
                        'id': str(user.id),
                        'num': str(user.mobile)
                    },
                                            key=settings.SECRET_KEY,
                                            algorithm='HS256')
                    # user = User(mobile=mobile, name=name, user_type=user_type, language=language)
                    # user.save()
                    user.name = name
                    user.user_type = user_type.lower()
                    user.user_language = user_language.lower()
                    user.save()
                    resp_data = create_resp_dict(True, AUTH_SUCCESS)
                    resp_data['auth_token'] = auth_token.decode('utf-8')
                    resp_data['user_id'] = str(user.id)
                    return JsonResponse(data=resp_data,
                                        safe=False,
                                        status=HTTPStatus.OK)
                else:
                    return JsonResponse(data=create_resp_dict(
                        False, AUTH_FAIL),
                                        safe=False,
                                        status=HTTPStatus.UNAUTHORIZED)
            except Exception as e:
                return JsonResponse(data=create_resp_dict(False, e),
                                    safe=False,
                                    status=HTTPStatus.INTERNAL_SERVER_ERROR)
Exemplo n.º 10
0
def create_language(request):
    if request.method == 'POST':
        if request.body is None or len(request.body.decode('utf-8')) == 0:
            return JsonResponse(data=create_resp_dict(False, INCORRECT_REQUEST), safe=False,
                                status=HTTPStatus.BAD_REQUEST)
        else:
            try:
                print(client)
                body_data = json.loads(request.body.decode('utf-8'))
                language_data = body_data['language_data']
                for data in language_data:
                    # print(data['english'])
                    Language(text=data['text'], english=data['english']).save()

                return JsonResponse(data=create_resp_dict(True, CATEGORY_ADDED), safe=False, status=HTTPStatus.OK)
            except Exception as e:
                return JsonResponse(data=create_resp_dict(False, e), safe=False, status=HTTPStatus.OK)
Exemplo n.º 11
0
def login(request):
    if request.method == 'POST':
        if request.body is None or len(request.body.decode('utf-8')) == 0:
            return JsonResponse(data=create_resp_dict(False,
                                                      INCORRECT_REQUEST),
                                safe=False,
                                status=HTTPStatus.BAD_REQUEST)
        else:
            try:
                body_data = json.loads(request.body.decode('utf-8'))
                mobile = body_data['mobile']
                user_query = User.objects(mobile=mobile)
                print(mobile)
                print(len(user_query))
                if len(user_query) == 0:
                    # todo generate a random otp and message the user
                    user = User(mobile=mobile)
                    user.save()
                    resp = create_resp_dict(True, OTP_GENERATED)
                    resp['newuser'] = True
                    return JsonResponse(data=resp,
                                        safe=False,
                                        status=HTTPStatus.OK)
                elif len(user_query[0].name) == 0 or len(
                        user_query[0].user_type) == 0 or len(
                            user_query[0].user_language) == 0:
                    resp = create_resp_dict(True, OTP_GENERATED)
                    resp['newuser'] = True
                    return JsonResponse(data=resp,
                                        safe=False,
                                        status=HTTPStatus.OK)
                else:
                    resp = create_resp_dict(True, USER_EXISTS)
                    resp['newuser'] = False
                    resp['user'] = create_user_dict(user_query[0])
                    return JsonResponse(data=resp,
                                        safe=False,
                                        status=HTTPStatus.OK)

            except Exception as e:
                return JsonResponse(data=create_resp_dict(False, e),
                                    safe=False,
                                    status=HTTPStatus.OK)
Exemplo n.º 12
0
def language_data(request):
    if request.method == 'POST':
        if request.body is None and len(request.body.decode('utf-8'))==0:
            return JsonResponse(data=create_resp_dict(False, INCORRECT_REQUEST), safe=False,
                                status=HTTPStatus.BAD_REQUEST)
        else:
            try:
                body_data = json.loads(request.body.decode('utf-8'))
                user_language = body_data['user_language'].lower()
                # resp = create_resp_dict(True, DATA_FETCHED)
                if user_language=='english':
                    resp = create_resp_dict(True, DATA_FETCHED)
                    resp['user_language'] = data['english']
                elif user_language=='hindi':
                    resp = create_resp_dict(True, DATA_FETCHED)
                    resp['user_language'] = data['hindi']
                else:
                    resp = create_resp_dict(False, LANGUAGE_NOT_AVAILABLE)
                return JsonResponse(data=resp, safe=False, status=HTTPStatus.OK)
            except Exception as e:
                return JsonResponse(data=create_resp_dict(False, e), safe=False, status=HTTPStatus.OK )
Exemplo n.º 13
0
def my_request(request):
    if request.method == 'POST':
        if request.body is None or len(request.body.decode('utf-8')) == 0:
            return JsonResponse(data=create_resp_dict(False, INCORRECT_REQUEST), safe=False,
                                status=HTTPStatus.BAD_REQUEST)
        else:
            try:
                print(client)
                body_data = json.loads(request.body.decode('utf-8'))
                # todo verify auth token
                auth_token = body_data['auth_token']
                ret = []
                user_id = body_data['user_id']
                request = Request.objects(user_id=user_id)
                resp_data = create_resp_dict(True, REQUEST_FETCHED)
                for i in request:
                    temp = request_json_for_myrequests(i)
                    # how to fetch all the categories with particular category_id
                    ret.append(temp)
                resp_data['myrequests'] = ret
                return JsonResponse(data=resp_data, safe=False, status=HTTPStatus.OK)
            except Exception as e:
                return JsonResponse(data=create_resp_dict(False, e), safe=False,
                                    status=HTTPStatus.INTERNAL_SERVER_ERROR)