Ejemplo n.º 1
0
def new(request, target_id):
    target_info = __get_target_info(request)
    target = target_info['target']
    context = __load_context(request, target_info)
    context = dict(context.items() + __load_alert_options(target).items())

    alert  = Alert(user_profile_id=request.user.user_profile.id)
    if target_info['type'] == 'accounts':  alert.account  = target
    if target_info['type'] == 'buildings': alert.building = target
    if target_info['type'] == 'spaces':    alert.space    = target
    if target_info['type'] == 'meters':    alert.meter    = target
    context['alert'] = alert

    return render(request, 'buildingspeakapp/alerts/new.html', RequestContext(request, context))
Ejemplo n.º 2
0
def create(request, target_id):
    if request.method != 'POST': raise Exception("Please use post for this url.")

    # Build the alert
    target_info = __get_target_info(request)
    target = target_info['target']
    account = target_info['account']
    params = request.POST

    #pdb.set_trace()
    # Redirect before saving if any rule violated
    error_message = None
    # first convert integers to display values
    d_f1 = {v:k for k, v in Alert.FIELD_OPTIONS_1.items()}
    d_f2 = {v:k for k, v in Alert.FIELD_OPTIONS_2.items()}
    d_u1 = {v:k for k, v in Alert.UTILITY_TYPE_OPTIONS_1.items()}
    d_u2 = {v:k for k, v in Alert.UTILITY_TYPE_OPTIONS_2.items()}
    f1 = d_f1[int(params['field_1'])]
    f2 = d_f2[int(params['field_2'])]
    u1 = d_u1[int(params['utility_type_1'])]
    u2 = d_u2[int(params['utility_type_2'])]
    # (1) field_1 and field_2 must match - both must have Cost, Consumption, or PD
    if ( ('Cost'        in f1 and 'Cost'        not in f2) or
         ('Cost'        in f2 and 'Cost'        not in f1) or
         ('Consumption' in f1 and 'Consumption' not in f2) or
         ('Consumption' in f2 and 'Consumption' not in f1) or
         ('Peak Demand' in f1 and 'Peak Demand' not in f2) or
         ('Peak Demand' in f2 and 'Peak Demand' not in f1)    ):
        error_message = 'Comparison fields must match with respect to Cost, Consumption, and Peak Demand.'
    # (2) domestic water must be both utils if it's one util, unless Cost is in both fields
    elif ( (u1 == 'domestic water' or u2 == 'domestic water') and
           not( u1 == u2 or ('Cost' in f1 and 'Cost' in f2) )  ):
        error_message = 'If one utility type is domestic water, either both types must be domestic water or both fields must be Cost fields.'
    
    if error_message is not None:
        messages.error(request, error_message)
        if target_info['type'] == 'accounts':
            return HttpResponseRedirect('/%s/alerts/new/' % account.id)
        else:
            return HttpResponseRedirect('/%s/%s/%s/alerts/new/' % (account.id, target_info['type'], target.id))
    # if no rule violated, proceed with saving

    alert  = Alert(
        user_profile_id = request.user.user_profile.id,
        field_1 = params['field_1'],
        field_2 = params['field_2'],
        utility_type_1 = params['utility_type_1'],
        utility_type_2 = params['utility_type_2'],
        comparator = params['comparator'],
        percent = (float(params['percent']) / 100.0),
        notify_by = params['notify_by'],
        notify_frequency = params['notify_frequency'],
    )
    if target_info['type'] == 'accounts':  alert.account  = target
    if target_info['type'] == 'buildings': alert.building = target
    if target_info['type'] == 'spaces':    alert.space    = target
    if target_info['type'] == 'meters':    alert.meter    = target

    # Save
    alert.save()

    # Add a success message
    messages.success(request, 'Alert has been created.')

    # Redirect to the alerts index page
    account = target_info['account']
    if target_info['type'] == 'accounts':
        return HttpResponseRedirect('/%s/alerts/' % target.id)
    else:
        return HttpResponseRedirect('/%s/%s/%s/alerts/' % (account.id, target_info['type'], target.id))