Пример #1
0
def site_alert_rules(request, site_id):
    """
    Alert rules for a given site
    """

    context_dict = {}
    site = Sesh_Site.objects.filter(id=site_id).first()
    alert_rules = Alert_Rule.objects.filter(site=site)
    form = AlertRuleForm()



    if request.method == 'POST':
        form = AlertRuleForm(request.POST)
        if form.is_valid():
            alert_rule = form.save(commit=False)
            alert_rule.site = site
            alert_rule.save()
            return redirect(reverse('site_alert_rules', args=[site.id]))


    context_dict['form'] = form
    context_dict['site'] = site
    context_dict['alert_rules'] = alert_rules
    user_sites = _get_user_sites(request)
    context_dict['permitted'] = get_org_edit_permissions(request.user)
    context_dict['sites_stats'] = get_quick_status(user_sites)
    return render(request, 'seshdash/settings/alert_rules.html', context_dict)
Пример #2
0
 def test_status(self):
     #insert_point in influx
     self.Client.insert_point(self.site, 'soc', 20.0)
     sites = Sesh_Site.objects.all()
     response = get_quick_status(sites)
     print 'The reponse is: %s' % response
     self.assertEqual(response[0]['battery'], 'red')
     self.assertEqual(response[0]['weather'], 'yellow')
 def test_status(self):
     #insert_point in influx
     self.Client.insert_point(self.site,'soc',20.0)
     sites = Sesh_Site.objects.all()
     response = get_quick_status(sites)
     print 'The reponse is: %s' % response
     self.assertEqual(response[0]['battery'],'red')
     self.assertEqual(response[0]['weather'],'yellow')
Пример #4
0
def settings_alert_rules(request):
    """
    To allow users to manage alert
    rules for given sites
    """
    context_dict = {}
    sites = _get_user_sites(request)

    user_sites = _get_user_sites(request)
    context_dict['permitted'] = get_org_edit_permissions(request.user)
    context_dict['sites_stats'] = get_quick_status(user_sites)
    context_dict['sites'] = sites
    return render(request, 'seshdash/settings/sites_alert_rules.html', context_dict)
Пример #5
0
def index(request,site_id=0):
    """
    Initial user view user needs to be logged
    Get user related site data initially to display on main-dashboard view
    """
    sites =  _get_user_sites(request)

    context_dict = {}
    # Handle fisrt login if user has no site setup:
    if not sites:
           context_dict['VRM_form'] = _create_vrm_login_form()
           return render(request,'seshdash/initial-login.html',context_dict)

    if not site_id:
        #return first site user has action
        site_id = sites[0].pk
    #Check if user has any sites under their permission
    context_dict, content_json = get_user_data(request.user,site_id,sites)

    context_dict = jsonify_dict(context_dict,content_json)
    #Generate date for dashboard  TODO use victron solar yield data using mock weather data for now
    context_dict['site_id'] = site_id

    #Generating site measurements for a graph
    current_site = Sesh_Site.objects.filter(id = site_id).first()

    #getting site measurements
    site_measurements = get_site_measurements(current_site)
    measurements ={}

    for measurement in site_measurements:
        #getting verbose names
        measurement_verbose_name = get_measurement_verbose_name(measurement)
        measurements[measurement] = measurement_verbose_name
    context_dict['measurements']= measurements

    # status card form
    site = Sesh_Site.objects.filter(id=site_id).first()
    form = StatusCardForm(instance=site.status_card) 
    context_dict['status_form'] = form


    #sites witth weather and battery status
    sites_stats = get_quick_status(sites)
    context_dict['sites_stats'] = sites_stats

    # user permissions
    context_dict['permitted'] = get_org_edit_permissions(request.user)
    context_dict['user'] = request.user

    return render(request,'seshdash/main-dash.html',context_dict)
Пример #6
0
def manage_org_users(request):
    """
    View to manage the users of an organisation
    Should only be accessed by admin users of the organisation
    """
    if request.user.is_org_admin:
        context_dict = {}
        context_dict['organisation_users'] = request.user.organisation.get_users()
        context_dict['form'] = SeshUserForm()
        user_sites = _get_user_sites(request)
        context_dict['permitted'] = get_org_edit_permissions(request.user)
        context_dict['sites_stats'] = get_quick_status(user_sites)
        return render(request, 'seshdash/settings/organisation_users.html', context_dict)
    else:
        return HttpResponseForbidden()
Пример #7
0
def export_csv_measurement_data(request):
    """
    Returns a csv of a given measurement a request

    """
    import csv
    context_dict = {}
    measurement = request.POST.get('measurement', '')
    start_time = request.POST.get('start-time', None)
    end_time = request.POST.get('end-time', None)
    site_name = request.POST.get('site-name', '')

    site = Sesh_Site.objects.filter(site_name=site_name).first()

    if request.method == 'POST':
        # Converting strings to date
        start_time = datetime.strptime(start_time, '%Y-%m-%d')
        end_time = datetime.strptime(end_time, '%Y-%m-%d')

        i = Influx()

        results = i.get_measurement_range(measurement, start_time, end_time, site=site)
 
        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = 'attachment; filename="%s.csv"' % ( site.site_name + '_' + measurement + '_sesh')
        writer = csv.DictWriter(response, ['site_name', 'time', 'value'])

        writer.writeheader()
        for result in results:
            del result['source']
            writer.writerow(result)

        return response

    i = Influx()
    user_sites = _get_user_sites(request)
    context_dict['sites'] = user_sites
    context_dict['measurements'] = i.get_measurements()
    context_dict['permitted'] = get_org_edit_permissions(request.user)
    context_dict['sites_stats'] = get_quick_status(user_sites)
    return render(request, 'seshdash/data_analysis/export-csv.html', context_dict)
