Exemple #1
0
def res_view(request):
    if request.user.is_authenticated:
        logging.debug('Составление страницы РЭС')
        url_param = request.GET['res']
        result_url_string = url_param.split('/')
        current_username = ''
        if 'username' in request.session:
            current_username = request.session['username']
        user = str(current_username)
        logging.info(f'Пользователь {user}')
        result_filter_db = db_work.read_user_filters(user)
        result_inhibit_filter_db = db_work.read_user_inhibit_filters(user)
        result = db.get_elements_for_table(url_param, result_filter_db, result_inhibit_filter_db)
        array_list = []
        common_dictionary = {}
        data_layer = db_work.get_all_points_by_res(url_param, result_filter_db, result_inhibit_filter_db)
        for elem in data_layer:
            elem = str(elem).lstrip("('").rstrip("',)")
            array_list.append(elem.split('\\\\'))
        result_for_menu = views.menu_items_dict(request)
        fes_dictionary = {}
        ps_dictionary = {}
        fes_name_dictionary = views.create_dict_fes_description()
        ps_name_dictionary = views.create_dict_ps_description()
        for fes in fes_name_dictionary.keys():
            fes_value = db_work.read_fes_value(fes)
            fes_dictionary[fes] = fes_value

        for ps in ps_name_dictionary.keys():
            ps_value = db_work.read_ps_value(ps)
            ps_dictionary[ps] = ps_value
        for item in array_list:
            end_part = ""
            for id_item, val in enumerate(item):
                if id_item == 3:
                    end_part = val
            if item[0] in common_dictionary:
                if item[1] in common_dictionary[item[0]]:
                    if item[2] in common_dictionary[item[0]][item[1]]:
                        common_dictionary[item[0]][item[1]][item[2]].append(end_part)
                    else:
                        common_dictionary[item[0]][item[1]][item[2]] = []
                        common_dictionary[item[0]][item[1]][item[2]].append(end_part)
                else:
                    common_dictionary[item[0]][item[1]] = {}
                    common_dictionary[item[0]][item[1]][item[2]] = []
                    common_dictionary[item[0]][item[1]][item[2]].append(end_part)
            else:
                common_dictionary[item[0]] = {}
                common_dictionary[item[0]][item[1]] = {}
                common_dictionary[item[0]][item[1]][item[2]] = []
                common_dictionary[item[0]][item[1]][item[2]].append(end_part)
            agas_array = sorted(set(common_dictionary[item[0]][item[1]][item[2]]))
            common_dictionary[item[0]][item[1]][item[2]] = agas_array
    else:
        return HttpResponseRedirect('/')
    logging.debug('res_view() OK...')
    return render(request, 'res.html', {"name": result_url_string[1], "table_elems": common_dictionary,
                                        "ps": ps_dictionary, "dict": result_for_menu, "dict_two": fes_dictionary,
                                        "dict_three": ps_dictionary})
def device_view(request):
    if request.user.is_authenticated:
        logging.debug('Составление страницы Информация о приборе')
        url_param = request.GET['name']
        result_url_string = url_param.split('\\')
        if 'username' in request.session:
            current_username = request.session['username']
        user = str(current_username)
        logging.info(f'Пользователь {user}')
        result_filter_db = db_work.read_user_filters(user)
        result_inhibit_filter_db = db_work.read_user_inhibit_filters(user)
        description = []
        result = db.get_elements_for_device(url_param, result_filter_db,
                                            result_inhibit_filter_db)
        for res in result:
            description.append(res['driver'])
            description.append(res['ktt'])
            description.append(res['number_point'])
        result_for_menu = views.menu_items_dict(request)
        fes_dictionary = {}
        ps_dictionary = {}
        fes_name_dictionary = views.create_dict_fes_description()
        ps_name_dictionary = views.create_dict_ps_description()
        for fes in fes_name_dictionary.keys():
            fes_value = db_work.read_fes_value(fes)
            fes_dictionary[fes] = fes_value

        for ps in ps_name_dictionary.keys():
            ps_value = db_work.read_ps_value(ps)
            ps_dictionary[ps] = ps_value
        array_ids = db.get_id_device_elements(url_param, result_filter_db,
                                              result_inhibit_filter_db)
        start_date = date.today()
        start_date_month = date.today().replace(day=1)
        result_data_day = (db.get_result_device_day_elements(
            array_ids, str(start_date)))
        result_date_month = (db.get_result_device_day_elements(
            array_ids, str(start_date_month)))
    else:
        return HttpResponseRedirect('/')
    logging.debug('device_view() OK...')
    return render(
        request, 'device.html', {
            "ps": result_url_string[2],
            "vl": result_url_string[3],
            "tp": result_url_string[4],
            "tu": result_url_string[5],
            'description_array': description,
            "day_start": result_data_day,
            "day_start_month": result_date_month,
            "dict": result_for_menu,
            "dict_two": fes_dictionary,
            "dict_three": ps_dictionary
        })
