Example #1
0
    def token_check(request, *args, **kwargs):

        requtils = RequestUtils()
        auth = LemariSidikModule()
        rg = ResponseGenerator()

        # Check the token
        try:
            token = requtils.get_access_token(request)
        except KeyError:
            response_data = rg.error_response_generator(
                1, 'Please define the authorization header')
            return HttpResponse(json.dumps(response_data),
                                content_type='application/json',
                                status=401)
        else:
            if token['token'] != None:
                user = auth.get_user_data(token['token'])
                # Check token validity
                if user['user_uuid'] != None:
                    # Return the user object too
                    return function(request, user, *args, **kwargs)
                else:
                    response_data = rg.error_response_generator(
                        1, user['error'])
                    return HttpResponse(json.dumps(response_data),
                                        content_type='application/json',
                                        status=401)
            else:
                response_data = rg.error_response_generator(1, token['error'])
                return HttpResponse(json.dumps(response_data),
                                    content_type="application/json",
                                    status=401)
Example #2
0
def get_device_exported_xlsx(request, user):

    # Gudang class
    rg = ResponseGenerator()
    db = LemariMongoDB()

    exported_xlsxs = db.get_user_exported_xlsx(user_uuid=user['user_uuid'])
    data = {
        'user_uuid': user['user_uuid'],
        'device_exported_xlsx': [],
        'device_exported_xlsx_count': exported_xlsxs.count(True)
    }
    for exported_xlsx in exported_xlsxs:
        device = db.get_device_by_device_uuid(
            device_uuid=exported_xlsx['device_uuid'],
            user_uuid=user['user_uuid'])
        # Remove the id
        exported_xlsx.pop('_id')
        exported_xlsx['device_name'] = device['device_name']
        data['device_exported_xlsx'].append(exported_xlsx)

    # Generate response object
    response_data = rg.data_response_generator(data)
    return HttpResponse(json.dumps(response_data),
                        content_type='application/json',
                        status=200)
Example #3
0
    def get_check(request, *args, **kwargs):
        # Gudang class
        rg = ResponseGenerator()

        if request.method == "GET":
            return function(request, *args, **kwargs)
        else:
            response_data = rg.error_response_generator(
                400, 'Bad request method')
            return HttpResponse(json.dumps(response_data),
                                content_type='application/json',
                                status=400)
Example #4
0
def retrieve_user_wifi_connection_list(request, user):

    # Gudang class
    rg = ResponseGenerator()
    db = LemariMongoDB()

    # Get all conenction detail
    wifi_connections = db.get_user_wifi_connection(user_uuid=user['user_uuid'])
    data = {
        'user_uuid': user['user_uuid'],
        'wifi_connections': [],
        'wifi_connection_count': wifi_connections.count(True)
    }

    # Append to data
    for wifi_connection in wifi_connections:
        # Security enabled
        security_enabled = '0'
        if wifi_connection['security_enabled']:
            security_enabled = '1'

        wifi_connection_data = {
            'user_wifi_connection_uuid':
            wifi_connection['user_wifi_connection_uuid'],
            'connection_name':
            wifi_connection['connection_name'],
            'ssid':
            wifi_connection['ssid'],
            'security_enabled':
            security_enabled,
            'password':
            wifi_connection['password'],
            'time_updated':
            wifi_connection['time_updated']
        }
        data['wifi_connections'].append(wifi_connection_data)

    # Generate response object
    response_data = rg.data_response_generator(data)
    return HttpResponse(json.dumps(response_data),
                        content_type='application/json',
                        status=200)
Example #5
0
def user_profile(request, user):

    # Lemari classes
    rg = ResponseGenerator()
    db = LemariDynamoDB()

    try:
        user_profile = db.get_user_profile(user['user_uuid'])
    except:
        # for unknown internal server error
        response_data = rg.error_response_generator(500,
                                                    "Internal server error")
        return HttpResponse(json.dumps(response_data),
                            content_type="application/json",
                            status=500)
    else:
        # add aditional data
        user_profile['email'] = user['email']
        response_data = rg.data_response_generator(user_profile)
        return HttpResponse(json.dumps(response_data),
                            content_type="application/json",
                            status=200)
