def new_tagged_booking(step, booking_name, building, place_name, start_date, end_date, username, tag): booking = Booking() booking.user = User() booking.user = User.objects.get(username=username) booking.name = booking_name booking.start_date = start_date booking.end_date = end_date booking.place = Place() booking.place.name = place_name booking.place.building = Building() booking.place.building.name = building booking.responsible = username booking.save() tag_o = Tag(name=tag) tag_o.save() booking.tags.add(tag_o) for day in range(0, 10): book = BookTime() book.date_booking = parser.parse(start_date) + timedelta(days=day) book.start_hour = "20:00" book.end_hour = "22:00" book.save() booking.time.add(book) booking.save()
def save(self, user, force_insert=False, force_update=False, commit=True): booking = Booking() booking.user = user booking.name = self.cleaned_data.get("name") booking.start_date = self.cleaned_data.get("start_date") booking.end_date = self.cleaned_data.get("end_date") booking.place = self.cleaned_data.get("place") weekdays = self.cleaned_data.get("week_days") #if user.profile_user.is_admin(): # booking.engineering = self.cleaned_data.get("engineering_choice") # booking.responsible = self.cleaned_data.get("responsible") # name = re.search('[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+', # booking.responsible) # if name is not None: # name = name.group() # users = User.objects.filter(username=name) # ONE_FOUND = 1 # if user.profile_user.is_admin() and (users.count() is ONE_FOUND): # booking.user = users[0] #else: # booking.engineering = user.profile_user.engineering # booking.responsible = str(user.profile_user) book = BookTime() book.date_booking = booking.start_date book.start_hour = self.cleaned_data.get("start_hour") book.end_hour = self.cleaned_data.get("end_hour") try: booking.save() if booking.exists(book.start_hour, book.end_hour, weekdays): booking.delete() return None else: for day in date_range(book.date_booking, booking.end_date): if (day.isoweekday() - 1 in map(int, weekdays)): newBookTime = BookTime(start_hour=book.start_hour, end_hour=book.end_hour, date_booking=day) newBookTime.save() booking.time.add(newBookTime) #tags = self.cleaned_data['tags'] #if tags: # tags = ast.literal_eval(tags) # for name in tags: # if not Tag.objects.filter(name=name).exists(): # tag = Tag(name=name) # tag.save() # tag = Tag.objects.get(name=name) # booking.tags.add(tag) #booking.save() except Exception as e: booking.delete() msg = _('Failed to book selected period') print(e) raise forms.ValidationError(msg) return None return booking