def _make_series(klass, form, **kwargs): series_data = DataBuilder.series_data_from_form(form) series_data.update(kwargs) klass._validate_series_data(series_data) series_data = klass._remove_none_fields(series_data) series = EventSeries(**series_data) series.save() return series
def unique_with_database(form, field): message = "An event with that slug already exists." from app.models import Event, EventSeries if EventSeries.objects(slug=field.data).count() != 0: raise ValidationError(message) if Event.objects(slug=field.data).count() != 0: raise ValidationError(message)
def register_delete_rules(): """ All delete rules for User fields must by DENY, because User objects should never be deleted. Lists of reference fields should PULL, to remove deleted objects from the list, and all others should NULLIFY """ from app.models import Event, EventSeries, User, Post, BlogPost, Image from mongoengine import NULLIFY, PULL, DENY Event.register_delete_rule(EventSeries, 'events', PULL) Image.register_delete_rule(BlogPost, 'images', PULL) Image.register_delete_rule(User, 'image', NULLIFY) Image.register_delete_rule(BlogPost, 'featured_image', NULLIFY) Image.register_delete_rule(Event, 'image', NULLIFY) EventSeries.register_delete_rule(Event, 'parent_series', NULLIFY) User.register_delete_rule(Event, 'creator', DENY) User.register_delete_rule(Image, 'creator', DENY) User.register_delete_rule(Post, 'author', DENY) User.register_delete_rule(Post, 'posted_by', DENY)
def run(self): """Run the generation. Uses the configurations passed to func:`__init__`. """ # Setup: db connection, superuser, and printer. connect(config['MONGODB_SETTINGS']['DB']) try: superuser = User.objects().get(gplus_id='super') except DoesNotExist: print ('Failed to get superuser. Try running:\n' '\texport GOOGLE_AUTH_ENABLED=TRUE') printer = ProgressPrinter(self.quiet) # Images if self.should_gen_images: if self.wipe: self.warn('Image') print CLIColor.warning('Wiping Image database.') Image.drop_collection() create_images(12, superuser, printer) # Blog posts if self.should_gen_posts: if self.wipe: self.warn('BlogPost') print CLIColor.warning('Wiping BlogPost database.') BlogPost.drop_collection() create_posts(10, superuser, printer) # Events and event series if self.should_gen_events: if self.wipe: self.warn('Event and EventSeries') print CLIColor.warning('Wiping Event database.') Event.drop_collection() print CLIColor.warning('Wiping EventSeries database.') EventSeries.drop_collection() create_events(superuser, printer)
def _make_series(cls, form, **kwargs): """Create a new :class:`EventSeries` object and save it to Mongoengine. The event is made by creating ``series_data`` and then unpacking it into the constructor for :class:`EventSeries`. :param form: The WTForm form to fetch series data from. :type form: :class:`CreateEventForm` or a subclass. :param dict kwargs: Any other arguments that should be applied on top of the form data. :returns: The newly created series. :rtype: :class:`EventSeries` """ series_data = DataBuilder.series_data_from_form(form) series_data.update(kwargs) cls._validate_series_data(series_data) series_data = cls._remove_none_fields(series_data) series = EventSeries(**series_data) series.save() return series
def _make_series(klass, form, **kwargs): """Create a new :class:`EventSeries` object and save it to Mongoengine. The event is made by creating ``series_data`` and then unpacking it into the constructor for :class:`EventSeries`. :param form: The WTForm form to fetch series data from. :type form: :class:`CreateEventForm` or a subclass. :param dict kwargs: Any other arguments that should be applied on top of the form data. :returns: The newly created series. :rtype: :class:`EventSeries` """ series_data = DataBuilder.series_data_from_form(form) series_data.update(kwargs) klass._validate_series_data(series_data) series_data = klass._remove_none_fields(series_data) series = EventSeries(**series_data) series.save() return series
def register_delete_rules(): """Registers rules for how Mongoengine handles the deletion of objects that are being referenced by other objects. See the documentation for :func:`mongoengine.model.register_delete_rule` for more information. All delete rules for User fields must by DENY, because User objects should never be deleted. Lists of reference fields should PULL, to remove deleted objects from the list, and all others should NULLIFY """ from app.models import Event, EventSeries, User, Post, BlogPost, Image from mongoengine import NULLIFY, PULL, DENY Event.register_delete_rule(EventSeries, 'events', PULL) Image.register_delete_rule(BlogPost, 'images', PULL) Image.register_delete_rule(User, 'image', NULLIFY) Image.register_delete_rule(BlogPost, 'featured_image', NULLIFY) Image.register_delete_rule(Event, 'image', NULLIFY) EventSeries.register_delete_rule(Event, 'parent_series', NULLIFY) User.register_delete_rule(Event, 'creator', DENY) User.register_delete_rule(Image, 'creator', DENY) User.register_delete_rule(Post, 'author', DENY) User.register_delete_rule(Post, 'posted_by', DENY)
def __call__(self, form, field): """Called internally by :mod:`wtforms` on validation of the field. :param form: The parent form :type form: :class:`Form` :param field: The field to validate :type field: :class:`Field` :raises: :class:`wtforms.validators.ValidationError` """ from app.models import Event, EventSeries if EventSeries.objects(slug=field.data).count(): raise ValidationError(self.message) if Event.objects(slug=field.data).count(): raise ValidationError(self.message)
def __call__(self, form, field): """Called internally by :mod:`wtforms` on validation of the field. :param form: The parent form :type form: :class:`Form` :param field: The field to validate :type field: :class:`Field` :raises: :class:`wtforms.validators.ValidationError` """ from app.models import Event, EventSeries # If we change the slug, make sure the new slug doesn't exist if self.original.slug != field.data: if EventSeries.objects(slug=field.data).count(): raise ValidationError(self.message) if Event.objects(slug=field.data).count(): raise ValidationError(self.message)
def make_series(self, slug, num_events): """Create and return a new :class:`~app.models.EventSeries` object using the configuration variables on the ``self`` and any passed configurations. :param str slug: slug for the series :param int num_events: number of occurrences for the series :returns: The blog post. :rtype: :class:`~app.models.Event` """ return EventSeries(frequency='weekly', every=1, slug=slug, ends_on=False, ends_after=True, num_occurrences=num_events, recurrence_summary=self._recurrence_summary())