Fix bug when POST'ing filter form data to edit schedule page
This commit is contained in:
		
							parent
							
								
									048951153d
								
							
						
					
					
						commit
						e57757d44b
					
				
					 1 changed files with 34 additions and 31 deletions
				
			
		| 
						 | 
					@ -60,41 +60,44 @@ class ScheduleView(TimeSheetView):
 | 
				
			||||||
                        if uuid:
 | 
					                        if uuid:
 | 
				
			||||||
                            data[field][uuid] = self.request.POST[key]
 | 
					                            data[field][uuid] = self.request.POST[key]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # apply delete operations
 | 
					            # if no fields, don't apply changes (filter form uses POST also)
 | 
				
			||||||
            deleted = []
 | 
					            if any(data.itervalues()):
 | 
				
			||||||
            for uuid, value in data['delete'].iteritems():
 | 
					 | 
				
			||||||
                assert value == 'delete'
 | 
					 | 
				
			||||||
                shift = Session.query(model.ScheduledShift).get(uuid)
 | 
					 | 
				
			||||||
                assert shift
 | 
					 | 
				
			||||||
                Session.delete(shift)
 | 
					 | 
				
			||||||
                deleted.append(uuid)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            # apply create / update operations
 | 
					                # apply delete operations
 | 
				
			||||||
            created = {}
 | 
					                deleted = []
 | 
				
			||||||
            updated = {}
 | 
					                for uuid, value in data['delete'].iteritems():
 | 
				
			||||||
            time_format = '%a %d %b %Y %I:%M %p'
 | 
					                    assert value == 'delete'
 | 
				
			||||||
            for uuid, employee_uuid in data['start_time'].iteritems():
 | 
					 | 
				
			||||||
                if uuid in deleted:
 | 
					 | 
				
			||||||
                    continue
 | 
					 | 
				
			||||||
                if uuid.startswith('new-'):
 | 
					 | 
				
			||||||
                    shift = model.ScheduledShift()
 | 
					 | 
				
			||||||
                    shift.employee_uuid = data['employee_uuid'][uuid]
 | 
					 | 
				
			||||||
                    shift.store_uuid = data['store_uuid'][uuid]
 | 
					 | 
				
			||||||
                    Session.add(shift)
 | 
					 | 
				
			||||||
                    created[uuid] = shift
 | 
					 | 
				
			||||||
                else:
 | 
					 | 
				
			||||||
                    shift = Session.query(model.ScheduledShift).get(uuid)
 | 
					                    shift = Session.query(model.ScheduledShift).get(uuid)
 | 
				
			||||||
                    assert shift
 | 
					                    assert shift
 | 
				
			||||||
                    updated[uuid] = shift
 | 
					                    Session.delete(shift)
 | 
				
			||||||
                start_time = datetime.datetime.strptime(data['start_time'][uuid], time_format)
 | 
					                    deleted.append(uuid)
 | 
				
			||||||
                shift.start_time = make_utc(localtime(self.rattail_config, start_time))
 | 
					 | 
				
			||||||
                end_time = datetime.datetime.strptime(data['end_time'][uuid], time_format)
 | 
					 | 
				
			||||||
                shift.end_time = make_utc(localtime(self.rattail_config, end_time))
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            self.request.session.flash("Changes were applied: created {}, updated {}, "
 | 
					                # apply create / update operations
 | 
				
			||||||
                                       "deleted {} Scheduled Shifts".format(
 | 
					                created = {}
 | 
				
			||||||
                                           len(created), len(updated), len(deleted)))
 | 
					                updated = {}
 | 
				
			||||||
            return self.redirect(self.request.route_url('schedule.edit'))
 | 
					                time_format = '%a %d %b %Y %I:%M %p'
 | 
				
			||||||
 | 
					                for uuid, employee_uuid in data['start_time'].iteritems():
 | 
				
			||||||
 | 
					                    if uuid in deleted:
 | 
				
			||||||
 | 
					                        continue
 | 
				
			||||||
 | 
					                    if uuid.startswith('new-'):
 | 
				
			||||||
 | 
					                        shift = model.ScheduledShift()
 | 
				
			||||||
 | 
					                        shift.employee_uuid = data['employee_uuid'][uuid]
 | 
				
			||||||
 | 
					                        shift.store_uuid = data['store_uuid'][uuid]
 | 
				
			||||||
 | 
					                        Session.add(shift)
 | 
				
			||||||
 | 
					                        created[uuid] = shift
 | 
				
			||||||
 | 
					                    else:
 | 
				
			||||||
 | 
					                        shift = Session.query(model.ScheduledShift).get(uuid)
 | 
				
			||||||
 | 
					                        assert shift
 | 
				
			||||||
 | 
					                        updated[uuid] = shift
 | 
				
			||||||
 | 
					                    start_time = datetime.datetime.strptime(data['start_time'][uuid], time_format)
 | 
				
			||||||
 | 
					                    shift.start_time = make_utc(localtime(self.rattail_config, start_time))
 | 
				
			||||||
 | 
					                    end_time = datetime.datetime.strptime(data['end_time'][uuid], time_format)
 | 
				
			||||||
 | 
					                    shift.end_time = make_utc(localtime(self.rattail_config, end_time))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                self.request.session.flash("Changes were applied: created {}, updated {}, "
 | 
				
			||||||
 | 
					                                           "deleted {} Scheduled Shifts".format(
 | 
				
			||||||
 | 
					                                               len(created), len(updated), len(deleted)))
 | 
				
			||||||
 | 
					                return self.redirect(self.request.route_url('schedule.edit'))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        form = Form(self.request, schema=ShiftFilter)
 | 
					        form = Form(self.request, schema=ShiftFilter)
 | 
				
			||||||
        self.process_filter_form(form)
 | 
					        self.process_filter_form(form)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue