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
        })
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('/')
示例#3
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
        })