Example #6
0
def get_device_dashboard_chart(request, user):

    # Gudang class
    rg = ResponseGenerator()
    db = LemariMongoDB()

    device_dashboard_charts = db.get_dashboard_chart_by_user_uuid(
        user_uuid=user['user_uuid'])
    data = {
        'device_dashboard_charts': [],
        'device_dashboard_chart_count': device_dashboard_charts.count(True)
    }
    for device_dashboard_chart in device_dashboard_charts:
        # Remove unnecessary key
        device_dashboard_chart.pop('_id')
        device_dashboard_chart.pop('user_uuid')
        data['device_dashboard_charts'].append(device_dashboard_chart)

    response_data = rg.data_response_generator(data)
    return HttpResponse(json.dumps(response_data),
                        content_type="application/json",
                        status=200)
Example #7
0
def add_user_wifi_connection(request, user):

    # Gudang class
    rg = ResponseGenerator()
    db = LemariMongoDB()

    form = AddUserWifiConnectionForm(request.POST)
    if form.is_valid():

        # Check for security_enabled
        raw_security_enabled = form.cleaned_data['security_enabled']
        if raw_security_enabled == '1':
            if 'password' in form.cleaned_data:
                password = form.cleaned_data['password']
                security_enabled = True
            else:
                response_data = rg.error_response_generator(
                    400, 'Please spesify SSID password')
                return HttpResponse(json.dumps(response_data),
                                    content_type='application/json',
                                    status=400)

        elif raw_security_enabled == '0':
            security_enabled = False
            password = '******'
        else:
            response_data = rg.error_response_generator(
                400, 'invalid or missing parameter submitted')
            return HttpResponse(json.dumps(response_data),
                                content_type='application/json',
                                status=400)

        # Construct user wifi connection structure
        user_wifi_connection = {
            'user_wifi_connection_uuid': uuid4().hex,
            'user_uuid': user['user_uuid'],
            'connection_name': form.cleaned_data['connection_name'],
            'ssid': form.cleaned_data['ssid'],
            'security_enabled': security_enabled,
            'password': password,
            'time_updated': datetime.now().timestamp(),
            'removed': False
        }

        # Put in db
        db.put_user_wifi_connection(user_wifi_connection=user_wifi_connection)

        # Generate response object
        response_data = rg.success_response_generator(
            200, 'Wifi connection successfully added')
        return HttpResponse(json.dumps(response_data),
                            content_type="application/json",
                            status=200)
    else:
        response_data = rg.error_response_generator(
            400, 'invalid or missing parameter submitted')
        return HttpResponse(json.dumps(response_data),
                            content_type='application/json',
                            status=400)
Example #8
0
def remove_user_wifi_connection(request, user, user_wifi_connection_uuid):

    # Gudang class
    rg = ResponseGenerator()
    db = LemariMongoDB()

    # Check user_wifi_connection_uuid
    user_wifi_connection = db.get_user_wifi_connection_by_uuid(
        user_uuid=user['user_uuid'],
        user_wifi_connection_uuid=user_wifi_connection_uuid)
    if user_wifi_connection != None:
        # Check if the connection is being used
        user_devices = db.get_user_devices_by_user_wifi_connection_uuid(
            user_uuid=user['user_uuid'],
            user_wifi_connection_uuid=user_wifi_connection_uuid)
        if user_devices.count(True) == 0:
            db.remove_user_wifi_connection_by_uuid(
                user_wifi_connection_uuid=user_wifi_connection_uuid)
            # Generate response object
            response_data = rg.success_response_generator(
                200, 'Wifi connection successfully removed')
            return HttpResponse(json.dumps(response_data),
                                content_type="application/json",
                                status=200)
        else:
            # Generate response object
            response_data = rg.error_response_generator(
                400, 'Wifi connection is being used by one or more device')
            return HttpResponse(json.dumps(response_data),
                                content_type="application/json",
                                status=400)
    else:
        response_data = rg.error_response_generator(
            400, 'invalid user wifi connection uuid')
        return HttpResponse(json.dumps(response_data),
                            content_type='application/json',
                            status=400)