Exemple #3
0
def accounting_view(request):
    if request.user.is_authenticated:
        logging.debug('Составление страницы Учёты электроэнергии')
        result_for_menu = views.menu_items_dict(request)
        fes_dictionary = {}
        ps_dictionary = {}
        fes_name_dictionary = views.create_dict_fes_description()
        ps_name_dictionary = views.create_dict_ps_description()
        for fes in fes_name_dictionary.keys():
            fes_value = db_work.read_fes_value(fes)
            fes_dictionary[fes] = fes_value
        for ps in ps_name_dictionary.keys():
            ps_value = db_work.read_ps_value(ps)
            ps_dictionary[ps] = ps_value
        return render(
            request, 'accounting.html', {
                'dict': result_for_menu,
                'dict_two': fes_dictionary,
                'dict_three': ps_dictionary
            })
    else:
        return HttpResponseRedirect('/')
def archive_day_view(request):
    if request.user.is_authenticated:
        logging.debug('Составление страницы "Архив по дню"')
        today = datetime.datetime.today()

        if len(str(today.month)) < 2:
            month = '0' + str(today.month)

        result_date = str(today.year) + "-" + month
        url_param = request.GET.get('driver_name')
        result_url_string = url_param.split('\\')
        try:
            date_param_one = request.GET['date_one']
            date_param_second = request.GET['date_second']
        except Exception as e:
            date_param_one = ''
            date_param_second = ''
        if 'username' in request.session:
            current_username = request.session['username']
        user = str(current_username)
        logging.info(f'Пользователь {user}')
        result_filter_db = db_work.read_user_filters(user)
        result_inhibit_filter_db = db_work.read_user_inhibit_filters(user)
        description = []
        result = db.get_elements_for_device(url_param, result_filter_db,
                                            result_inhibit_filter_db)
        for res in result:
            description.append(res['driver'])
            description.append(res['ktt'])
            description.append(res['number_point'])
        data_by_day_dict = {}
        result_for_menu = views.menu_items_dict(request)
        fes_dictionary = {}
        ps_dictionary = {}
        fes_name_dictionary = views.create_dict_fes_description()
        ps_name_dictionary = views.create_dict_ps_description()
        for fes in fes_name_dictionary.keys():
            fes_value = db_work.read_fes_value(fes)
            fes_dictionary[fes] = fes_value

        for ps in ps_name_dictionary.keys():
            ps_value = db_work.read_ps_value(ps)
            ps_dictionary[ps] = ps_value
        if date_param_one == '' and date_param_second == '':
            date_param_one = date.today().replace(day=1)
            last_day = monthrange(date_param_one.year, date_param_one.month)
            date_param_second = f"{date_param_one.year}-{date_param_one.month}-{last_day[1]}"
        array_ids = db.get_id_device_elements(url_param, result_filter_db,
                                              result_inhibit_filter_db)
        result_data_by_day = db.get_result_day_by_day_elements(
            array_ids, str(date_param_one), str(date_param_second))
        minsk = pytz.timezone('Europe/Minsk')
        for elem_result in result_data_by_day:
            formatedDate = elem_result[1].replace(
                tzinfo=pytz.UTC).astimezone(minsk).replace(
                    tzinfo=None).strftime("%d.%m.%Y")
            data_by_day_dict[formatedDate] = elem_result[0]
        return_button = '\\'.join(result_url_string)
        logging.debug('archive_day_view() OK...')
        return render(
            request, 'archive_day.html', {
                "ps": result_url_string[2],
                "vl": result_url_string[3],
                "ktp": result_url_string[4],
                "description": description,
                "data_by_date": data_by_day_dict,
                "return": return_button,
                "dict": result_for_menu,
                "dict_two": fes_dictionary,
                "dict_three": ps_dictionary,
                "date_result": result_date
            })
    else:
        return HttpResponseRedirect('/')
