def post(self, request, *args, **kwargs): if self.get_zone and self.get_mode() == 'layout': form = ZonemapNewForm(request.POST) if form.is_valid(): zone_id = form.cleaned_data.get('zone_id') rows = form.cleaned_data.get('rows') cols = form.cleaned_data.get('cols') onidc_id = request.user.onidc.id zone_id = self.get_zone.id creator_id = request.user.id cells = [] for row in range(rows): for col in range(cols): point = Zonemap.objects.filter(zone_id=zone_id, row=row, col=col) if not point.exists(): cells.append( Zonemap( onidc_id=onidc_id, zone_id=zone_id, creator_id=creator_id, row=row, col=col, )) Zonemap.objects.bulk_create(cells) redirect_to = reverse_lazy('idcops:zonemap') return HttpResponseRedirect('{}?zone_id={}'.format( redirect_to, self.get_zone.id)) if self.get_zone and self.get_mode() == 'config': if request.is_ajax(): cell_id = request.POST.get('cell_id', None) rack_id = request.POST.get('rack_id', None) cell_desc = request.POST.get('cell_desc', None) try: cell = Zonemap.objects.get(pk=cell_id) except: cell = None if cell is not None: cell.rack_id = rack_id cell.desc = cell_desc cell.save() data = { 'cell_id': cell.pk, 'rack_id': cell.rack_id, 'cell_desc': cell.desc, 'messages': "更新成功", } return JsonResponse(data)
def get_context_data(self, **kwargs): context = super(ZonemapView, self).get_context_data(**kwargs) model = apps.get_model('idcops', 'zonemap') meta, _ = construct_model_meta(self.request, model, str(self.get_zone)) if self.get_mode() == 'layout': form = ZonemapNewForm(zone_id=self.get_zone.id) else: form = None if self.get_mode() == 'config': incells = list(Zonemap.objects.filter( zone_id=self.get_zone.id).values_list( 'rack_id', flat=True).exclude(rack=None)) rackswap = Rack.objects.filter(zone_id=self.get_zone.id).exclude( pk__in=incells).order_by('-name') else: incells = rackswap = None _extra_cxt = { 'zones': self.get_zones, 'current_zone': self.get_zone, 'meta': meta, 'form': form, 'incells': incells, 'clients': self.get_clients(), 'rackswap': rackswap, 'config': self.get_mode() == 'config', 'racks': self.get_racks(), 'cells': self.get_cells(), 'statistics': self.get_rack_statistics(), 'max_col': self.max_col, } context.update(_extra_cxt) return context
def post(self, request, *args, **kwargs): if self.get_zone and self.get_mode() == 'layout': form = ZonemapNewForm(request.POST) if form.is_valid(): zone_id = form.cleaned_data.get('zone_id') rows = form.cleaned_data.get('rows') cols = form.cleaned_data.get('cols') onidc_id = request.user.onidc.id zone_id = self.get_zone.id creator_id = request.user.id old_cells = Zonemap.objects.filter(zone_id=zone_id).order_by( "row", "col").values("row", "col") if old_cells.exists(): LAST_ROWS = old_cells.aggregate(Max('row'))['row__max'] + 1 LAST_COLS = old_cells.aggregate(Max('col'))['col__max'] + 1 Zonemap.objects.filter( zone_id=zone_id, row__in=list(range(rows, LAST_ROWS)) ).delete() Zonemap.objects.filter( zone_id=zone_id, col__in=list(range(cols, LAST_COLS)) ).delete() cells = [] for row in range(rows): for col in range(cols): point = Zonemap.objects.filter( zone_id=zone_id, row=row, col=col) if not point.exists(): cells.append(Zonemap( onidc_id=onidc_id, zone_id=zone_id, creator_id=creator_id, row=row, col=col, )) Zonemap.objects.bulk_create(cells) redirect_to = reverse_lazy('idcops:zonemap') return HttpResponseRedirect('{}?zone_id={}'.format( redirect_to, self.get_zone.id)) if self.get_zone and self.get_mode() == 'config': if request.is_ajax(): cell_id = request.POST.get('cell_id', None) rack_id = request.POST.get('rack_id', None) cell_desc = request.POST.get('cell_desc', None) try: cell = Zonemap.objects.get(pk=cell_id) except BaseException: cell = None if cell is not None: with transaction.atomic(): cell.rack_id = rack_id cell.desc = cell_desc cell.save() data = { 'cell_id': cell.pk, 'rack_id': cell.rack_id, 'cell_desc': cell.desc, 'messages': "更新成功", } return JsonResponse(data)