def save(self, *args, **kwargs): if not self.__member_has_open_reservations(): raise DataError('Member has already booked a timeslot for this day.') if not self.__timeslot_is_available(): raise DataError('The timeslot is already full') return super().save(*args, **kwargs)
def save(self, *args, **kwargs): if not self.tracker.has_changed('household_id'): return super().save(*args, **kwargs) if not self.__household_has_vacancies(): raise DataError('Household is already full') if self.household_id and self.student_team_id: raise DataError( 'One cannot join both a household and a student team') # we're in the clear super().save(*args, **kwargs)
def _map_entity(self, entity): new_key = Key.from_path(self.to_kind, entity.key().id_or_name(), namespace=self.to_namespace) parent = entity.parent() if parent: # If the entity has an ancestor then we need to make sure that that ancestor exists in # the new namespace as well new_parent_key = Key.from_path(parent.kind(), parent.is_or_name(), namespace=self.to_namespace) new_parent_exists = Get([new_parent_key])[0] if not new_parent_exists: raise DataError( "Trying to copy entity with an ancestor (%r) to a new namespace but the " "ancestor does not exist in the new namespace. Copy the ancestors first." % entity.key()) def txn(): existing = Get([new_key])[0] if existing and not self.overwrite_existing: return if isinstance(entity.key().id_or_name(), (int, long)): reserve_id(self.to_kind, entity.key().id_or_name(), self.to_namespace) new_entity = clone_entity(entity, new_key) Put(new_entity) RunInTransaction(txn)
def save(self, *args, **kwargs): self.end_time = self.start_time + timedelta(hours=LENGTH_OF_TIMESLOT) slug = [ str(self.area.id), self.start_time.strftime(SLUG_STRFTIME_FORMAT), self.end_time.strftime(SLUG_STRFTIME_FORMAT), ] self.slug = "-".join(slug) if (self.start_time.hour % LENGTH_OF_TIMESLOT != 0): raise DataError('Timeslots must be divisible by LENGTH_OF_TIMESLOT') if (self.start_time.minute != 0 or self.start_time.second != 0): raise DataError('Timeslots must start at the beginning of the hour') return super().save(*args, **kwargs)
def join_alt_fields(apps, schema_editor): for model in apps.app_configs["enhydris"].get_models(): alt_fields = [ field for field in model._meta.get_fields() if field.name.endswith("_alt") ] if not alt_fields: continue for obj in model.objects.all(): for alt_field in alt_fields: field = [ field for field in model._meta.get_fields() if field.name == alt_field.name[:-4] ][0] value = getattr(obj, field.name) value_alt = getattr(obj, alt_field.name) if (not value_alt) or (value == value_alt): # The _alt field is empty or the same as the main field, do nothing continue elif not value: # The main field is empty, set it to the _alt field setattr(obj, field.name, value_alt) else: # Both are nonempty, join them if type(field).__name__ == "CharField": fmt = "{} [{}]" elif type(field).__name__ == "TextField": fmt = "{}" + SEPARATOR + "{}" else: raise Exception( "Field {}.{} is neither a CharField nor a TextField".format( model.__name__, field.name ) ) setattr(obj, field.name, fmt.format(value, value_alt)) # Set the alt field to empty. Then, if we come again to the same object # (e.g. when iterating in Gentity, and then again in Station), there # won't be chaos. setattr(obj, alt_field.name, "") try: obj.save() except DataError as e: raise DataError( "Couldn't save object {} (id={}); perhaps {}='{}' is too long. " "Original message: {}".format( model.__name__, obj.id, field.name, getattr(obj, field.name), str(e), ) )
def new_proposal(proposal_name="only a test", date_proposed=None, proposal_description="yes, this is only a test", proposal_group=None, owned_by=None): if not date_proposed: date_proposed = timezone.now() if owned_by is None: raise DataError("Owned by must be set for proposal creation.") return Proposal.objects.create( proposal_name=proposal_name, date_proposed=date_proposed, proposal_description=proposal_description, owned_by=owned_by, proposal_group=proposal_group)
def update_review_fields(lesson, isCorrect): """ Update lesson fields relating to reviews """ now = timezone.now() if lesson.next_review_time > now: raise DataError('The lesson is not available for review yet.') if isCorrect: lesson.review_correct += 1 else: lesson.review_incorrect += 1 lesson.review_stage = new_review_stage(lesson, isCorrect) lesson.next_review_time = nearest_hour( now + nextReviewTimedeltaTable[lesson.review_stage] ) lesson.save(update_fields=[ 'review_correct', 'review_incorrect', 'review_stage', 'next_review_time', ]) print(f'Updated lesson { lesson.id }')