Пример #8
0
def site_add_edit(request):
    """
    This views renders the page for editing and
    adding new rmc sites
    """
    context_dict = {}
    sites = _get_user_sites(request)
    context_dict['sites_stats'] = get_quick_status(sites)
    context_dict['sites'] = sites
    context_dict['permitted'] = get_org_edit_permissions(request.user)
    user_vrm_accounts = (get_user_vrm_accounts(request))
    context_dict['has_vrm'] = False
    if user_vrm_accounts.get('items',''):
        context_dict.update(get_user_vrm_accounts(request)) # Add user VRM accounts
        context_dict['has_vrm'] = True
        context_dict['var_url'] = 'import-site-account' # for link generation on list
        context_dict['param'] = 'vrm_account' # for link generation on list

    else:
        context_dict['VRM_form'] = _create_vrm_login_form()


    return render(request, 'seshdash/settings/site_settings.html', context_dict)
Пример #9
0
def edit_sesh_user(request, user_id):
    """
    Edits a sesh user
    the user loged in should be an admin of the organisation
    """
    if request.user.is_org_admin:
        context_dict = {}
        user = Sesh_User.objects.filter(id=user_id).first()
        form = SeshUserForm(instance=user)

        if request.method == 'POST':
            form = SeshUserForm(request.POST, instance=user)
            if form.is_valid():
                form.save()
                return redirect('manage_org_users')

        user_sites = _get_user_sites(request)
        context_dict['form'] = form
        context_dict['permitted'] = get_org_edit_permissions(request.user)
        context_dict['sites_stats'] = get_quick_status(user_sites)
        return render(request, 'seshdash/settings/edit_sesh_user.html', context_dict)
    else:
        return HttpResponseForbidden()
Пример #10
0
def user_notifications(request):
    """
    Renders page to display users of an organization
    and their values to view the sites
    """
    context_dict = {}
    user = request.user

    organisation_users = Sesh_User.objects.filter(organisation=user.organisation) # all users belonging to the same organisations
    SeshUserFormSetFactory = modelformset_factory(Sesh_User, fields=('on_call', 'send_mail', 'send_sms',), extra=0)
    sesh_user_formset = SeshUserFormSetFactory(queryset=organisation_users)

    if request.method == 'POST':
        sesh_user_formset = SeshUserFormSetFactory(request.POST, queryset=organisation_users)
        if sesh_user_formset.is_valid():
            sesh_user_formset.save()
            return redirect('index')

    user_sites = _get_user_sites(request)
    context_dict['permitted'] = get_org_edit_permissions(request.user)
    context_dict['sites_stats'] = get_quick_status(user_sites)
    context_dict['user_formset'] = sesh_user_formset
    return render(request, 'seshdash/settings/user_notifications.html', context_dict)
Пример #11
0
def historical_data(request):
    # If ajax request
    if request.method == 'POST':
        sort_value = request.POST.get('sort_value', '')
        historical_data = get_historical_dict(column=sort_value)
        return HttpResponse(json.dumps(historical_data))

    # On page load
    else:
        sort_data_dict = get_model_verbose(Daily_Data_Point)
        # Removing values that are not data historical values
        sort_data_dict.pop('id')
        sort_data_dict.pop('site')
        sort_data_dict.pop('date')


        #sites = get_objects_for_user(request.user, 'seshdash.view_Sesh_Site')
        sites =  _get_user_sites(request)
        active_site = sites[0]
        context_dict = {}

        #sites witth weather and battery status
        user_sites =  sites
        sites_stats = get_quick_status(user_sites)
        context_dict['sites_stats'] = sites_stats

        #checking user permissions
        user = request.user
        permission = get_permissions(user)
        context_dict['permitted'] = get_org_edit_permissions(user)

        context_dict['sites'] = sites
        context_dict['site_id'] = 0
        context_dict['active_site'] = active_site
        context_dict['sort_keys'] = sort_data_dict.keys()
        context_dict['sort_dict'] = sort_data_dict
        return render(request, 'seshdash/data_analysis/historical-data.html', context_dict);
Пример #12
0
def edit_site(request,site_Id=1):
    context_dict = {}
    site = get_object_or_404(Sesh_Site, id=site_Id)
    rmc_account = Sesh_RMC_Account.objects.filter(site=site).first();

    # If it is an rmc site create a rmc_form
    if rmc_account:
        site_form = SiteRMCForm(instance=site)
        rmc_form = RMCForm(instance=rmc_account)
        context_dict['RMCForm'] = rmc_form
    else:
        site_form = SiteVRMForm(instance=site)


    if request.method == 'POST':
        if rmc_account:
            site_form = SiteRMCForm(request.POST, instance=site)
            rmc_form = RMCForm(request.POST, instance=rmc_account)

            if site_form.is_valid() and rmc_form.is_valid():
                site_form.save()
                rmc_form.save()
            context_dict['RMCForm'] = rmc_form
        else:
            site_form = SiteVRMForm(request.POST, instance=site)
            if site_form.is_valid():
                site_form.save()


    user_sites = _get_user_sites(request)
    context_dict['VRM_form'] = VRMForm()
    context_dict['site_form']= site_form
    context_dict['sites']= user_sites
    context_dict['permitted'] = get_org_edit_permissions(request.user)
    context_dict['sites_stats'] = get_quick_status(user_sites)
    return render(request,'seshdash/settings/site_settings.html', context_dict)