Ejemplo n.º 1
0
 def filter_fields(self, data, model):
     if isinstance(data, str):
         data = json.loads(data)
     fields = list(get_model_fields(model))
     fields = list(filter(lambda f_field: data.__contains__(f_field.name), fields))
     fields = list(filter(lambda f_field: data[f_field.name], fields))
     return fields
Ejemplo n.º 2
0
    def add_foreign_model_to_excel(self, model: models.Model, to=None, data=None, full=False):
        if full:
            fields = list(get_model_fields(model))
        else:
            fields = self.filter_fields(data, model)
        next_column = self.column
        self.row = self.START_ROW
        get_atr_str = str(model._meta.model_name).lower()
        get_atr_str += "_set"
        for school in self.schools:
            cur_column = self.column
            if to is None:
                objects_list = list(school.__getattribute__(get_atr_str).all())
            self.shit.write(1, cur_column, model._meta.verbose_name)
            for model_object in objects_list:
                for field in fields:
                    self.shit.write(2, cur_column, field.verbose_name.__str__())
                    self.shit.write(self.row, cur_column, getattr(model_object, field.name).__str__())
                    cur_column += 1
            else:
                if len(objects_list) == 0:
                    self.shit.write(self.row, cur_column, "-")
            self.row += 1
            if next_column < cur_column:
                next_column = cur_column

        self.column = next_column
Ejemplo n.º 3
0
    def add_buildings_to_excel(self, temperature_data: dict = None, data=None, full=False):
        global end, temperature_fields
        if full:
            fields = list(get_model_fields(Building))
        else:
            fields = self.filter_fields(data, Building)
        start = None
        if temperature_data is not None:
            if temperature_data.__contains__('start'):
                start = parse_date(temperature_data.pop('start'))
            if temperature_data.__contains__('end'):
                end = parse_date(temperature_data.pop('end'))
            else:
                end = datetime.date.today()
            if full:
                temperature_fields = list(get_model_fields(Temperature))
            else:
                temperature_fields = self.filter_fields(temperature_data, Temperature)
            try:
                temperature_data.pop('date')
            except:
                pass

        next_column = self.column
        self.row = self.START_ROW
        for school in self.schools:
            cur_column = self.column
            buildings = list(school.building_set.all())
            self.shit.write(1, cur_column, Building._meta.verbose_name)
            for building in buildings:
                for field in fields:
                    self.shit.write(2, cur_column, field.verbose_name.__str__())
                    self.shit.write(self.row, cur_column, getattr(building, field.name).__str__())
                    cur_column += 1
                if start is not None:
                    cur_column = self.add_building_temperature_to_excel(start=start, end=end, fields=temperature_fields,
                                                                        cur_column=cur_column,
                                                                        building=building)
            if len(buildings) == 0:
                self.shit.write(self.row, cur_column, "-")
            self.row += 1
            if next_column < cur_column:
                next_column = cur_column
        self.column = next_column
Ejemplo n.º 4
0
    def add_schools_to_excel(self, data=None, full=False):
        if full:
            fields = list(get_model_fields(School))
        else:
            fields = self.filter_fields(data, School)
        next_column = self.column
        self.row = self.START_ROW

        for model_object in self.schools:
            cur_column = self.column
            for field in fields:
                self.shit.write(2, cur_column, field.verbose_name.__str__())
                self.shit.write(self.row, cur_column, getattr(model_object, field.name).__str__())
                cur_column += 1
            self.row += 1
            if next_column < cur_column:
                next_column = cur_column

        self.column = next_column
Ejemplo n.º 5
0
    def add_one_model_to_excel(self, model: models.Model, data=None, full=False):
        if full:
            fields = list(get_model_fields(model))
        else:
            fields = self.filter_fields(data, model)
        next_column = self.column
        self.row = self.START_ROW
        get_atr_str = "get_" + str(model._meta.model_name).lower()
        for school in self.schools:
            model_object = school.__getattribute__(get_atr_str)()
            cur_column = self.column
            for field in fields:
                self.shit.write(2, cur_column, field.verbose_name.__str__())
                self.shit.write(self.row, cur_column, getattr(model_object, field.name).__str__())
                cur_column += 1
            self.row += 1
            if next_column < cur_column:
                next_column = cur_column

        self.column = next_column