Example #9
0
def remove_device_dashboard_chart(request, user, user_dashboard_chart_uuid):

    # Gudang class
    rg = ResponseGenerator()
    db = LemariMongoDB()

    dashboard_chart = db.get_dashboard_chart_by_uuid(
        user_dashboard_chart_uuid=user_dashboard_chart_uuid,
        user_uuid=user['user_uuid'])
    if (dashboard_chart):
        db.remove_chart_by_chart_uuid(
            user_dashboard_chart_uuid=user_dashboard_chart_uuid)
        response_data = rg.success_response_generator(
            200, "Device dashboard chart successfully removed")
        return HttpResponse(json.dumps(response_data),
                            content_type="application/json",
                            status=200)

    else:
        response_data = rg.error_response_generator(
            400, 'Invalid user dashboard chart')
        return HttpResponse(json.dumps(response_data),
                            content_type='application/json',
                            status=400)
Example #10
0
def user_profile_image_update(request, user):
    # Lemari classes
    rg = ResponseGenerator()
    auth = LemariSidikModule()
    utils = LemariUtils()
    db = LemariDynamoDB()
    s3 = S3()
    requtils = RequestUtils()

    form = UpdateProfilePictureForm(request.POST, request.FILES)
    if form.is_valid():
        profile_image_file = form.cleaned_data['profile_image']
        file_extension = utils.get_file_type(profile_image_file)
        target_location = 'image/profile/{}/{}{}'.format(
            user['user_uuid'],
            uuid4().hex, file_extension)
        try:
            response = s3.put_object(RUMAHIOT_UPLOAD_BUCKET, target_location,
                                     profile_image_file.file.getvalue())
        except:
            # catch unknown error
            response_data = rg.error_response_generator(
                500, "Internal server error")
            return HttpResponse(json.dumps(response_data),
                                content_type="application/json",
                                status=500)
        else:
            file_location = 'https://s3-{}.amazonaws.com/{}/{}'.format(
                RUMAHIOT_REGION, RUMAHIOT_UPLOAD_BUCKET, target_location)
            try:
                db.update_user_profile_image(user['user_uuid'], file_location)
            except:
                # catch unknown error
                response_data = rg.error_response_generator(
                    500, "Internal server error")
                return HttpResponse(json.dumps(response_data),
                                    content_type="application/json",
                                    status=500)
            else:
                response_data = rg.success_response_generator(
                    200, "Profile picture successfully updated")
                return HttpResponse(json.dumps(response_data),
                                    content_type="application/json",
                                    status=200)
    else:
        response_data = rg.error_response_generator(400,
                                                    "Invalid image format")
        return HttpResponse(json.dumps(response_data),
                            content_type="application/json",
                            status=400)
Example #11
0
def user_profile_update(request, user):

    # Lemari classes
    rg = ResponseGenerator()
    db = LemariDynamoDB()

    form = UpdateProfileForm(request.POST)
    if form.is_valid():
        # Check for string in the number
        if form.cleaned_data['phone_number'].isdigit():
            # check if defined form field is submitted (check only for the field with required = False)
            # use '-' as business logic for None in DynamoDB
            if form.cleaned_data['phone_number'] == '':
                form.cleaned_data['phone_number'] = '-'
            # catch the exception if DynamoDB client got error
            try:
                db.update_user_profile(user['user_uuid'],
                                       form.cleaned_data['full_name'],
                                       form.cleaned_data['phone_number'])
            except:
                response_data = rg.error_response_generator(
                    500, "Internal server error")
                return HttpResponse(json.dumps(response_data),
                                    content_type="application/json",
                                    status=500)
            else:
                response_data = rg.success_response_generator(
                    200, "Profile successfully updated")
                return HttpResponse(json.dumps(response_data),
                                    content_type="application/json",
                                    status=200)
        else:
            response_data = rg.error_response_generator(
                400, "Phone Number must be number only")
            return HttpResponse(json.dumps(response_data),
                                content_type="application/json",
                                status=400)

    else:
        response_data = rg.error_response_generator(
            400, "Invalid or missing parameter submitted")
        return HttpResponse(json.dumps(response_data),
                            content_type="application/json",
                            status=400)