def rashod_month_view(request):
    if request.user.is_authenticated:
        logging.debug('Составление страницы "Расход по месяцу"')
        url_param = request.GET['name']
        result_url_string = url_param.split('\\')
        try:
            year = request.GET['year']
        except Exception as e:
            year = ''
        if 'username' in request.session:
            current_username = request.session['username']
        user = str(current_username)
        logging.info(f'Пользователь {user}')
        result_for_menu = views.menu_items_dict(request)
        fes_dictionary = {}
        ps_dictionary = {}
        fes_name_dictionary = views.create_dict_fes_description()
        ps_name_dictionary = views.create_dict_ps_description()
        for fes in fes_name_dictionary.keys():
            fes_value = db_work.read_fes_value(fes)
            fes_dictionary[fes] = fes_value

        for ps in ps_name_dictionary.keys():
            ps_value = db_work.read_ps_value(ps)
            ps_dictionary[ps] = ps_value
        result_filter_db = db_work.read_user_filters(user)
        result_inhibit_filter_db = db_work.read_user_inhibit_filters(user)
        description = []
        result = db.get_elements_for_device(url_param, result_filter_db,
                                            result_inhibit_filter_db)
        for res in result:
            description.append(res['driver'])
            description.append(res['ktt'])
            description.append(res['number_point'])
        array_ids = db.get_id_device_elements(url_param, result_filter_db,
                                              result_inhibit_filter_db)
        data_by_day_dict = {}
        if year == '':
            result_data_by_day = []
            today = date.today()
            for i in range(1, 13):
                if i == 12:
                    continue
                if i == today.month:
                    date_start = f"{today.year}-{i}-{1}"
                    date_end = f"{today.year}-{i}-{today.day}"
                    result_data_by_day.extend(
                        db_work.get_result_elements(array_ids, str(date_start),
                                                    str(date_end)))
                else:
                    date_start = f"{today.year}-{i}-{1}"
                    date_end = f"{today.year}-{i + 1}-{1}"
                    result_data_by_day.extend(
                        db_work.get_result_elements(array_ids, str(date_start),
                                                    str(date_end)))
        else:
            result_data_by_day = []
            today = date.today()
            for i in range(1, 13):
                if i == 12:
                    continue
                if i == today.month:
                    date_start = f"{year}-{i}-{1}"
                    date_end = f"{year}-{i}-{today.day}"
                    result_data_by_day.extend(
                        db_work.get_result_elements(array_ids, str(date_start),
                                                    str(date_end)))
                else:
                    date_start = f"{year}-{i}-{1}"
                    date_end = f"{year}-{i + 1}-{1}"
                    result_data_by_day.extend(
                        db_work.get_result_elements(array_ids, str(date_start),
                                                    str(date_end)))
                date_start = f"{year}-{i}-{1}"
                date_end = f"{year}-{i+1}-{1}"
                result_data_by_day.extend(
                    db_work.get_result_elements(array_ids, str(date_start),
                                                str(date_end)))
        minsk = pytz.timezone('Europe/Minsk')
        for elem_result in result_data_by_day:
            formatedDate = elem_result['time_start_write'].replace(
                tzinfo=pytz.UTC).astimezone(minsk).replace(
                    tzinfo=None).strftime("%d.%m.%Y")
            data_by_day_dict[formatedDate] = "{:10.3f}".format(
                (elem_result['end_period'] - elem_result['start_period']) *
                elem_result['ktt'])
        return_button = '\\'.join(result_url_string)
    else:
        return HttpResponseRedirect('/')
    logging.debug('rashod_month_view() OK...')
    return render(
        request, 'rashod_month.html', {
            "ps": result_url_string[2],
            "vl": result_url_string[3],
            "ktp": result_url_string[4],
            "description": description,
            "data_by_date": data_by_day_dict,
            "return": return_button,
            "dict": result_for_menu,
            "dict_two": fes_dictionary,
            "dict_three": ps_dictionary
        })
