class URLField(CharField): ''' A :py:class:`CharField` that validates it's input is a valid URL. ''' validators = [validators.URLValidator()]
class PackageURLField(forms.URLField): default_validators = [validators.URLValidator(schemes=["http", "https"])]
class CreateImageForm(forms.SelfHandlingForm): name = forms.CharField(max_length=255, label=_("Name")) description = forms.CharField(widget=forms.widgets.Textarea(attrs={ 'class': 'modal-body-fixed-width', 'rows': 4 }), label=_("Description"), required=False) source_type = forms.ChoiceField( label=_('Image Source'), required=False, choices=[('url', _('Image Location')), ('file', _('Image File'))], widget=forms.Select(attrs={ 'class': 'switchable', 'data-slug': 'source' })) image_url = forms.URLField( label=_("Image Location"), help_text=_("An external (HTTP) URL to load " "the image from."), widget=forms.TextInput( attrs={ 'class': 'switched', 'data-switch-on': 'source', 'data-source-url': _('Image Location'), 'ng-model': 'copyFrom', 'ng-change': 'ctrl.selectImageFormat(copyFrom)' }), validators=[validators.URLValidator(schemes=["http", "https"])], required=False) image_file = forms.FileField( label=_("Image File"), help_text=_("A local image to upload."), widget=forms.FileInput( attrs={ 'class': 'switched', 'data-switch-on': 'source', 'data-source-file': _('Image File'), 'ng-model': 'imageFile', 'ng-change': 'ctrl.selectImageFormat(imageFile.name)', 'image-file-on-change': None }), required=False) kernel = forms.ChoiceField( label=_('Kernel'), required=False, widget=forms.SelectWidget(transform=lambda x: "%s (%s)" % ( x.name, defaultfilters.filesizeformat(x.size)))) ramdisk = forms.ChoiceField( label=_('Ramdisk'), required=False, widget=forms.SelectWidget(transform=lambda x: "%s (%s)" % ( x.name, defaultfilters.filesizeformat(x.size)))) disk_format = forms.ChoiceField( label=_('Format'), choices=[], widget=forms.Select(attrs={ 'class': 'switchable', 'ng-model': 'ctrl.diskFormat' })) architecture = forms.CharField(max_length=255, label=_("Architecture"), required=False) minimum_disk = forms.IntegerField( label=_("Minimum Disk (GB)"), min_value=0, help_text=_('The minimum disk size required to boot the image. ' 'If unspecified, this value defaults to 0 (no minimum).'), required=False) minimum_ram = forms.IntegerField( label=_("Minimum RAM (MB)"), min_value=0, help_text=_('The minimum memory size required to boot the image. ' 'If unspecified, this value defaults to 0 (no minimum).'), required=False) is_copying = forms.BooleanField( label=_("Copy Data"), initial=True, required=False, help_text=_('Specify this option to copy image data to the image ' 'service. If unspecified, image data will be used in its ' 'current location.'), widget=forms.CheckboxInput( attrs={ 'class': 'switched', 'data-source-url': _('Image Location'), 'data-switch-on': 'source' })) is_public = forms.BooleanField(label=_("Public"), required=False) protected = forms.BooleanField(label=_("Protected"), required=False) def __init__(self, request, *args, **kwargs): super(CreateImageForm, self).__init__(request, *args, **kwargs) if (not settings.HORIZON_IMAGES_ALLOW_UPLOAD or not policy.check( (("image", "upload_image"), ), request)): self._hide_file_source_type() if not policy.check((("image", "set_image_location"), ), request): self._hide_url_source_type() if not policy.check((("image", "publicize_image"), ), request): self._hide_is_public() self.fields['disk_format'].choices = IMAGE_FORMAT_CHOICES try: kernel_images = api.glance.image_list_detailed( request, filters={'disk_format': 'aki'})[0] except Exception: kernel_images = [] msg = _('Unable to retrieve image list.') messages.error(request, msg) if kernel_images: choices = [('', _("Choose an image"))] for image in kernel_images: choices.append((image.id, image)) self.fields['kernel'].choices = choices else: del self.fields['kernel'] try: ramdisk_images = api.glance.image_list_detailed( request, filters={'disk_format': 'ari'})[0] except Exception: ramdisk_images = [] msg = _('Unable to retrieve image list.') messages.error(request, msg) if ramdisk_images: choices = [('', _("Choose an image"))] for image in ramdisk_images: choices.append((image.id, image)) self.fields['ramdisk'].choices = choices else: del self.fields['ramdisk'] def _hide_file_source_type(self): self.fields['image_file'].widget = HiddenInput() source_type = self.fields['source_type'] source_type.choices = [ choice for choice in source_type.choices if choice[0] != 'file' ] if len(source_type.choices) == 1: source_type.widget = HiddenInput() def _hide_url_source_type(self): self.fields['image_url'].widget = HiddenInput() source_type = self.fields['source_type'] source_type.choices = [ choice for choice in source_type.choices if choice[0] != 'url' ] if len(source_type.choices) == 1: source_type.widget = HiddenInput() def _hide_is_public(self): self.fields['is_public'].widget = HiddenInput() self.fields['is_public'].initial = False def clean(self): data = super(CreateImageForm, self).clean() # The image_file key can be missing based on particular upload # conditions. Code defensively for it here... image_file = data.get('image_file', None) image_url = data.get('image_url', None) if not image_url and not image_file: raise ValidationError( _("A image or external image location must be specified.")) elif image_url and image_file: raise ValidationError( _("Can not specify both image and external image location.")) else: return data def handle(self, request, data): meta = create_image_metadata(data, False) # Add image source file or URL to metadata if (settings.HORIZON_IMAGES_ALLOW_UPLOAD and policy.check( (("image", "upload_image"), ), request) and data.get('image_file', None)): meta['data'] = self.files['image_file'] elif data['is_copying']: meta['copy_from'] = data['image_url'] else: meta['location'] = data['image_url'] try: image = api.glance.image_create(request, **meta) messages.success( request, _('Your image %s has been queued for creation.') % meta['name']) return image except Exception as e: msg = _('Unable to create new image') # TODO(nikunj2512): Fix this once it is fixed in glance client if hasattr(e, 'code') and e.code == 400: if "Invalid disk format" in e.details: msg = _('Unable to create new image: Invalid disk format ' '%s for image.') % meta['disk_format'] elif "Image name too long" in e.details: msg = _('Unable to create new image: Image name too long.') elif "not supported" in e.details: msg = _('Unable to create new image: URL scheme not ' 'supported.') exceptions.handle(request, msg) return False
from django.core import validators URL_VALIDATORS = [validators.URLValidator()]
def __init__(self, verbose_name=None, name=None, **kwargs): kwargs['max_length'] = kwargs.get('max_length', 200) CharField.__init__(self, verbose_name, name, **kwargs) self.validators.append(validators.URLValidator())
class TLE(models.Model): """TLE database model. Class that models the TLE elements within the database. """ class Meta: app_label = 'fetcher' ordering = ['identifier'] objects = TLEManager() identifier = models.CharField('Spacecraft', max_length=MAX_TLE_ID_LEN, unique=True) timestamp = models.BigIntegerField('Timestamp') source = models.TextField('Source', max_length=100, validators=[validators.URLValidator()]) first_line = models.CharField( 'L1', max_length=MAX_TLE_LINE_LEN, validators=[ validators.RegexValidator(regex=REGEX_TLE_LINE, message="Alphanumeric or '.-_' required", code='invalid_tle_line_1') ]) second_line = models.CharField( 'L2', max_length=MAX_TLE_LINE_LEN, validators=[ validators.RegexValidator(regex=REGEX_TLE_LINE, message="Alphanumeric or '.-_' required", code='invalid_tle_line_2') ]) def dirtyUpdate(self, source, identifier, l1, l2): """ Updates the configuration for this TwoLineEelment with the data provided. :param source: The source for this TLE. :param identifier: The identification line of the TLE (line #0). :param l1: The first line of the TLE (line#1). :param l2: The second line of the TLE (line#2). """ changed = False reason = None if self.identifier != identifier: reason = 'identifier ({}, {})'.format(self.identifier, identifier) self.identifier = identifier changed = True if self.first_line != l1: reason = 'l1: ({}, {})'.format(self.first_line, l1) self.first_line = l1 changed = True if self.second_line != l2: reason = 'l2: ({}, {})'.format(self.second_line, l2) self.second_line = l2 changed = True if self.source != source: logger.debug('Multiple appearance for %s', identifier) self.source = source if changed: self.timestamp = misc.get_utc_timestamp() self.save() raise UpdatedException(self.identifier, reason)
class RTSPURLFormField(FormURLField): default_validators = [validators.URLValidator(schemes=['rtsp'])]
def __init__(self, verify_exists=False, **kwargs): kwargs['max_length'] = kwargs.get('max_length', 2083) super(URLProperty, self).__init__(**kwargs) self.validators.append( validators.URLValidator(verify_exists=verify_exists))
def __init__(self, max_length=None, min_length=None, verify_exists=False, validator_user_agent=validators.URL_VALIDATOR_USER_AGENT, *args, **kwargs): super(URLField, self).__init__(max_length, min_length, *args, **kwargs) self.validators.append(validators.URLValidator(verify_exists=verify_exists, validator_user_agent=validator_user_agent))
def __init__(self, size=None, **kwargs): super().__init__(base_field=models.URLField( _('url'), validators=[validators.URLValidator(schemes=['http', 'https'])]), size=size, **kwargs)
class InstantMessagingField(models.URLField): """Instant messaging field class.""" default_validators = [validators.URLValidator(schemes=im_schemes)]
class Data(models.Model): """ The Data model holds the information related to the url and the data file on the server. """ created = models.DateTimeField(auto_now_add=True) updated = models.DateTimeField(auto_now=True) container = models.ForeignKey(Container, on_delete=models.CASCADE) url = models.TextField(validators=[validators.URLValidator()], null=True) hostname = models.CharField(max_length=500, null=True) # if True the data object is a crawl seed seed = models.BooleanField(default=False) # these fields were saved on the data object of the container. file_id = models.UUIDField(default=uuid.uuid4, editable=False) file_path = models.CharField(max_length=500, blank=True, null=True) title = models.TextField(blank=True, null=True) hash_text = models.CharField(max_length=config.HEXDIGEST_SIZE, blank=True, null=True) # todo(): review the link field. # links = UrlListField() # file_name = models.CharField(max_length=300) # text_url = models.TextField() # checked = models.BooleanField(default=False) @classmethod def get_object(cls, pk: int = None): """Retrieves an object for a given pk.""" try: obj = cls.objects.get(pk=pk) except cls.DoesNotExist as _: raise ValueError(f"Data object with pk: `{pk}` doesn't exist.") return obj @classmethod def create(cls, data: ( str, list, ) = None, containerid: int = None, links: list = None, title: str = None, endpoint: str = None, seed: bool = False): """ Create and save a Data object with all the urls that make it. :param data: :param containerid: :param links: :param title: :param endpoint: :param seed: :return: """ container_obj = Container.get_object(containerid) url_parse = urllib.parse.urlparse(endpoint) obj = cls(title=title, container=container_obj, url=endpoint, seed=seed, hostname=url_parse.hostname) file_path = obj.get_file_path(container=container_obj) obj.file_path = file_path try: hash_text = obj.write_data_to_file(path=file_path, data=data) except DuplicateUrlError as _: return None else: obj.hash_text = hash_text obj.save() for item in links: Link.create(url=item, data=obj) return obj @classmethod def filter_seed_data(cls, cids: typing.List[int]): """ Filters all seed data objects for a list of container ids. :param cids: list of container ids. :return: """ return cls.objects.filter(seed=True, container_id__in=cids) def get_file_path(self, container: Container = None): """ Returns the path of the file as it is saved on disk :return: """ containerid = self.container.pk if not container: container = Container.get_object(containerid) return os.path.normpath( os.path.join(container.container_path(), self.dataid)) def write_data_to_file(self, path, data) -> (str, str): """ Writing data into a file in the container folder. """ if os.path.isfile(path): raise DuplicateUrlError(path) self.save() hash = hashlib.blake2b(digest_size=config.DIGEST_SIZE) with open(path, 'a+') as _file: for txt in data: hash.update(bytes(txt, 'utf-8')) _file.write(txt) _file.write('\n\n') # permissions 'read, write, execute' to user, group, other (777) os.chmod(path, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) return hash.hexdigest() def get_all_links(self): """Returns all the links for a given container id.""" return self.link_set.all() def get_text(self): """Retrieves the text from the file save don disk. :return: """ out = [] with open(self.get_file_path(), 'r') as _file: for line in _file.readlines(): out.append(line) return out @classmethod def delete_many(cls, data_ids: typing.List[int], containerid: int = None): """ Delete many objects for a given containerid and a list of data ids. :param data_ids: :param containerid: :return: """ container = Container.get_object(pk=containerid) for obj in cls.objects.filter(pk__in=data_ids): if container != obj.container: continue _path = obj.file_path if os.path.exists(_path): os.remove(_path) obj.delete() @property def dataid(self): """Returns the file_id as a hex string.""" return self.file_id.hex
def url_or_relative_url_validator(value): if value.startswith('/'): # We transform a relative url into a fictitious absolute url # so we could use built-in url validator. value = 'http://example.com' + value return validators.URLValidator()(value)
# Copyright 2013 Hewlett-Packard Development Company, L.P. # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. from django.core import validators from django.utils.translation import ugettext_lazy as _ # noqa class NotificationType(object): EMAIL = "EMAIL" WEBHOOK = "WEBHOOK" PAGERDUTY = "PAGERDUTY" EMAIL_VALIDATOR = validators.EmailValidator( message=_("Address must contain a valid email address.")) WEBHOOK_VALIDATOR = validators.URLValidator( message=_("Address must contain a valid URL address.")) URL_PREFIX = 'horizon:monitoring:notifications:' TEMPLATE_PREFIX = 'monitoring/notifications/'
class InvenTreeURLFormField(FormURLField): """ Custom URL form field with custom scheme validators """ default_validators = [ validators.URLValidator(schemes=allowable_url_schemes()) ]
class ConfigureForm(forms.Form): """Form to configure the Dynamic DNS client.""" help_update_url = \ gettext_lazy('The Variables <User>, <Pass>, <Ip>, ' '<Domain> may be used within the URL. For details ' 'see the update URL templates of the example providers.') help_service_type = \ gettext_lazy('Please choose an update protocol according to your ' 'provider. If your provider does not support the GnuDIP ' 'protocol or your provider is not listed you may use ' 'the update URL of your provider.') help_server = \ gettext_lazy('Please do not enter a URL here (like ' '"https://example.com/") but only the hostname of the ' 'GnuDIP server (like "example.com").') help_domain = format_lazy(gettext_lazy( 'The public domain name you want to use to reach your ' '{box_name}.'), box_name=gettext_lazy(cfg.box_name)) help_disable_ssl_cert_check = \ gettext_lazy('Use this option if your provider uses self signed ' 'certificates.') help_use_http_basic_auth = \ gettext_lazy('If this option is selected, your username and password ' 'will be used for HTTP basic authentication.') help_password = \ gettext_lazy('Leave this field empty if you want to keep your ' 'current password.') help_ip_lookup_url = format_lazy(gettext_lazy( 'Optional Value. If your {box_name} is not connected ' 'directly to the Internet (i.e. connected to a NAT ' 'router) this URL is used to determine the real ' 'IP address. The URL should simply return the IP where ' 'the client comes from (example: ' 'https://ddns.freedombox.org/ip/).'), box_name=gettext_lazy(cfg.box_name)) help_username = \ gettext_lazy('The username that was used when the account was ' 'created.') provider_choices = (('gnudip', gettext_lazy('GnuDIP')), ('noip.com', 'noip.com'), ('freedns.afraid.org', 'freedns.afraid.org'), ('other', gettext_lazy('Other update URL'))) service_type = forms.ChoiceField(label=gettext_lazy('Service Type'), help_text=help_service_type, choices=provider_choices) server = forms.CharField(label=gettext_lazy('GnuDIP Server Address'), required=False, help_text=help_server, validators=[ validators.RegexValidator( r'^[\w-]{1,63}(\.[\w-]{1,63})*$', gettext_lazy('Invalid server name')) ]) update_url = forms.CharField(label=gettext_lazy('Update URL'), required=False, help_text=help_update_url) disable_ssl_cert_check = forms.BooleanField( label=gettext_lazy('Accept all SSL certificates'), help_text=help_disable_ssl_cert_check, required=False) use_http_basic_auth = forms.BooleanField( label=gettext_lazy('Use HTTP basic authentication'), help_text=help_use_http_basic_auth, required=False) domain = forms.CharField(label=gettext_lazy('Domain Name'), help_text=help_domain, required=True, validators=[ validators.RegexValidator( r'^[\w-]{1,63}(\.[\w-]{1,63})*$', gettext_lazy('Invalid domain name')) ]) username = forms.CharField(label=gettext_lazy('Username'), required=False, help_text=help_username) password = forms.CharField(label=gettext_lazy('Password'), widget=forms.PasswordInput(), required=False, help_text=help_password) show_password = forms.BooleanField(label=gettext_lazy('Show password'), required=False) ip_lookup_url = forms.CharField( label=gettext_lazy('URL to look up public IP'), required=False, help_text=help_ip_lookup_url, validators=[validators.URLValidator(schemes=['http', 'https'])]) use_ipv6 = forms.BooleanField( label=gettext_lazy('Use IPv6 instead of IPv4'), required=False) def clean(self): """Further validate and transform field data.""" cleaned_data = super().clean() # Domain name is not case sensitive, but Let's Encrypt # certificate paths use lower-case domain name. cleaned_data['domain'] = cleaned_data['domain'].lower() update_url = cleaned_data.get('update_url') password = cleaned_data.get('password') service_type = cleaned_data.get('service_type') old_password = self.initial.get('password') if not password: # If password is not set, use old password cleaned_data['password'] = old_password message = _('This field is required.') if service_type == 'gnudip': for field_name in ['server', 'username', 'password']: if not cleaned_data.get(field_name): self.add_error(field_name, message) else: if not update_url: self.add_error('update_url', message) param_map = (('username', '<User>'), ('password', '<Pass>'), ('ip_lookup_url', '<Ip>')) for field_name, param in param_map: if (update_url and param in update_url and not cleaned_data.get(field_name)): self.add_error(field_name, message) if cleaned_data.get('use_http_basic_auth'): for field_name in ('username', 'password'): if not cleaned_data.get(field_name): self.add_error(field_name, message) del cleaned_data['show_password'] return cleaned_data
def __init__(self, **kwargs): if not "validators" in kwargs: kwargs["validators"] = [validators.URLValidator()] super(URLField, self).__init__(**kwargs)
class URLField(CharField): """Django like url field.""" default_validators = [django_validators.URLValidator()] form_class = djangofields.URLField
class Tool(ExportModelOperationsMixin("tool"), SafeDeleteModel): """Description of a tool.""" TOOL_TYPE_CHOICES = ( ("web app", _("web app")), ("desktop app", _("desktop app")), ("bot", _("bot")), ("gadget", _("gadget")), ("user script", _("user script")), ("command line tool", _("command line tool")), ("coding framework", _("coding framework")), ("lua module", _("lua module")), ("template", _("template")), ("other", _("other")), ) ORIGIN_CRAWLER = "crawler" ORIGIN_API = "api" ORIGIN_CHOICES = ( (ORIGIN_CRAWLER, _("crawler")), (ORIGIN_API, _("api")), ) name = models.SlugField( unique=True, max_length=255, allow_unicode=True, help_text=_( "Unique identifier for this tool. Must be unique for every tool. " "It is recommended you prefix your tool names to reduce the risk " "of clashes." ), ) title = models.CharField( max_length=255, help_text=_( "Human readable tool name. Recommended limit of 25 characters." ), ) description = models.TextField( max_length=65535, help_text=_( "A longer description of the tool. " "The recommended length for a description is 3-5 sentences." ), ) url = models.CharField( max_length=2047, validators=[validators.URLValidator(schemes=["http", "https"])], help_text=_( "A direct link to the tool or to instructions on how to use or " "install the tool." ), ) # TODO: Do we even want to persist this info? Valid per spec and stored in # db can be separate things. keywords = JSONSchemaField( blank=True, default=list, schema=schema.KEYWORDS, ) author = JSONSchemaField( blank=True, default=list, help_text=_("The primary tool developers."), schema=schema.schema_for("author", oneof=1), ) repository = BlankAsNullCharField( blank=True, max_length=2047, null=True, help_text=_("A link to the repository where the tool code is hosted."), ) subtitle = BlankAsNullCharField( blank=True, max_length=255, null=True, help_text=_( "Longer than the full title but shorter than the description. " "It should add some additional context to the title." ), ) openhub_id = BlankAsNullCharField( blank=True, max_length=255, null=True, help_text=_( "The project ID on OpenHub. " "Given a URL of https://openhub.net/p/foo, " "the project ID is `foo`." ), ) url_alternates = JSONSchemaField( blank=True, default=list, help_text=_( "Alternate links to the tool or install documentation in " "different natural languages." ), schema=schema.schema_for("url_alternates"), validators=[validate_url_mutilingual_list], ) bot_username = BlankAsNullCharField( blank=True, max_length=255, null=True, help_text=_( "If the tool is a bot, the Wikimedia username of the bot. " "Do not include 'User:'******'this tool works on all Wikisource wikis.' `*` means " "'this works on all wikis, including Wikimedia wikis.'" ), schema=schema.schema_for("for_wikis", oneof=0), ) icon = BlankAsNullCharField( blank=True, max_length=2047, null=True, validators=[ validators.RegexValidator( regex=r"^https://commons\.wikimedia\.org/wiki/File:.+\..+$" ), ], help_text=_( "A link to a Wikimedia Commons file description page for an icon " "that depicts the tool." ), ) license = BlankAsNullCharField( # noqa: A003 blank=True, max_length=255, null=True, help_text=_( "The software license the tool code is available under. " "Use a standard SPDX license identifier like 'GPL-3.0-or-later'." ), validators=[validate_spdx], ) sponsor = JSONSchemaField( blank=True, default=list, help_text=_("Organization(s) that sponsored the tool's development."), schema=schema.schema_for("sponsor", oneof=1), ) available_ui_languages = JSONSchemaField( blank=True, default=list, help_text=_( "The language(s) the tool's interface has been translated into. " "Use ISO 639 language codes like `zh` and `scn`. If not defined " "it is assumed the tool is only available in English." ), schema=schema.schema_for("available_ui_languages", oneof=0), validators=[validate_language_code_list], ) technology_used = JSONSchemaField( blank=True, default=list, help_text=_( "A string or array of strings listing technologies " "(programming languages, development frameworks, etc.) used in " "creating the tool." ), schema=schema.schema_for("technology_used", oneof=1), ) tool_type = BlankAsNullCharField( choices=TOOL_TYPE_CHOICES, blank=True, max_length=32, null=True, help_text=_( "The manner in which the tool is used. " "Select one from the list of options." ), ) api_url = BlankAsNullTextField( blank=True, max_length=2047, null=True, validators=[validators.URLValidator(schemes=["http", "https"])], help_text=_("A link to the tool's API, if available."), ) developer_docs_url = JSONSchemaField( blank=True, default=list, help_text=_( "A link to the tool's developer documentation, if available." ), schema=schema.schema_for("developer_docs_url", oneof=0), validators=[validate_url_mutilingual_list], ) user_docs_url = JSONSchemaField( blank=True, default=list, help_text=_("A link to the tool's user documentation, if available."), schema=schema.schema_for("user_docs_url", oneof=0), validators=[validate_url_mutilingual_list], ) feedback_url = JSONSchemaField( blank=True, default=list, help_text=_( "A link to location where the tool's user can leave feedback." ), schema=schema.schema_for("feedback_url", oneof=0), validators=[validate_url_mutilingual_list], ) privacy_policy_url = JSONSchemaField( blank=True, default=list, help_text=_("A link to the tool's privacy policy, if available."), schema=schema.schema_for("privacy_policy_url", oneof=0), validators=[validate_url_mutilingual_list], ) translate_url = BlankAsNullTextField( blank=True, max_length=2047, null=True, validators=[validators.URLValidator(schemes=["http", "https"])], help_text=_("A link to the tool's translation interface."), ) bugtracker_url = BlankAsNullTextField( blank=True, max_length=2047, null=True, validators=[validators.URLValidator(schemes=["http", "https"])], help_text=_( "A link to the tool's bug tracker on GitHub, Bitbucket, " "Phabricator, etc." ), ) _schema = BlankAsNullCharField( blank=True, max_length=32, null=True, help_text=_( "A URI identifying the jsonschema for this toolinfo.json record. " "This should be a short uri containing only the name and revision " "at the end of the URI path." ), ) _language = BlankAsNullCharField( blank=True, max_length=16, null=True, validators=[validate_language_code], help_text=_( "The language in which this toolinfo record is written. " "If not set, the default value is English. " "Use ISO 639 language codes." ), ) origin = models.CharField( choices=ORIGIN_CHOICES, max_length=32, default=ORIGIN_CRAWLER, help_text=_("Origin of this tool record."), ) created_by = models.ForeignKey( settings.AUTH_USER_MODEL, related_name="tools", db_index=True, on_delete=models.CASCADE, ) created_date = models.DateTimeField( auto_now_add=True, editable=False, db_index=True ) modified_by = models.ForeignKey( settings.AUTH_USER_MODEL, related_name="+", null=True, blank=True, on_delete=models.SET_NULL, ) modified_date = models.DateTimeField( auto_now=True, editable=False, db_index=True ) objects = ToolManager() def __str__(self): """Str repr""" return self.name @property def auditlog_label(self): """Get label for use in auditlog output.""" return self.name
def __init__(self, max_length=None, min_length=None, *args, **kwargs): super(URLField, self).__init__(max_length, min_length, *args, **kwargs) self.validators.append(validators.URLValidator())
# Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # import logging import json from datetime import datetime from django import forms from django.core import validators from django.conf import settings DOMAIN_VALIDATOR = validators.URLValidator(schemes=['']) VALID_TLDS = set([ # version 2016020800, last updated mon feb 8 07:07:01 2016 utc 'aaa', 'aarp', 'abb', 'abbott', 'abogado', 'ac', 'academy', 'accenture', 'accountant', 'accountants', 'aco', 'active',
class CompanyForm(forms.Form): # id = forms.IntegerField(widget=forms.HiddenInput(), label='') id = forms.IntegerField(label='id', initial = 0, widget = forms.HiddenInput() ) type_id = forms.ModelChoiceField( queryset = Type.objects.all(), to_field_name = "id", empty_label = "Пусто", label = "Форма собственности", required = True, widget = forms.Select( attrs = { 'class': 'form-control' })) name = forms.CharField( max_length=100, label="Название компани", error_messages={'required': 'Поле обязательное для заполнеия'}, required = True, widget = forms.TextInput( attrs = { 'placeholder': 'Рога и копыта', 'class': 'form-control' })) ogrn = forms.CharField( max_length=15, label="ОГРН", required = True, help_text = 'Число из 15 знаков', validators=[clean_ogrn], widget = forms.TextInput( attrs = { 'placeholder': '773452345674', 'class': 'form-control' })) inn = forms.CharField( max_length=10, label="ИНН", validators=[clean_inn], required = True, widget = forms.TextInput( attrs = { 'placeholder': '773452345674', 'class': 'form-control' })) telephon = forms.CharField( max_length=50, label="Номер телефона", validators=[clean_telephon], required = True, help_text = 'Телефон компании', widget = forms.TextInput( attrs = { 'placeholder': '+79160146441', 'class': 'form-control' })) logo = forms.ImageField( label="Логотип компании", required = False, validators=[file_size],) url = forms.CharField( label="Адрес сайта", help_text = 'Корпоративный сайт компании', validators=[validators.URLValidator()], initial = 'http://', widget = forms.URLInput( attrs = { 'class': 'form-control', })) text = forms.CharField( label="Дополнительная информация", required = False, validators=[validators.MaxLengthValidator(500)], widget=forms.Textarea( attrs = { 'placeholder': 'Допускается объем текста не более 500 символов', 'class': 'form-control', 'rows':3, }))
class ConfigureForm(forms.Form): """Form to configure the Dynamic DNS client.""" help_update_url = \ ugettext_lazy('The Variables <User>, <Pass>, <Ip>, ' '<Domain> may be used within the URL. For details ' 'see the update URL templates of the example providers.') help_services = \ ugettext_lazy('Please choose an update protocol according to your ' 'provider. If your provider does not support the GnuDIP ' 'protocol or your provider is not listed you may use the ' 'update URL of your provider.') help_server = \ ugettext_lazy('Please do not enter a URL here (like ' '"https://example.com/") but only the hostname of the ' 'GnuDIP server (like "example.com").') help_domain = format_lazy(ugettext_lazy( 'The public domain name you want to use to reach your ' '{box_name}.'), box_name=ugettext_lazy(cfg.box_name)) help_disable_ssl = \ ugettext_lazy('Use this option if your provider uses self signed ' 'certificates.') help_http_auth = \ ugettext_lazy('If this option is selected, your username and password ' 'will be used for HTTP basic authentication.') help_secret = \ ugettext_lazy('Leave this field empty if you want to keep your ' 'current password.') help_ip_url = format_lazy(ugettext_lazy( 'Optional Value. If your {box_name} is not connected ' 'directly to the Internet (i.e. connected to a NAT ' 'router) this URL is used to determine the real ' 'IP address. The URL should simply return the IP where ' 'the client comes from (example: ' 'http://myip.datasystems24.de).'), box_name=ugettext_lazy(cfg.box_name)) help_user = \ ugettext_lazy('The username that was used when the account was ' 'created.') """ToDo: sync this list with the html template file""" provider_choices = (('GnuDIP', 'GnuDIP'), ('noip', 'noip.com'), ('selfhost', 'selfhost.bz'), ('freedns', 'freedns.afraid.org'), ('other', 'other update URL')) enabled = forms.BooleanField(label=ugettext_lazy('Enable Dynamic DNS'), required=False) service_type = forms.ChoiceField(label=ugettext_lazy('Service Type'), help_text=help_services, choices=provider_choices) dynamicdns_server = TrimmedCharField( label=ugettext_lazy('GnuDIP Server Address'), required=False, help_text=help_server, validators=[ validators.RegexValidator(r'^[\w-]{1,63}(\.[\w-]{1,63})*$', ugettext_lazy('Invalid server name')) ]) dynamicdns_update_url = TrimmedCharField(label=ugettext_lazy('Update URL'), required=False, help_text=help_update_url) disable_SSL_cert_check = forms.BooleanField( label=ugettext_lazy('Accept all SSL certificates'), help_text=help_disable_ssl, required=False) use_http_basic_auth = forms.BooleanField( label=ugettext_lazy('Use HTTP basic authentication'), help_text=help_http_auth, required=False) dynamicdns_domain = TrimmedCharField( label=ugettext_lazy('Domain Name'), help_text=help_domain, required=False, validators=[ validators.RegexValidator(r'^[\w-]{1,63}(\.[\w-]{1,63})*$', ugettext_lazy('Invalid domain name')) ]) dynamicdns_user = TrimmedCharField(label=ugettext_lazy('Username'), required=False, help_text=help_user) dynamicdns_secret = TrimmedCharField(label=ugettext_lazy('Password'), widget=forms.PasswordInput(), required=False, help_text=help_secret) showpw = forms.BooleanField(label=ugettext_lazy('Show password'), required=False) dynamicdns_ipurl = TrimmedCharField( label=ugettext_lazy('URL to look up public IP'), required=False, help_text=help_ip_url, validators=[validators.URLValidator(schemes=['http', 'https', 'ftp'])]) def clean(self): cleaned_data = super(ConfigureForm, self).clean() dynamicdns_secret = cleaned_data.get('dynamicdns_secret') dynamicdns_update_url = cleaned_data.get('dynamicdns_update_url') dynamicdns_user = cleaned_data.get('dynamicdns_user') dynamicdns_domain = cleaned_data.get('dynamicdns_domain') dynamicdns_server = cleaned_data.get('dynamicdns_server') service_type = cleaned_data.get('service_type') old_dynamicdns_secret = self.initial['dynamicdns_secret'] # Clear the fields which are not in use if service_type == 'GnuDIP': dynamicdns_update_url = '' else: dynamicdns_server = '' if cleaned_data.get('enabled'): # Check if gnudip server or update URL is filled if not dynamicdns_update_url and not dynamicdns_server: raise forms.ValidationError( _('Please provide an update URL or a GnuDIP server ' 'address')) if dynamicdns_server and not dynamicdns_user: raise forms.ValidationError( _('Please provide a GnuDIP username')) if dynamicdns_server and not dynamicdns_domain: raise forms.ValidationError( _('Please provide a GnuDIP domain name')) # Check if a password was set before or a password is set now if dynamicdns_server and \ not dynamicdns_secret and not old_dynamicdns_secret: raise forms.ValidationError(_('Please provide a password'))
class MainForm(forms.Form): url = forms.URLField(label=False, validators=[validators.URLValidator()])
def __init__(self, schemes=('http', 'https'), **kwargs): if 'validators' not in kwargs: kwargs['validators'] = (validators.URLValidator(schemes=schemes), ) super(URLField, self).__init__(**kwargs)
# USER_MOBILE_SYNC_SERVER_DOMAIN, USER_MOBILE_SYNC_SERVER_LOGIN, # USER_MOBILE_SYNC_SERVER_PWD, USER_MOBILE_SYNC_SERVER_SSL, USER_MOBILE_SYNC_SERVER_URL) from .errors import (CremeActiveSyncError, SYNC_ERR_WRONG_CFG_NO_SERVER_URL, SYNC_ERR_WRONG_CFG_NO_LOGIN, SYNC_ERR_WRONG_CFG_NO_PWD, SYNC_ERR_ABORTED, SYNC_ERR_WRONG_CFG_INVALID_SERVER_URL) from .mappings import FOLDERS_TYPES_CREME_TYPES_MAPPING, CREME_AS_MAPPING from .messages import MessageInfo, MessageSucceed, MessageError, _INFO, _ERROR, _SUCCESS from .models import CremeClient, AS_Folder from .utils import is_user_sync_calendars, is_user_sync_contacts, get_default_server_setting_values INFO = 'info' ERROR = 'error' SUCCESS = 'success' url_validator = validators.URLValidator() # ACTIVE_SYNC_DEBUG = settings.ACTIVE_SYNC_DEBUG class Synchronization: """ TODO: Handle SSL & Domain """ def __init__(self, user, *args, **kwargs): self.user = user self.client = CremeClient.objects.get_or_create(user=user)[0] self.client_id = self.client.client_id self.policy_key = self.client.policy_key self.last_sync = self.client.last_sync self.contact_folder_id = self.client.contact_folder_id
def __init__(self, **kwargs): if not 'validators' in kwargs: kwargs['validators'] = [validators.URLValidator()] super(URLField, self).__init__(**kwargs)
def __init__(self, **kwargs): kwargs['max_length'] = kwargs.get('max_length', 200) kwargs['validators'] = [validators.URLValidator()] super(URLField, self).__init__(**kwargs)
class URLValue(ValidationMixin, Value): message = 'Cannot interpret URL value {0!r}' validator = validators.URLValidator()
def __init__(self, **kwargs): super(InstantMessagingField, self).__init__(**kwargs) validator = validators.URLValidator( schemes=im_schemes, message=self.error_messages['invalid']) self.validators.append(validator)