class TestSchema(StoredObject): _id = StringField(primary=True) # Simple fields intfield = IntegerField(list=False, validate=True) floatfield = FloatField(list=False, validate=True) boolfield = BooleanField(list=False, validate=True) datetimefield = DateTimeField(list=False, validate=True) stringfield = StringField(list=False, validate=True) regexfield = StringField(list=False, validate=RegexValidator('^foo$')) urlfield = StringField(list=False, validate=URLValidator()) int_min_field = IntegerField(validate=MinValueValidator(3)) int_max_field = IntegerField(validate=MaxValueValidator(15)) string_min_field = StringField(validate=MinLengthValidator(3)) string_max_field = StringField(validate=MaxLengthValidator(15)) # List fields # int_list = IntegerField(list=True, validate=MinValueValidator(3)) # float_list = FloatField(list=True, validate=True) # bool_list = BooleanField(list=True, validate=True) # datetime_list = DateTimeField(list=True, default=[]) # string_list = StringField(list=True, validate=True) _meta = {'optimistic': True}
class ForwardNodeSettings(AddonNodeSettingsBase): complete = True has_auth = True url = fields.StringField(validate=URLValidator()) label = fields.StringField(validate=sanitized) @property def link_text(self): return self.label if self.label else self.url def on_delete(self): self.reset() def reset(self): self.url = None self.label = None def after_register(self, node, registration, user, save=True): clone = self.clone() clone.owner = registration clone.on_add() clone.save() return clone, None
class ForwardNodeSettings(AddonNodeSettingsBase): url = fields.StringField(validate=URLValidator()) label = fields.StringField(validate=sanitized) redirect_bool = fields.BooleanField(default=True, validate=True) redirect_secs = fields.IntegerField( default=15, validate=[MinValueValidator(5), MaxValueValidator(60)] ) @property def link_text(self): return self.label if self.label else self.url
class ForwardNodeSettings(AddonNodeSettingsBase): complete = True url = fields.StringField(validate=URLValidator()) label = fields.StringField(validate=sanitized) @property def link_text(self): return self.label if self.label else self.url def on_delete(self): self.reset() def reset(self): self.url = None self.label = None
string_required(item.get('institution')) startMonth = item.get('startMonth') startYear = item.get('startYear') endMonth = item.get('endMonth') endYear = item.get('endYear') if startYear and endYear: if endYear < startYear: raise ValidationValueError( 'End date must be later than start date.') elif endYear == startYear: if endMonth and startMonth and endMonth < startMonth: raise ValidationValueError( 'End date must be later than start date.') validate_url = URLValidator() def validate_personal_site(value): if value: try: validate_url(value) except ValidationError: # Reraise with a better message raise ValidationError('Invalid personal URL.') def validate_social(value): validate_personal_site(value.get('personal'))
class Foo(StoredObject): _id = IntegerField() url_field = StringField(list=False, validate=[URLValidator()])
class ApiOAuth2Application(StoredObject): """Registration and key for user-created OAuth API applications This collection is also used by CAS to create the master list of available applications. Any changes made to field names in this model must be echoed in the CAS implementation. """ _id = fields.StringField(primary=True, default=lambda: str(ObjectId())) # Client ID and secret. Use separate ID field so ID format doesn't have to be restricted to database internals. client_id = fields.StringField( default=lambda: uuid.uuid4(). hex, # Not *guaranteed* unique, but very unlikely unique=True, index=True) client_secret = fields.StringField(default=generate_client_secret) is_active = fields.BooleanField( default=True, # Set to False if application is deactivated index=True) owner = fields.ForeignField('User', index=True, required=True) # User-specified application descriptors name = fields.StringField( index=True, required=True, validate=[string_required, MaxLengthValidator(200)]) description = fields.StringField(required=False, validate=MaxLengthValidator(1000)) date_created = fields.DateTimeField(auto_now_add=True, editable=False) home_url = fields.StringField(required=True, validate=URLValidator()) callback_url = fields.StringField(required=True, validate=URLValidator()) def deactivate(self, save=False): """ Deactivate an ApiOAuth2Application Does not delete the database record, but revokes all tokens and sets a flag that hides this instance from API """ client = cas.get_client() # Will raise a CasHttpError if deletion fails, which will also stop setting of active=False. resp = client.revoke_application_tokens(self.client_id, self.client_secret) # noqa self.is_active = False if save: self.save() return True def reset_secret(self, save=False): """ Reset the secret of an ApiOAuth2Application Revokes all tokens """ client = cas.get_client() client.revoke_application_tokens(self.client_id, self.client_secret) self.client_secret = generate_client_secret() if save: self.save() return True @property def url(self): return '/settings/applications/{}/'.format(self.client_id) @property def absolute_url(self): return urlparse.urljoin(settings.DOMAIN, self.url) # Properties used by Django and DRF "Links: self" field @property def absolute_api_v2_url(self): path = '/applications/{}/'.format(self.client_id) return api_v2_url(path) # used by django and DRF def get_absolute_url(self): return self.absolute_api_v2_url