Exemple #6
0
def table_view(request):
    """
    Выполняет составление таблицы балансов распределений,
    основываясь на пути, взятом из меню пользователя
    """
    if request.user.is_authenticated:
        logging.debug('Составление таблицы балансов')
        url_param = request.GET.get('balans')
        try:
            vl_param = request.GET['vl']
            start_date = request.GET['start']
            end_date = request.GET['end']
            time_dif = request.GET['time_diff']
        except Exception as e:
            vl_param = ''
            start_date = ''
            end_date = ''
            time_dif = ''
        try:
            result_url_string = url_param.split('/')
            current_username = request.session['username']
            user = str(current_username)
            logging.info(f'Пользователь {user}')
            ps_dictionary = {}
            ps_name_dictionary = views.create_dict_ps_description()
            for ps in ps_name_dictionary.keys():
                ps_value = db_work.read_ps_value(ps)
                ps_dictionary[ps] = ps_value
            result_filter_db = db_work.read_user_filters(user)
            result_inhibit_filter_db = db_work.read_user_inhibit_filters(user)
            result = db.get_elements_for_table(url_param, result_filter_db,
                                               result_inhibit_filter_db)
            array_elements = []
            res_name = result_url_string[0] + "/" + result_url_string[1]
            res_res = result_url_string[1]
            menu_table_dictionary = my_dictionary()
            result_for_menu = views.menu_items_dict(request)
            fes_dictionary = {}
            pss_dictionary = {}
            fes_name_dictionary = views.create_dict_fes_description()
            ps_name_dictionary = views.create_dict_ps_description()
            for fes in fes_name_dictionary.keys():
                fes_value = db_work.read_fes_value(fes)
                fes_dictionary[fes] = fes_value
            for ps in ps_name_dictionary.keys():
                ps_value = db_work.read_ps_value(ps)
                pss_dictionary[ps] = ps_value
            name_ps = pss_dictionary[result_url_string[2]]
            for db_data in result:
                db_data = str(db_data).lstrip("('").rstrip("',)")
                left_join = db_data.find(result_url_string[-1])
                db_data = db_data[left_join:]
                right_join = db_data.find('\\\\')
                db_data = db_data[right_join + 2:]
                array_elements.append(first_words(db_data, 1))
                menu_table_dictionary.add(result_url_string[-1],
                                          set(array_elements))

            if vl_param == '':
                vl_param = array_elements[0]

            if start_date == '' and end_date == '':
                start_date = date.today()
                end_date = date.today() - timedelta(days=1)

            result_array_aga = []
            for elem in menu_table_dictionary.get(result_url_string[-1]):
                result_array_aga.append(elem)

            all_path = url_param + '/' + vl_param
            array_ids = db_work.get_id_elements(all_path, result_filter_db,
                                                result_inhibit_filter_db)
            array_reg_ids = db_work.get_reg_id_elements(
                all_path.replace('/', r'\\'))

            result_data = []
            result_data_reg = []
            result_data.extend(
                db_work.get_result_elements(array_ids, str(start_date),
                                            str(end_date)))
            result_data_reg.extend(
                db_work.get_reg_result_elements(array_reg_ids, str(start_date),
                                                str(end_date)))
            sub = 0
            hidden_start_date = db.get_start_data_res()
            year = hidden_start_date._row[0].year
            month = hidden_start_date._row[0].month
            day = hidden_start_date._row[0].day
            if len(result_data_reg) != 0:
                leng = len(result_data_reg)
                second = result_data_reg[leng - 1].start_period
                first = result_data_reg[0].start_period
                sub = second - first
            sub = abs(round(sub, 1))
            finish_data_str = str(year) + '-' + str(month) + '-' + str(day + 1)
        except Exception as e:
            fdsh = e
            print(e)
    else:
        return HttpResponseRedirect('/')
    logging.debug('table_view() OK...')
    return render(
        request, 'substation_info.html', {
            "table_elements": result_data,
            "menu_table": menu_table_dictionary,
            "time_dif": time_dif,
            "reg_consumption": sub,
            "hidden_date": finish_data_str,
            "ps": ps_dictionary,
            "dict": result_for_menu,
            "dict_two": fes_dictionary,
            "dict_three": pss_dictionary,
            "res_name": res_name,
            "res": res_res,
            "ps_name": name_ps
        })