示例#1
0
def save_settings(request):

    #check for POST params specific to the alert type
    #alert_type = request.POST['alert_type']


    if 'alert_setting_id' not in request.POST:
        return render_json({'success': False, 'error_id': 1, 'error_msg': 'Data not set'})

    alert_setting_id = request.POST['alert_setting_id']


    user = get_current_user(request)
    if not user:
        return render_json({'success': False, 'error_id': 1, 'error_msg': 'User is not authenticated'})



    try:
        alert_setting = AlertSetting.objects.get(id=alert_setting_id)
    except:
        return render_json({'success': False, 'error_id': 2, 'error_msg':'alert_setting id ' + str(alert_setting_id) + ' doesnt exist'})


    print "is default alert setting id"
    print "alert setting id: " + str(alert_setting_id)
    print is_default_alert_setting_id(alert_setting_id)
    print 'alert setting user id'
    print alert_setting.user_id
    print 'user id'
    print user.id

    if not is_default_alert_setting_id(alert_setting_id) and alert_setting.user_id != user.id:
        return render_json({'success': False, 'error_id': 1, 'error_msg': 'Not a default setting or this users setting'})


    #check for the params that are required by all alert types
    required_post_params = ['in_animation', 'out_animation', 'font', 'font_size', 'font_weight', 'font_color', 'highlight_color']
    if not_in(required_post_params, request.POST):
        return render_json({'success': False, 'error_id': 3, 'error_msg': 'Required post params not set'})


    alert_type = alert_setting.type


    required_type_post_params = {
        'tip': ['donation_minimum', 'message_template'],
        'train': ['timer_seconds', 'box_color']
    }

    if alert_type not in required_type_post_params:
        return render_json({'success': False, 'error_id': 4, 'error_msg': alert_type + ' IS AN INVALID ALERT TYPE'})

    if not_in(required_type_post_params[alert_type], request.POST):
        return render_json({'success': False, 'error_id': 5, 'error_msg': 'All data is not set'})




    print "SAVE SETTINGS POST"
    print request.POST



    in_animation = request.POST['in_animation']
    out_animation = request.POST['out_animation']
    font = request.POST['font']
    font_size = request.POST['font_size']
    font_weight = request.POST['font_weight']
    font_color = request.POST['font_color']
    highlight_color = request.POST['highlight_color']
    image_id = alert_setting.image_id
    sound_id = alert_setting.sound_id
    message_template = alert_setting.message_template


    try:
        request.POST['donation_minimum'] = int(request.POST['donation_minimum'])
        if request.POST['donation_minimum'] < 0:
            return render_json({'success': False, 'error_id': 6, 'error_msg': 'Donation amount must be 0 or higher'})
    except:
        return render_json({'success': False, 'error_id': 7, 'error_msg': 'Error parsing donation_minimum'})


    if alert_type == 'tip':
        message_template = request.POST['message_template']
    elif alert_type == 'train':
        request.POST['timer_seconds'] = settings.GLOBALS['timer_seconds_default']
        # try:
        #     request.POST['timer_seconds'] = int(request.POST['timer_seconds'])
        #     if request.POST['timer_seconds'] < settings.GLOBALS['timer_seconds_min'] or request.POST['timer_seconds'] > settings.GLOBALS['timer_seconds_max']:
        #         return render_json({'success': False, 'error_id': 1, 'error_msg': 'Invalid timer_seconds. Must be between ' + str(settings.GLOBALS['timer_seconds_min']) + ' and ' + str(settings.GLOBALS['timer_seconds_max'])})
        # except:
        #     return render_json({'success': False, 'error_id': 1, 'error_msg': 'Error parsing timer_seconds'})

        if not valid_hex(request.POST['box_color']):
            return render_json({'success': False, 'error_id': 1, 'error_msg': 'Invalid hex for font color'})





    if in_animation not in settings.GLOBALS['in_animations']:
        return render_json({'success': False, 'error_id': 5, 'error_msg': 'Invalid in animation'})

    if out_animation not in settings.GLOBALS['out_animations']:
        return render_json({'success': False, 'error_id': 5, 'error_msg': 'Invalid out animation'})

    if font not in settings.GLOBALS['fonts']:
        return render_json({'success': False, 'error_id': 6, 'error_msg': 'Invalid font'})

    try:
        font_size = int(font_size)
        if font_size < settings.GLOBALS['font_size_min'] or font_size > settings.GLOBALS['font_size_max']:
            return render_json({'success': False, 'error_id': 1, 'error_msg': 'Font size too big or too small'})
    except:
        return render_json({'success': False, 'error_id': 1, 'error_msg': 'Invalid font size'})

    try:
        font_weight = int(font_weight)
        if font_weight < settings.GLOBALS['font_weight_min'] or font_weight > settings.GLOBALS['font_weight_max']:
            return render_json({'success': False, 'error_id': 1, 'error_msg': 'Font weight too small or too big'})
    except:
        print traceback.print_exc()


    if not valid_hex(font_color):
        return render_json({'success': False, 'error_id': 1, 'error_msg': 'Invalid hex for font color'})

    if not valid_hex(highlight_color):
        return render_json({'success': False, 'error_id': 1, 'error_msg': 'Invalid hex for highlight_color'})


    path = 'test/'
    file_data = {}
    if 'image_file' in request.FILES:
        file_data = file_upload(request=request, path=path, file_key_name='image_file', file_type='image')
        image_id = file_data['id']
        print "UPLOADED IMAGE FILE WITH FILE ID"
        print image_id
    elif 'image_url' in request.POST:
        image_url = request.POST['image_url']
        try:
            image_file = File.objects.get(file_url=image_url)
            image_id = image_file.id
        except:
            image_id = None
            #return render_json({'success': False, 'error_id': 1, 'error_msg': 'File with image_url ' + image_url + ' doesnt exist'})
    else:
        image_id = None
        delete_user_file(user, alert_setting.image_id)



    if 'sound_file' in request.FILES:
        file_data = file_upload(request=request, path=path, file_key_name='sound_file', file_type='sound')
        sound_id = file_data['id']
        print "UPLOADED SOUND FILE WITH FILE ID"
        print sound_id
    elif 'sound_url' in request.POST:
        sound_url = request.POST['sound_url']
        try:
            sound_file = File.objects.get(file_url=sound_url)
            sound_id = sound_file.id
        except:
            sound_id = None
            #return render_json({'success': False, 'error_id': 1, 'error_msg': 'File with sound_url ' + sound_url + ' doesnt exist'})
    else:
        sound_id = None
        delete_user_file(user, alert_setting.sound_id)


    # def add_type_data_to_alert_setting(alert_setting, alert_type, request):
    #     if alert_type == 'donation':
    #         alert_setting.donation_minimum = request.POST['donation_minimum']
    #     elif alert_type == 'train':
    #         try:
    #             alert_setting.train.box_color = request.POST['box_color']
    #             alert_setting.train.timer_seconds = request.POST['timer_seconds']
    #             alert_setting.train.save()
    #
    #             alert_setting.donation_minimum = request.POST['donation_minimum']
    #         except:
    #             print "TIMER_SETTING DOESNT EXIST FOR " + str(alert_setting.id)
    #             try:
    #                 alert_setting.train = TrainSetting.objects.create(alert_setting_id=alert_setting.id, timer_seconds=request.POST['timer_seconds'], box_color=request.POST['box_color'])
    #             except:
    #                 print traceback.print_exc()
    #                 print "ERROR CREATING TRAIN SETTING"
    #                 return False
    #
    #
    #     return alert_setting


    try:

        if is_default_alert_setting_id(alert_setting_id) or alert_setting.user_id != user.id:

            #check if an alert setting for this user with this type already exists
            try:
                alert_setting_check = AlertSetting.objects.get(user_id=user.id, type=alert_type)
                return render_json({'success': False, 'error_id': 11, 'error_msg': 'Not creating another alert setting for this type'})
            except:
                pass


            print "CREATING NEW ALERT SETTING"
            alert_setting = AlertSetting.objects.create(
                type=alert_type,
                user_id=user.id,
                in_animation=in_animation,
                out_animation=out_animation,
                font=font,
                font_size=font_size,
                font_weight=font_weight,
                font_color=font_color,
                highlight_color=highlight_color,
                image_id=image_id,
                sound_id=sound_id,
                message_template=message_template
            )




            get_or_create_type_setting_model(alert_setting, setting_data_dict=request.POST)

            alert_setting.save()

        else:
            print "REUSING ALERT SETTING"
            alert_setting.in_animation = in_animation
            alert_setting.out_animation = out_animation
            alert_setting.font = font
            alert_setting.font_size = font_size
            alert_setting.font_weight = font_weight
            alert_setting.font_color = font_color
            alert_setting.highlight_color = highlight_color
            alert_setting.image_id = image_id
            alert_setting.sound_id = sound_id
            alert_setting.message_template = message_template

            if alert_setting.type == 'train':
                if 'box_color' in request.POST:
                    train_setting = get_or_create_type_setting_model(alert_setting, setting_data_dict=request.POST)
                    print train_setting
                    print "SAVING TRAIN SETTING BOX COLOR"
                    print train_setting.box_color
                    train_setting.box_color = request.POST['box_color']
                    train_setting.save()
                    print train_setting.box_color


            alert_setting.save()

        return render_json({'success': True, 'alert_setting': model_to_dict(alert_setting)})
    except:
        print traceback.print_exc()
        return render_json({'success': False, 'error_id': 2, 'error_msg': 'Error saving alert setting', 'file_data': file_data})
