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