Example #12
0
def add_device_dashboard_chart(request, user):
    # Gudang class
    rg = ResponseGenerator()
    db = LemariMongoDB()

    try:
        # Verify the data format
        j = json.loads(request.body.decode('utf-8'))
        d = UserAdddashBoardChartResource(**j)
    except TypeError:
        response_data = rg.error_response_generator(
            400, "One of the request inputs is not valid")
        return HttpResponse(json.dumps(response_data),
                            content_type="application/json",
                            status=400)
    except ValueError:
        response_data = rg.error_response_generator(400, "Malformed JSON")
        return HttpResponse(json.dumps(response_data),
                            content_type="application/json",
                            status=400)
    else:
        device = db.get_device_by_device_uuid(device_uuid=d.device_uuid,
                                              user_uuid=user['user_uuid'])
        if (device):
            if (type(d.statistic_type) is
                    str) and d.statistic_type in VALID_STATISTIC_TYPE:
                if (d.statistic_type == '2'):
                    # Make sure it is less than equal 168 hour and more than equal 1 hour
                    if (type(d.n_last_hour) is int
                            and (d.n_last_hour >= 1 and d.n_last_hour <= 168)):
                        data = {
                            'user_dashboard_chart_uuid': uuid4().hex,
                            'device_uuid': d.device_uuid,
                            'user_uuid': user['user_uuid'],
                            'statistic_type': d.statistic_type,
                            'n_last_hour': d.n_last_hour
                        }
                        # put the data into db
                        db.put_user_device_dashboard_chart(data=data)
                        response_data = rg.success_response_generator(
                            200, "Device dashboard chart successfully added")
                        return HttpResponse(json.dumps(response_data),
                                            content_type="application/json",
                                            status=200)
                    else:
                        response_data = rg.error_response_generator(
                            400,
                            'Hour must be larger than 1 and less than 168')
                        return HttpResponse(json.dumps(response_data),
                                            content_type="application/json",
                                            status=400)
                else:
                    data = {
                        'user_dashboard_chart_uuid': uuid4().hex,
                        'device_uuid': d.device_uuid,
                        'user_uuid': user['user_uuid'],
                        'statistic_type': d.statistic_type,
                    }
                    # put the data into db
                    db.put_user_device_dashboard_chart(data=data)
                    response_data = rg.success_response_generator(
                        200, "Device dashboard chart successfully added")
                    return HttpResponse(json.dumps(response_data),
                                        content_type="application/json",
                                        status=200)

            else:
                response_data = rg.error_response_generator(
                    400, 'Invalid statistic type')
                return HttpResponse(json.dumps(response_data),
                                    content_type="application/json",
                                    status=400)
        else:
            response_data = rg.error_response_generator(
                400, 'Invalid device uuid')
            return HttpResponse(json.dumps(response_data),
                                content_type="application/json",
                                status=400)
Example #13
0
def update_user_wifi_connection(request, user):

    # Gudang class
    rg = ResponseGenerator()
    db = LemariMongoDB()

    form = UpdateUserWifiConnectionForm(request.POST)
    if form.is_valid():
        # Check user_wifi_connection_uuid
        user_wifi_connection = db.get_user_wifi_connection_by_uuid(
            user_uuid=user['user_uuid'],
            user_wifi_connection_uuid=form.
            cleaned_data['user_wifi_connection_uuid'])
        if user_wifi_connection != None:
            # Check for security_enabled
            raw_security_enabled = form.cleaned_data['security_enabled']
            if raw_security_enabled == '1':
                if 'password' in form.cleaned_data:
                    password = form.cleaned_data['password']
                    security_enabled = True
                else:
                    response_data = rg.error_response_generator(
                        400, 'Please spesify SSID password')
                    return HttpResponse(json.dumps(response_data),
                                        content_type='application/json',
                                        status=400)

            elif raw_security_enabled == '0':
                security_enabled = False
                password = '******'
            else:
                response_data = rg.error_response_generator(
                    400, 'invalid or missing parameter submitted')
                return HttpResponse(json.dumps(response_data),
                                    content_type='application/json',
                                    status=400)
            # Put in db
            db.update_user_wifi_connection(
                object_id=user_wifi_connection['_id'],
                new_connection_name=form.cleaned_data['connection_name'],
                new_ssid=form.cleaned_data['ssid'],
                new_security_enabled=security_enabled,
                new_password=password)
            # Generate response object
            response_data = rg.success_response_generator(
                200, 'Wifi connection successfully updated')
            return HttpResponse(json.dumps(response_data),
                                content_type="application/json",
                                status=200)
        else:
            response_data = rg.error_response_generator(
                400, 'invalid user wifi connection uuid')
            return HttpResponse(json.dumps(response_data),
                                content_type='application/json',
                                status=400)
    else:
        response_data = rg.error_response_generator(
            400, 'invalid or missing parameter submitted')
        return HttpResponse(json.dumps(response_data),
                            content_type='application/json',
                            status=400)