def add_rmc_account(request, site_id): """ This view is for adding a new rmc account to the database and associate it with a site """ site = Sesh_Site.objects.filter(id=site_id).first() # sensors formset factories emonThFormSetFactory = formset_factory(SensorEmonThForm) emonTxFormSetFactory = formset_factory(SensorEmonTxForm) bmvFormSetFactory = formset_factory(SensorBMVForm) # formsets emonth_form_set = emonThFormSetFactory(prefix="emonth") emontx_form_set = emonTxFormSetFactory(prefix="emontx") bmv_form_set = bmvFormSetFactory(prefix="bmv") # emonpi form site_emonpi = Sensor_EmonPi.objects.filter(site=site).first() emonpi_form = SensorEmonPiForm(prefix='emonpi', instance=site_emonpi) context_dict = {} rmc_form = RMCForm() if request.method == 'POST': rmc_form = RMCForm(request.POST) emonpi_form = SensorEmonPiForm(request.POST, prefix='emonpi', instance=site_emonpi) emonth_form_set = emonThFormSetFactory(request.POST, prefix="emonth") emontx_form_set = emonTxFormSetFactory(request.POST, prefix="emontx") bmv_form_set = bmvFormSetFactory(request.POST, prefix="bmv") sensors_sets = [emonth_form_set, emontx_form_set, bmv_form_set] if rmc_form.is_valid(): rmc_account = rmc_form.save(commit=False) rmc_account.site = site rmc_account.save() associate_sensors_sets_to_site(sensors_sets, site) if emonpi_form.is_valid(): emonpi_form.save() return redirect('index') context_dict['rmc_form'] = rmc_form context_dict['emonpi_form'] = emonpi_form context_dict['site_id'] = site_id context_dict['sensors_list'] = SENSORS_LIST context_dict['emonth_form'] = emonThFormSetFactory(prefix="emonth") context_dict['emontx_form'] = emonTxFormSetFactory(prefix="emontx") context_dict['bmv_form'] = bmvFormSetFactory(prefix="bmv") return render(request, 'seshdash/add_rmc_account.html', context_dict)
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)