示例#2
0
def save_tip_page_settings(request):

    user = get_current_user(request)
    if not user:
        return render_json({'success': False, 'error_id': 1, 'error_msg': 'user is not logged in'})


    post_params = ['user_currency', 'suggested_tip', 'button_color', 'page_text']

    if not_in(post_params, request.POST):
        return render_json({'success': False, 'error_id': 2, 'error_msg': 'All data not set'})


    try:
        tip_page_setting = TipPageSetting.objects.get(user_id=user.id)
    except:
        tip_page_setting = None




    user_currency = request.POST['user_currency']
    #minimum_tip = request.POST['minimum_tip']
    minimum_tip = 0
    suggested_tip = request.POST['suggested_tip']
    button_color = request.POST['button_color']
    page_text = request.POST['page_text']
    banner_image_id = None


    if user_currency not in settings.GLOBALS['currency_list']:
        return render_json({'success': False, 'error_id': 3, 'error_msg': 'Invalid currency'})


    # try:
    #     minimum_tip = float(minimum_tip)
    #     if minimum_tip < settings.GLOBALS['tip_page_setting_defaults']['minimum_tip']:
    #         return render_json({'success': False, 'error_id': 1, 'error_msg': 'Invalid minimum tip'})
    # except:
    #     print traceback.print_exc()
    #     return render_json({'success': False, 'error_id': 1, 'error_msg': 'Error parsing minimum_tip'})

    try:
        suggested_tip = float(suggested_tip)
        if suggested_tip < settings.GLOBALS['tip_page_setting_defaults']['suggested_tip']:
            return render_json({'success': False, 'error_id': 1, 'error_msg': 'Invalid suggested tip'})
    except:
        return render_json({'success': False, 'error_id': 1, 'error_msg': 'Error parsing suggested_tip'})

    if not valid_hex(button_color):
        return render_json({'success': False, 'error_id': 1, 'error_msg': 'invalid hex for button_color'})


    if not is_string(page_text):
        return render_json({'success': False, 'error_id': 1, 'error_msg': 'Page text not a string'})

    if len(page_text) > settings.GLOBALS['page_text_length_max']:
        return render_json({'success': False, 'error_id': 1, 'error_msg': 'Page text too long'})



    path = 'test/'
    file_data = {}
    if 'image_file' in request.FILES:
        file_data = file_upload(request=request, path=path, file_key_name='image_file', file_type='image')
        banner_image_id = file_data['id']
        print "UPLOADED IMAGE FILE WITH FILE ID"
        print banner_image_id
    elif 'image_url' in request.POST:
        image_url = request.POST['image_url']
        try:
            image_file = File.objects.get(file_url=image_url)
            banner_image_id = image_file.id
        except:
            return render_json({'success': False, 'error_id': 1, 'error_msg': 'File with image_url ' + image_url + ' doesnt exist'})
    else:
        banner_image_id = None


    #if the banner img has changed or removed, delete it
    if tip_page_setting and banner_image_id and banner_image_id != tip_page_setting.banner_image_id:
        delete_user_file(user, tip_page_setting.banner_image_id)






    if tip_page_setting:
        try:
            tip_page_setting.user_currency = user_currency
            tip_page_setting.minimum_tip = minimum_tip
            tip_page_setting.suggested_tip = suggested_tip
            tip_page_setting.button_color = button_color
            tip_page_setting.page_text = page_text
            tip_page_setting.banner_image_id = banner_image_id

            tip_page_setting.save()
        except:
            return render_json({'success': False, 'error_id': 1, 'error_msg': 'Error updating tip_page_setting'})
    else:
        try:
            tip_page_setting = TipPageSetting.objects.create(user_id=user.id, user_currency=user_currency, minimum_tip=minimum_tip, suggested_tip=suggested_tip, button_color=button_color, page_text=page_text)
        except:
            print traceback.print_exc()
            return render_json({'success': False, 'error_id': 1, 'error_msg': 'Error creating tip page setting'})


    return render_json({'success': True, 'tip_page_setting': model_to_dict(tip_page_setting)})