Esempio n. 1
0
import subprocess
import os
import codecs
from django.core.validators import URLValidator
from .settings import pdf_settings
from django.http import (HttpResponse, Http404)
from django.core.files.base import ContentFile
from .models import PdfDoc
from .utils import get_random_filename

validate_url = URLValidator(schemes=['https', 'http'])


class PDFGenerator(object):
    def __init__(self,
                 url,
                 paperformat='A4',
                 zoom=1,
                 script=pdf_settings.DEFAULT_RASTERIZE_SCRIPT,
                 temp_dir=pdf_settings.DEFAULT_TEMP_DIR,
                 header='',
                 footer='',
                 margin='0cm',
                 orientation='portrait'):
        validate_url(url)
        self.script = script
        self.temp_dir = temp_dir
        self.url = url
        self.filename = self.__get_random_filename()
        self.filepath = self.__get_filepath()
        self.paperformat = paperformat
Esempio n. 2
0
    (MinValueValidator(-10), 0, None),
    (MinValueValidator(NOW), NOW, None),
    (MinValueValidator(NOW), NOW + timedelta(days=1), None),
    (MinValueValidator(0), -1, ValidationError),
    (MinValueValidator(NOW), NOW - timedelta(days=1), ValidationError),

    # limit_value may be a callable.
    (MinValueValidator(lambda: 1), 0, ValidationError),
    (MinValueValidator(lambda: 1), 1, None),
    (MaxLengthValidator(10), '', None),
    (MaxLengthValidator(10), 10 * 'x', None),
    (MaxLengthValidator(10), 15 * 'x', ValidationError),
    (MinLengthValidator(10), 15 * 'x', None),
    (MinLengthValidator(10), 10 * 'x', None),
    (MinLengthValidator(10), '', ValidationError),
    (URLValidator(EXTENDED_SCHEMES), 'file://localhost/path', None),
    (URLValidator(EXTENDED_SCHEMES), 'git://example.com/', None),
    (URLValidator(EXTENDED_SCHEMES),
     'git+ssh://[email protected]/example/hg-git.git', None),
    (URLValidator(EXTENDED_SCHEMES), 'git://-invalid.com', ValidationError),
    (URLValidator(), None, ValidationError),
    (URLValidator(), 56, ValidationError),
    (URLValidator(), 'no_scheme', ValidationError),
    # Newlines and tabs are not accepted.
    (URLValidator(), 'http://www.djangoproject.com/\n', ValidationError),
    (URLValidator(), 'http://[::ffff:192.9.5.5]\n', ValidationError),
    (URLValidator(), 'http://www.djangoproject.com/\r', ValidationError),
    (URLValidator(), 'http://[::ffff:192.9.5.5]\r', ValidationError),
    (URLValidator(), 'http://www.django\rproject.com/', ValidationError),
    (URLValidator(), 'http://[::\rffff:192.9.5.5]', ValidationError),
    (URLValidator(), 'http://\twww.djangoproject.com/', ValidationError),
    def handle(self, **options):

        if not settings.MEDIA_ROOT:
            raise ImproperlyConfigured(
                'Please set MEDIA_ROOT in your settings file')

        remote_url = options['remote_url']
        try:
            val = URLValidator()
            val(remote_url)
        except ValidationError:
            raise CommandError('Please enter a valid URL')

        headers = {
            'User-agent':
            default_user_agent('wger/{} + requests'.format(get_version()))
        }

        # Get all images
        page = 1
        all_images_processed = False
        result = requests.get(IMAGE_API.format(remote_url),
                              headers=headers).json()
        self.stdout.write('*** Processing images ***')
        while not all_images_processed:
            self.stdout.write('')
            self.stdout.write(f'*** Page {page}')
            self.stdout.write('')
            page += 1

            if result['next']:
                result = requests.get(result['next'], headers=headers).json()
            else:
                all_images_processed = True

            for image_data in result['results']:
                image_uuid = image_data['uuid']

                self.stdout.write(f'Processing image {image_uuid}')

                try:
                    exercise_base = ExerciseBase.objects.get(
                        id=image_data['exercise_base'])
                except ExerciseBase.DoesNotExist:
                    self.stdout.write(
                        '    Remote exercise base not found in local DB, skipping...'
                    )
                    continue

                try:
                    image = ExerciseImage.objects.get(uuid=image_uuid)
                    self.stdout.write(
                        '    Image already present locally, skipping...')
                    continue
                except ExerciseImage.DoesNotExist:
                    self.stdout.write(
                        '    Image not found in local DB, creating now...')
                    image = ExerciseImage()
                    image.uuid = image_uuid

                # Save the downloaded image
                # http://stackoverflow.com/questions/1308386/programmatically-saving-image-to-
                retrieved_image = requests.get(image_data['image'],
                                               headers=headers)

                # Temporary files on windows don't support the delete attribute
                if os.name == 'nt':
                    img_temp = NamedTemporaryFile()
                else:
                    img_temp = NamedTemporaryFile(delete=True)
                img_temp.write(retrieved_image.content)
                img_temp.flush()

                image.exercise = exercise_base
                image.is_main = image_data['is_main']
                image.status = image_data['status']
                image.image.save(
                    os.path.basename(os.path.basename(image_data['image'])),
                    File(img_temp),
                )
                image.save()
                self.stdout.write(self.style.SUCCESS('    successfully saved'))
Esempio n. 4
0
def get_labels_for_uris(uri_list, scheme_uri, lang, acronyms=False):
    '''
    This methods gathers labels for concept in thesaurus.
    
    :param uri_list: The list of uris of thesaurus entries.    
    :param scheme_uri: The scheme (uri) of the thesaurus 
    :param lang: The language for the label
    :param acronyms: do the labels must include the acronym (altLabel) 
    '''
    query_without_acronym = """
PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl:<http://www.w3.org/2002/07/owl#>
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
SELECT ?uri ?label
WHERE {
    ?uri skos:inScheme <%s> .
    ?uri skos:prefLabel|skos:label ?label .
    FILTER (%s)
}
"""
    query_with_acronym = """
PREFIX skos:<http://www.w3.org/2004/02/skos/core#>
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl:<http://www.w3.org/2002/07/owl#>
PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
SELECT ?uri ?label ?acro
WHERE {
    ?uri skos:inScheme <%s> .
    ?uri skos:prefLabel|skos:label ?label .
    OPTIONAL { ?uri skos:altLabel ?acro }
    FILTER (%s)
}
"""
    if acronyms:
        query = query_with_acronym
    else:
        query = query_without_acronym
    res_dict = {}
    if not uri_list:
        return res_dict
    # We build the filter string
    filter_str = ""
    validate = URLValidator()
    for i, uri in enumerate(uri_list):
        res_dict[uri] = ""
        # We test if the uri is correct. If not, all the sparql request fails
        try:
            validate(uri)
        except:
            continue
        uri = uri.replace(" ", "")  # avoid bug when only few urls are not good
        filter_str += (" || ?uri = <" + uri + ">") if i else ("?uri = <" +
                                                              uri + ">")
    # We request the labels
    res = requests.get(
        settings.SPARQL_QUERY_ENDPOINT,
        params={
            'query': query % (scheme_uri, filter_str),
            'timeout': 10
        },  #, '$root' : "<"+uri+">"},
        headers={'accept': 'application/sparql-results+json'},
    )
    if res.ok and res.text:
        json_res = res.json()
        if 'results' in json_res and 'bindings' in json_res['results'] and len(
                json_res['results']['bindings']) > 0:
            # json_res['results']['bindings'] has several languages. If we find french, we save the french label.
            # If not, we save the first one.
            tmp_dict = {}
            first_label = None
            # We create a temporary dict with the lang code and the label
            for b in json_res['results']['bindings']:
                if lang:
                    if 'label' in b and 'value' in b[
                            'label'] and 'xml:lang' in b['label']:
                        tmp_dict[b['label']['xml:lang']] = b['label']['value']
                        if not first_label:
                            first_label = b['label']['value']
                else:
                    if 'acro' in b and 'value' in b['acro']:
                        first_label = b['acro']['value'] + ". " + b['label'][
                            'value']
                    else:
                        first_label = b['label']['value']
                if lang in tmp_dict or first_label:
                    if lang in tmp_dict:
                        label = tmp_dict[lang]
                    else:
                        label = first_label
                    res_dict[b['uri']['value']] = label
    return res_dict
Esempio n. 5
0
                # check past authorizations regarded the same scopes as the current one
                for token in tokens:
                    if token.allow_scopes(scopes):
                        success_url = self.get_authorization_redirect_url(
                            " ".join(kwargs['scopes']), credentials)
                        return Response({'success_url': success_url})

            return Response(kwargs)

        except OAuthToolkitError as error:
            return Response({'error': error.oauthlib_error.description},
                            status=HTTP_400_BAD_REQUEST)


httpsUrlValidator = URLValidator(message="Must be a valid HTTPS URI",
                                 schemes=['https'])


class RegistrationSerializer(serializers.Serializer):
    client_name = serializers.CharField(required=True, source='name')
    client_uri = serializers.URLField(required=True,
                                      source='applicationinfo.website_url',
                                      validators=[httpsUrlValidator])
    logo_uri = serializers.URLField(required=True,
                                    source='applicationinfo.logo_uri',
                                    validators=[httpsUrlValidator])
    tos_uri = serializers.URLField(required=True,
                                   source='applicationinfo.terms_uri',
                                   validators=[httpsUrlValidator])
    policy_uri = serializers.URLField(required=True,
                                      source='applicationinfo.policy_uri',
Esempio n. 6
0
 def validate_mockable_args(self, wp_site_url):
     """ Call validators in an independant function to allow mocking them """
     if Utils.get_domain(wp_site_url) != settings.HTTPD_CONTAINER_NAME:
         URLValidator()(wp_site_url)
Esempio n. 7
0
def support(request: HttpRequest) -> HttpResponse:
    context: Dict[str, Any] = {}

    if "success_message" in request.session:
        context["success_message"] = request.session["success_message"]
        del request.session["success_message"]

    if settings.BILLING_ENABLED and request.method == "POST":
        # We check that request.POST only has two keys in it: The
        # realm_id and a field to change.
        keys = set(request.POST.keys())
        if "csrfmiddlewaretoken" in keys:
            keys.remove("csrfmiddlewaretoken")
        if len(keys) != 2:
            raise JsonableError(_("Invalid parameters"))

        realm_id: str = assert_is_not_none(request.POST.get("realm_id"))
        realm = Realm.objects.get(id=realm_id)

        acting_user = request.user
        assert isinstance(acting_user, UserProfile)
        if request.POST.get("plan_type", None) is not None:
            new_plan_type = int(
                assert_is_not_none(request.POST.get("plan_type")))
            current_plan_type = realm.plan_type
            do_change_plan_type(realm, new_plan_type, acting_user=acting_user)
            msg = f"Plan type of {realm.string_id} changed from {get_plan_name(current_plan_type)} to {get_plan_name(new_plan_type)} "
            context["success_message"] = msg
        elif request.POST.get("discount", None) is not None:
            new_discount = Decimal(
                assert_is_not_none(request.POST.get("discount")))
            current_discount = get_discount_for_realm(realm) or 0
            attach_discount_to_realm(realm,
                                     new_discount,
                                     acting_user=acting_user)
            context[
                "success_message"] = f"Discount of {realm.string_id} changed to {new_discount}% from {current_discount}%."
        elif request.POST.get("new_subdomain", None) is not None:
            new_subdomain: str = assert_is_not_none(
                request.POST.get("new_subdomain"))
            old_subdomain = realm.string_id
            try:
                check_subdomain_available(new_subdomain)
            except ValidationError as error:
                context["error_message"] = error.message
            else:
                do_change_realm_subdomain(realm,
                                          new_subdomain,
                                          acting_user=acting_user)
                request.session[
                    "success_message"] = f"Subdomain changed from {old_subdomain} to {new_subdomain}"
                return HttpResponseRedirect(
                    reverse("support") + "?" + urlencode({"q": new_subdomain}))
        elif request.POST.get("status", None) is not None:
            status = request.POST.get("status")
            if status == "active":
                do_send_realm_reactivation_email(realm,
                                                 acting_user=acting_user)
                context[
                    "success_message"] = f"Realm reactivation email sent to admins of {realm.string_id}."
            elif status == "deactivated":
                do_deactivate_realm(realm, acting_user=acting_user)
                context["success_message"] = f"{realm.string_id} deactivated."
        elif request.POST.get("billing_method", None) is not None:
            billing_method = request.POST.get("billing_method")
            if billing_method == "send_invoice":
                update_billing_method_of_current_plan(
                    realm, charge_automatically=False, acting_user=acting_user)
                context[
                    "success_message"] = f"Billing method of {realm.string_id} updated to pay by invoice."
            elif billing_method == "charge_automatically":
                update_billing_method_of_current_plan(
                    realm, charge_automatically=True, acting_user=acting_user)
                context[
                    "success_message"] = f"Billing method of {realm.string_id} updated to charge automatically."
        elif request.POST.get("sponsorship_pending", None) is not None:
            sponsorship_pending = request.POST.get("sponsorship_pending")
            if sponsorship_pending == "true":
                update_sponsorship_status(realm, True, acting_user=acting_user)
                context[
                    "success_message"] = f"{realm.string_id} marked as pending sponsorship."
            elif sponsorship_pending == "false":
                update_sponsorship_status(realm,
                                          False,
                                          acting_user=acting_user)
                context[
                    "success_message"] = f"{realm.string_id} is no longer pending sponsorship."
        elif request.POST.get("approve_sponsorship") is not None:
            if request.POST.get(
                    "approve_sponsorship") == "approve_sponsorship":
                approve_sponsorship(realm, acting_user=acting_user)
                context[
                    "success_message"] = f"Sponsorship approved for {realm.string_id}"
        elif request.POST.get("downgrade_method", None) is not None:
            downgrade_method = request.POST.get("downgrade_method")
            if downgrade_method == "downgrade_at_billing_cycle_end":
                downgrade_at_the_end_of_billing_cycle(realm)
                context[
                    "success_message"] = f"{realm.string_id} marked for downgrade at the end of billing cycle"
            elif downgrade_method == "downgrade_now_without_additional_licenses":
                downgrade_now_without_creating_additional_invoices(realm)
                context[
                    "success_message"] = f"{realm.string_id} downgraded without creating additional invoices"
            elif downgrade_method == "downgrade_now_void_open_invoices":
                downgrade_now_without_creating_additional_invoices(realm)
                voided_invoices_count = void_all_open_invoices(realm)
                context[
                    "success_message"] = f"{realm.string_id} downgraded and voided {voided_invoices_count} open invoices"
        elif request.POST.get("scrub_realm", None) is not None:
            if request.POST.get("scrub_realm") == "scrub_realm":
                do_scrub_realm(realm, acting_user=acting_user)
                context["success_message"] = f"{realm.string_id} scrubbed."

    query = request.GET.get("q", None)
    if query:
        key_words = get_invitee_emails_set(query)

        users = set(UserProfile.objects.filter(delivery_email__in=key_words))
        realms = set(Realm.objects.filter(string_id__in=key_words))

        for key_word in key_words:
            try:
                URLValidator()(key_word)
                parse_result = urllib.parse.urlparse(key_word)
                hostname = parse_result.hostname
                assert hostname is not None
                if parse_result.port:
                    hostname = f"{hostname}:{parse_result.port}"
                subdomain = get_subdomain_from_hostname(hostname)
                try:
                    realms.add(get_realm(subdomain))
                except Realm.DoesNotExist:
                    pass
            except ValidationError:
                users.update(
                    UserProfile.objects.filter(full_name__iexact=key_word))

        for realm in realms:
            realm.customer = get_customer_by_realm(realm)

            current_plan = get_current_plan_by_realm(realm)
            if current_plan is not None:
                new_plan, last_ledger_entry = make_end_of_cycle_updates_if_needed(
                    current_plan, timezone_now())
                if last_ledger_entry is not None:
                    if new_plan is not None:
                        realm.current_plan = new_plan
                    else:
                        realm.current_plan = current_plan
                    realm.current_plan.licenses = last_ledger_entry.licenses
                    realm.current_plan.licenses_used = get_latest_seat_count(
                        realm)

        # full_names can have , in them
        users.update(UserProfile.objects.filter(full_name__iexact=query))

        context["users"] = users
        context["realms"] = realms

        confirmations: List[Dict[str, Any]] = []

        preregistration_users = PreregistrationUser.objects.filter(
            email__in=key_words)
        confirmations += get_confirmations(
            [
                Confirmation.USER_REGISTRATION, Confirmation.INVITATION,
                Confirmation.REALM_CREATION
            ],
            preregistration_users,
            hostname=request.get_host(),
        )

        multiuse_invites = MultiuseInvite.objects.filter(realm__in=realms)
        confirmations += get_confirmations([Confirmation.MULTIUSE_INVITE],
                                           multiuse_invites)

        confirmations += get_confirmations([Confirmation.REALM_REACTIVATION],
                                           [realm.id for realm in realms])

        context["confirmations"] = confirmations

    def get_realm_owner_emails_as_string(realm: Realm) -> str:
        return ", ".join(realm.get_human_owner_users().order_by(
            "delivery_email").values_list("delivery_email", flat=True))

    def get_realm_admin_emails_as_string(realm: Realm) -> str:
        return ", ".join(
            realm.get_human_admin_users(include_realm_owners=False).order_by(
                "delivery_email").values_list("delivery_email", flat=True))

    context[
        "get_realm_owner_emails_as_string"] = get_realm_owner_emails_as_string
    context[
        "get_realm_admin_emails_as_string"] = get_realm_admin_emails_as_string
    context["get_discount_for_realm"] = get_discount_for_realm
    context["get_org_type_display_name"] = get_org_type_display_name
    context["realm_icon_url"] = realm_icon_url
    context["Confirmation"] = Confirmation
    return render(request, "analytics/support.html", context=context)
Esempio n. 8
0
    def validate(self, data):
        # print('EndpointSerialize.validate')
        port_re = "(:[0-9]{1,5}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}" \
                  "|655[0-2][0-9]|6553[0-5])"

        if not self.context['request'].method == 'PATCH':
            if ('host' not in data or 'protocol' not in data
                    or 'path' not in data or 'query' not in data
                    or 'fragment' not in data):
                raise serializers.ValidationError(
                    'Please provide valid host, protocol, path, query and '
                    'fragment')
            protocol = data['protocol']
            path = data['path']
            query = data['query']
            fragment = data['fragment']
            host = data['host']
        else:
            protocol = data.get('protocol', self.instance.protocol)
            path = data.get('path', self.instance.path)
            query = data.get('query', self.instance.query)
            fragment = data.get('fragment', self.instance.fragment)
            host = data.get('host', self.instance.host)
        product = data.get('product', None)

        from urllib.parse import urlunsplit
        if protocol:
            endpoint = urlunsplit((protocol, host, path, query, fragment))
        else:
            endpoint = host

        from django.core import exceptions
        from django.core.validators import RegexValidator
        import re
        try:
            url_validator = URLValidator()
            url_validator(endpoint)
        except exceptions.ValidationError:
            try:
                # do we have a port number?
                regex = re.compile(port_re)
                host = endpoint
                if regex.findall(endpoint):
                    for g in regex.findall(endpoint):
                        host = re.sub(port_re, '', host)
                validate_ipv46_address(host)
            except exceptions.ValidationError:
                try:
                    validate_hostname = RegexValidator(
                        regex=r'[a-zA-Z0-9-_]*\.[a-zA-Z]{2,6}')
                    # do we have a port number?
                    regex = re.compile(port_re)
                    host = endpoint
                    if regex.findall(endpoint):
                        for g in regex.findall(endpoint):
                            host = re.sub(port_re, '', host)
                    validate_hostname(host)
                except:  # noqa
                    raise serializers.ValidationError(
                        'It does not appear as though this endpoint is a '
                        'valid URL or IP address.',
                        code='invalid')

        endpoint = Endpoint.objects.filter(protocol=protocol,
                                           host=host,
                                           path=path,
                                           query=query,
                                           fragment=fragment,
                                           product=product)
        if endpoint.count() > 0 and not self.instance:
            raise serializers.ValidationError(
                'It appears as though an endpoint with this data already '
                'exists for this product.',
                code='invalid')

        return data
Esempio n. 9
0
    def clean(self):
        number_message = "Enter a number."

        scale_preset = self.cleaned_data.get('scale_preset', '')
        if scale_preset == '5':
            # custom preset error handling
            scale_type = self.cleaned_data.get('scale_type', '')
            if scale_type == 'ul':
                scale_lower = self.cleaned_data.get('scale_lower')
                scale_upper = self.cleaned_data.get('scale_upper')
                if not scale_lower:
                    self._errors['scale_lower'] = self.error_class(
                        [number_message])
                if not scale_upper:
                    self._errors['scale_upper'] = self.error_class(
                        [number_message])
            elif scale_type == 'ev':
                scale_est = self.cleaned_data.get('scale_est')
                scale_err = self.cleaned_data.get('scale_err')
                if not scale_est:
                    self._errors['scale_est'] = self.error_class(
                        [number_message])
                if not scale_err:
                    self._errors['scale_err'] = self.error_class(
                        [number_message])
        else:
            # if scale isn't custom, use preset settings
            self.cleaned_data['scale_type'] = 'ul'
            self.cleaned_data['scale_units'] = 'degwidth'
            self.cleaned_data['scale_lower'] = self.SCALE_PRESET_SETTINGS[
                scale_preset][0]
            self.cleaned_data['scale_upper'] = self.SCALE_PRESET_SETTINGS[
                scale_preset][1]

        center_ra = self.cleaned_data.get('center_ra')
        center_dec = self.cleaned_data.get('center_dec')
        radius = self.cleaned_data.get('radius')
        if center_ra or center_dec or radius:
            if not center_ra:
                self._errors['center_ra'] = self.error_class([number_message])
            if not center_dec:
                self._errors['center_dec'] = self.error_class([number_message])
            if not radius:
                self._errors['radius'] = self.error_class([number_message])

        tweak_order = self.cleaned_data.get('tweak_order')
        if tweak_order < 0 or tweak_order > 9:
            self._errors['tweak_order'] = self.error_class(
                ['Tweak order must be between 0 and 9'])

        upload_type = self.cleaned_data.get('upload_type', '')
        if upload_type == 'file':
            if not self.cleaned_data.get('file'):
                raise forms.ValidationError(
                    "You must select a file to upload.")
        elif upload_type == 'url':
            url = self.cleaned_data.get('url', '')
            if not (url.startswith('http://') or url.startswith('ftp://')):
                url = 'http://' + url
            if url.startswith('http://http://') or url.startswith(
                    'http://ftp://'):
                url = url[7:]
            if len(url) == 0:
                raise forms.ValidationError("You must enter a url to upload.")
            urlvalidator = URLValidator()
            try:
                urlvalidator(url)
            except forms.ValidationError:
                raise forms.ValidationError("You must enter a valid url.")
            self.cleaned_data['url'] = url

        return self.cleaned_data
Esempio n. 10
0
from django.conf import settings
from django.core import exceptions
from django.http import QueryDict

from etgen.html import E

from django.core.validators import (validate_email, ValidationError,
                                    URLValidator)

from django.apps import apps
get_models = apps.get_models

from lino.utils import IncompleteDate
from .exceptions import ChangedAPI

validate_url = URLValidator()


def djangoname(o):
    return o.__module__.split('.')[-2] + '.' + o.__name__


def comma():
    return ', '


def qs2summary(ar, objects, separator=comma, max_items=5, **kw):
    """Render a collection of objects as a single paragraph.

    :param max_items: don't include more than the specified number of items.
Esempio n. 11
0
PEERINGDB = "https://peeringdb.com/asn/"
PEERINGDB_USERNAME = getattr(configuration, "PEERINGDB_USERNAME", "")
PEERINGDB_PASSWORD = getattr(configuration, "PEERINGDB_PASSWORD", "")

# GitHub releases check
RELEASE_CHECK_URL = getattr(
    configuration,
    "RELEASE_CHECK_URL",
    "https://api.github.com/repos/peering-manager/peering-manager/releases",
)
RELEASE_CHECK_TIMEOUT = getattr(configuration, "RELEASE_CHECK_TIMEOUT", 86400)

# Validate repository URL and timeout
if RELEASE_CHECK_URL:
    try:
        URLValidator(RELEASE_CHECK_URL)
    except ValidationError:
        raise ImproperlyConfigured(
            "RELEASE_CHECK_URL must be a valid API URL. Example: https://api.github.com/repos/peering-manager/peering-manager"
        )
if RELEASE_CHECK_TIMEOUT < 3600:
    raise ImproperlyConfigured(
        "RELEASE_CHECK_TIMEOUT must be at least 3600 seconds (1 hour)")

try:
    from peering_manager.ldap_config import *

    LDAP_CONFIGURED = True
except ImportError:
    LDAP_CONFIGURED = False
Esempio n. 12
0
class RSSTask(models.Model):
    name = models.CharField(max_length=32, null=False, verbose_name='任务名称')
    url = models.CharField(max_length=500,
                           null=False,
                           verbose_name='RSS地址',
                           validators=[URLValidator()])
    frequency = models.FloatField(null=False,
                                  default=5,
                                  verbose_name='频率(分钟)',
                                  validators=[MinValueValidator(0)])
    create_time = models.DateTimeField(null=False,
                                       auto_now_add=True,
                                       verbose_name='创建时间')

    notification = models.ManyToManyField(Notification,
                                          blank=False,
                                          verbose_name='通知方式')

    class Meta:
        verbose_name = "RSS监控"
        verbose_name_plural = "RSS监控管理"

    def __str__(self):
        return self.name

    def save(self, *args, **kwargs):
        from task.utils.scheduler import add_job

        # 新建
        if not self.pk:
            super(RSSTask, self).save(*args, **kwargs)
            id = self.pk

            add_job(id, self.frequency, 'rss')
            task_status = TaskStatus(task_name=self.name,
                                     task_id=id,
                                     task_type='rss')
            task_status.save()
        else:
            super(RSSTask, self).save(*args, **kwargs)

            id = self.pk
            task_status = TaskStatus.objects.get(task_id=id, task_type='rss')
            task_status.task_name = self.name
            task_status.last_status = '更新任务成功'
            task_status.last_run = datetime.now()
            task_status.task_status = 0
            task_status.save()

            add_job(id, self.frequency, 'rss')
            logger.info('task_RSS{}更新'.format(id))

    def delete(self, *args, **kwargs):
        from task.utils.scheduler import remove_job

        id = self.pk
        remove_job(id, 'rss')

        TaskStatus.objects.filter(task_id=id, task_type='rss').delete()
        Content.objects.filter(task_id=id, task_type='rss').delete()

        logger.info('task_RSS{}删除'.format(id))

        super(RSSTask, self).delete(*args, **kwargs)
Esempio n. 13
0
class Task(models.Model):
    name = models.CharField(max_length=100, verbose_name='任务名称', null=False)
    url = models.CharField(max_length=500,
                           verbose_name='监控网址',
                           null=False,
                           validators=[URLValidator()])

    selector_choices = (
        (0, 'Xpath'),
        (1, 'Css selector'),
        (2, 'JsonPath'),
    )

    selector_type = models.IntegerField(verbose_name='元素选择器类型',
                                        null=False,
                                        default='Xpath',
                                        choices=selector_choices)
    selector = models.TextField(
        verbose_name='元素选择器',
        blank=False,
        help_text=
        '''一行一个元素选择器,每一行的格式为:选择器名称{选择器内容},例如:title{//*[@id="id3"]/h3/text()}'''
    )
    template = models.TextField(
        verbose_name='消息体模板',
        blank=True,
        help_text='可为空,自定义发送的通知内容格式,按照选择器名称进行替换,具体示例见文档')
    is_chrome_choices = ((0, 'no'), (1, 'yes'))
    is_chrome = models.IntegerField(null=False,
                                    default='no',
                                    verbose_name='是否使用无头浏览器',
                                    choices=is_chrome_choices)
    frequency = models.FloatField(null=False,
                                  default=5,
                                  verbose_name='频率(分钟)',
                                  validators=[MinValueValidator(0)])
    create_time = models.DateTimeField(null=False,
                                       auto_now_add=True,
                                       verbose_name='创建时间')

    notification = models.ManyToManyField(Notification,
                                          blank=False,
                                          verbose_name='通知方式')

    regular_expression = models.CharField(max_length=500,
                                          verbose_name='正则表达式',
                                          blank=True,
                                          help_text='使用正则表达式进一步提取信息,可以留空')
    rule = models.CharField(max_length=500,
                            verbose_name='监控规则',
                            blank=True,
                            help_text='规则写法参考文档,留空则只简单监控内容变化')
    headers = models.TextField(verbose_name='自定义请求头',
                               blank=True,
                               help_text='自定义请求头,如可以设置cookie获取登录后才能查看的页面')

    class Meta:
        verbose_name = "网页监控"
        verbose_name_plural = "网页监控管理"

    def __str__(self):
        return self.name

    def save(self, *args, **kwargs):
        from task.utils.scheduler import add_job

        # 新建
        if not self.pk:
            super(Task, self).save(*args, **kwargs)
            id = self.pk

            add_job(id, self.frequency)

            task_status = TaskStatus(task_name=self.name, task_id=id)
            task_status.save()
        else:
            super(Task, self).save(*args, **kwargs)
            id = self.pk

            task_status = TaskStatus.objects.get(task_id=id, task_type='html')
            task_status.task_name = self.name
            task_status.last_status = '更新任务成功'
            task_status.last_run = datetime.now()
            task_status.task_status = 0
            task_status.save()

            add_job(id, self.frequency)
            logger.info('task_{}更新'.format(id))

    def delete(self, *args, **kwargs):
        from task.utils.scheduler import remove_job

        id = self.pk
        remove_job(id)

        TaskStatus.objects.filter(task_id=id, task_type='html').delete()
        Content.objects.filter(task_id=id, task_type='html').delete()

        logger.info('task_{}删除'.format(id))

        super(Task, self).delete(*args, **kwargs)
Esempio n. 14
0
def issue_details(request):
    """Determine the Github issue keywords of the specified Github issue or PR URL.

    Todo:
        * Modify the view to only use the Github API (remove BeautifulSoup).
        * Simplify the view logic.

    Returns:
        JsonResponse: A JSON response containing the Github issue or PR keywords.

    """
    response = {}

    url = request.GET.get('url')
    url_val = URLValidator()
    try:
        url_val(url)
    except ValidationError as e:
        response['message'] = 'invalid arguments'
        return JsonResponse(response)

    if url.lower()[:19] != 'https://github.com/':
        response['message'] = 'invalid arguments'
        return JsonResponse(response)

    # Web format:  https://github.com/jasonrhaas/slackcloud/issues/1
    # API format:  https://api.github.com/repos/jasonrhaas/slackcloud/issues/1
    gh_api = url.replace('github.com', 'api.github.com/repos')

    try:
        api_response = requests.get(gh_api, auth=_AUTH)
    except ValidationError:
        response['message'] = 'could not pull back remote response'
        return JsonResponse(response)

    if api_response.status_code != 200:
        response[
            'message'] = f'there was a problem reaching the github api, status code {api_response.status_code}'
        response['github_resopnse'] = api_response.json()
        return JsonResponse(response)

    try:
        response = api_response.json()
        body = response['body']
    except (KeyError, ValueError) as e:
        response['message'] = str(e)
    else:
        response['description'] = body.replace('\n', '').strip()
        response['title'] = response['title']

    keywords = []

    url = request.GET.get('url')
    url_val = URLValidator()
    try:
        url_val(url)
    except ValidationError:
        response['message'] = 'invalid arguments'
        return JsonResponse(response)

    if url.lower()[:19] != 'https://github.com/':
        response['message'] = 'invalid arguments'
        return JsonResponse(response)

    try:
        repo_url = None
        if '/pull' in url:
            repo_url = url.split('/pull')[0]
        if '/issue' in url:
            repo_url = url.split('/issue')[0]
        split_repo_url = repo_url.split('/')
        keywords.append(split_repo_url[-1])
        keywords.append(split_repo_url[-2])

        html_response = requests.get(repo_url, auth=_AUTH)
    except (AttributeError, ValidationError):
        response['message'] = 'could not pull back remote response'
        return JsonResponse(response)

    try:
        soup = BeautifulSoup(html_response.text, 'html.parser')

        eles = soup.findAll("span", {"class": "lang"})
        for ele in eles:
            keywords.append(ele.text)

    except ValidationError:
        response['message'] = 'could not parse html'
        return JsonResponse(response)

    try:
        response['keywords'] = keywords
    except Exception as e:
        print(e)
        response['message'] = 'could not find a title'

    return JsonResponse(response)
Esempio n. 15
0
def validate_relations(value):
    """
    Package Lists
    """
    relations = PkgRelation.parse_relations(value)
    for relation in relations:
        for rel in relation:
            if len(rel) == 3:
                raise ValidationError(
                    _("Cannot parse package relationship \"%s\"") %
                    rel.get("name", "untitled"))


bugs_validator = URLValidator(
    schemes=["bts-type", "debbugs"],
    message=_("Enter a valid url of the bug tracking system."),
    code="invalid")


def validate_bugs(value):
    """
    Inherits from a Built-in URLValidator
    """
    return bugs_validator(value)


class Version(models.Model):
    """
    DCRM Base Model: Version
    This model manages all versions generated from .deb files.
    
Esempio n. 16
0
    def handle(self, **options):

        if not settings.MEDIA_ROOT:
            raise ImproperlyConfigured(
                'Please set MEDIA_ROOT in your settings file')

        remote_url = options['remote_url']
        try:
            val = URLValidator()
            val(remote_url)
        except ValidationError:
            raise CommandError('Please enter a valid URL')

        exercise_api = "{0}/api/v2/exercise/?limit=999"
        image_api = "{0}/api/v2/exerciseimage/?exercise={1}"
        thumbnail_api = "{0}/api/v2/exerciseimage/{1}/thumbnails/"

        headers = {
            'User-agent':
            default_user_agent('wger/{} + requests'.format(get_version()))
        }

        # Get all exercises
        result = requests.get(exercise_api.format(remote_url),
                              headers=headers).json()
        for exercise_json in result['results']:
            exercise_name = exercise_json['name'].encode('utf-8')
            exercise_uuid = exercise_json['uuid']
            exercise_id = exercise_json['id']

            self.stdout.write('')
            self.stdout.write(
                u"*** Processing {0} (ID: {1}, UUID: {2})".format(
                    exercise_name, exercise_id, exercise_uuid))

            try:
                exercise = Exercise.objects.get(uuid=exercise_uuid)
            except Exercise.DoesNotExist:
                self.stdout.write(
                    '    Remote exercise not found in local DB, skipping...')
                continue

            # Get all images
            images = requests.get(image_api.format(remote_url, exercise_id),
                                  headers=headers).json()

            if images['count']:

                for image_json in images['results']:
                    image_id = image_json['id']
                    result = requests.get(thumbnail_api.format(
                        remote_url, image_id),
                                          headers=headers).json()

                    image_name = os.path.basename(result['original'])
                    self.stdout.write('    Fetching image {0} - {1}'.format(
                        image_id, image_name))

                    try:
                        image = ExerciseImage.objects.get(pk=image_id)
                        self.stdout.write(
                            '    --> Image already present locally, skipping...'
                        )
                        continue
                    except ExerciseImage.DoesNotExist:
                        self.stdout.write(
                            '    --> Image not found in local DB, creating now...'
                        )
                        image = ExerciseImage()
                        image.pk = image_id

                    # Save the downloaded image, see link for details
                    # http://stackoverflow.com/questions/1308386/programmatically-saving-image-to-
                    retrieved_image = requests.get(result['original'],
                                                   headers=headers)
                    img_temp = NamedTemporaryFile(delete=True)
                    img_temp.write(retrieved_image.content)
                    img_temp.flush()

                    image.exercise = exercise
                    image.is_main = image_json['is_main']
                    image.status = image_json['status']
                    image.image.save(
                        os.path.basename(image_name),
                        File(img_temp),
                    )
                    image.save()

            else:
                self.stdout.write(
                    '    No images for this exercise, nothing to do')
Esempio n. 17
0
class URLField(models.URLField):
    default_validators = [URLValidator(schemes=["http", "https", "telnet", "ssh"])]

    def __init__(self, *args, **kwargs):
        kwargs["max_length"] = 255
        super().__init__(*args, **kwargs)
Esempio n. 18
0
    def clean(self):
        cleaned_data = super(Profile, self).clean()

        name = cleaned_data.get('name')
        handle = cleaned_data.get('handle')
        city = cleaned_data.get('city')
        phone = cleaned_data.get('phone')
        tags = cleaned_data.get('tags')
        twitter = cleaned_data.get('twitter')
        facebook = cleaned_data.get('facebook')
        instagram = cleaned_data.get('instagram')
        google = cleaned_data.get('google')

        if name:
            if not len(name.split()) > 1:
                self.add_error('name', 'Please enter full name.')
            elif len(name.split()) > 2:
                self.add_error('name',
                               'Please enter only first and last names.')
            else:
                regex = re.compile(r'^[a-zA-Z]{2,}[\s][a-zA-Z]{2,}$', re.U)
                if not regex.match(name):
                    self.add_error('name', 'Please enter a valid name.')

        if handle:
            if len(handle) < 5:
                self.add_error('handle',
                               'Your handle should be at least 5 characters.')
            elif len(handle) > 15:
                self.add_error('handle',
                               'Your handle should be at most 15 characters.')
            else:
                regex = re.compile(r'[a-zA-Z0-9]+$', re.U)
                if not regex.match(handle):
                    self.add_error(
                        'handle',
                        'Your handle should only contain letters and numbers.')

        if phone:
            regex = re.compile(r'^\+?1?\d{9,15}$')
            if not regex.match(phone):
                self.add_error('phone', 'Please enter a valid phone number.')

        if tags:
            pass

        if city:
            try:
                c = City.objects.get(pk=city)
            except Exception as e:
                self.add_error('city', 'City not found')

        if twitter:
            try:
                validate = URLValidator(schemes=('http', 'https'))
                validate(twitter)
            except Exception as e:
                self.add_error('twitter', "Invalid URL")

        if facebook:
            try:
                validate = URLValidator(schemes=('http', 'https'))
                validate(facebook)
            except Exception as e:
                self.add_error('facebook', 'Invalid URL')

        if instagram:
            try:
                validate = URLValidator(schemes=('http', 'https'))
                validate(instagram)
            except Exception as e:
                self.add_error('instagram', 'Invalid URL')

        if google:
            try:
                validate = URLValidator(schemes=('http', 'https'))
                validate(google)
            except Exception as e:
                self.add_error('google', 'Invalid URL')
Esempio n. 19
0
 def _is_url(path):
     try:
         URLValidator()(path)
         return True
     except ValidationError:
         return False
Esempio n. 20
0
"""This file implements the tables storing information about suppliers
"""
from typing import List, Optional

from django.db import models
from django.contrib.auth.models import User

from django.core.validators import URLValidator, EmailValidator
from django.core.exceptions import ValidationError
from django.template.defaultfilters import truncatechars
from django.utils import timezone

from django.utils.translation import gettext_lazy as _


URL_VALIDATOR = URLValidator()
EMAIL_VALIDATOR = EmailValidator()


def is_email_or_url(string: str) -> bool:
    """Checks if string is either an email or url.
    """
    out = False
    try:
        URL_VALIDATOR(string)
        out = True
    except ValidationError:
        pass

    try:
        URL_VALIDATOR(string)
Esempio n. 21
0
class Podcast(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    slug = models.SlugField(unique=True)

    name = models.CharField(max_length=256)
    subtitle = models.CharField(max_length=512, default='', blank=True)

    created = models.DateTimeField(auto_now_add=True, editable=False)
    cover_image = models.URLField(max_length=500)
    description = models.TextField(blank=True)
    is_explicit = models.BooleanField(default=False)
    homepage = models.URLField(max_length=500,
                               blank=True,
                               validators=[URLValidator()])

    language = models.CharField(max_length=16, default='en-US')
    copyright = models.CharField(max_length=1024, blank=True)
    author_name = models.CharField(max_length=1024, default='Anonymous')

    owner = models.ForeignKey(User)

    rss_redirect = models.URLField(null=True, blank=True, max_length=500)
    stats_base_listens = models.PositiveIntegerField(default=0)

    networks = models.ManyToManyField(Network, blank=True)

    total_tips = models.PositiveIntegerField(
        default=0,
        help_text=ugettext_lazy(
            'Tips collected over podcast lifetime in cents'))

    @cached_method
    def get_site(self):
        try:
            return self.site
        except Exception:
            return None

    @cached_method
    def get_category_list(self):
        return ','.join(x.category for x in self.podcastcategory_set.all())

    def set_category_list(self, cat_str):
        existing = set(x.category for x in self.podcastcategory_set.all())
        new = set(cat_str.split(','))

        added = new - existing
        removed = existing - new

        for a in added:
            n = PodcastCategory(podcast=self, category=a)
            n.save()

        for r in removed:
            o = PodcastCategory.objects.get(podcast=self, category=r)
            o.delete()

    @cached_method
    def is_still_importing(self):
        return bool(
            self.assetimportrequest_set.filter(failed=False,
                                               resolved=False).count())

    @cached_method
    def get_all_episodes_raw(self):
        return PodcastEpisode.objects.filter(podcast=self)

    @cached_method
    def get_episodes(self):
        episodes = self.get_all_episodes_raw().filter(
            publish__lt=round_now(),
            awaiting_import=False).order_by('-publish')
        us = UserSettings.get_from_user(self.owner)
        if us.plan == payment_plans.PLAN_DEMO:
            episodes = episodes[:10]
        return episodes

    @cached_method
    def get_unpublished_count(self):
        return self.podcastepisode_set.filter(publish__gt=round_now()).count()

    @cached_method
    def get_most_recent_episode(self):
        episodes = list(self.get_episodes())
        return None if not episodes else episodes[0]

    def get_most_recent_publish_date(self):
        latest = self.get_most_recent_episode()
        return latest.publish if latest else None

    def get_available_flair_flags(self, flatten=False):
        us = UserSettings.get_from_user(self.owner)
        plan = us.plan
        flags = []
        if payment_plans.minimum(plan, payment_plans.PLAN_STARTER):
            # This is inside a conditional because it's forced on for free
            # users.
            flags.append(FLAIR_POWERED_BY)
        if payment_plans.minimum(plan, payment_plans.FEATURE_MIN_COMMENT_BOX):
            flags.append(FLAIR_FEEDBACK)
        if us.stripe_payout_managed_account:
            flags.append(FLAIR_TIP_JAR)

        if payment_plans.minimum(
                plan, payment_plans.FEATURE_MIN_SITES) and self.get_site():
            flags.append(FLAIR_SITE_LINK)

        if flatten:
            return flags
        else:
            return [(f, FLAIR_FLAGS_MAP[f]) for f in flags]

    def __str__(self):
        return self.name

    def last_eligible_payout_date(self):
        today = datetime.date.today()
        days_since_friday = (today.isoweekday() - 5) % 7
        return today - datetime.timedelta(days=days_since_friday)

    def last_payout_date(self):
        last_eligible_payout_date = self.last_eligible_payout_date()

        last_day_for_charges_in_last_payout = (last_eligible_payout_date -
                                               datetime.timedelta(days=7))

        last_tip = (self.tip_events.filter(
            occurred_at__lte=last_day_for_charges_in_last_payout).order_by(
                '-occurred_at').first())

        if not last_tip:
            return None

        return last_tip.payout_date()

    def next_payout_date(self):
        last_eligible_payout_date = self.last_eligible_payout_date()
        last_tip = (self.tip_events.filter(
            occurred_at__gt=last_eligible_payout_date -
            datetime.timedelta(days=7)).order_by('-occurred_at').first())

        if not last_tip:
            return None

        return last_tip.payout_date()

    def average_tip_value_this_month(self):
        events = (self.tip_events.filter(occurred_at__gt=round_now() -
                                         datetime.timedelta(days=30)))
        return events.aggregate(models.aggregates.Avg('amount'))['amount__avg']

    def tip_fees_paid(self):
        return (self.tip_events.all().aggregate(
            models.aggregates.Sum('fee_amount')))['fee_amount__sum'] or 0

    @cached_method
    def get_remaining_surge(self, max_size):
        uset = UserSettings.get_from_user(self.owner)
        if not payment_plans.minimum(uset.plan, payment_plans.PLAN_STARTER):
            return 0
        thirty_ago = datetime.datetime.now() - datetime.timedelta(days=30)
        last_thirty_eps = self.podcastepisode_set.filter(
            created__gt=thirty_ago, audio_size__gt=max_size)
        surge_count = last_thirty_eps.count()
        surge_amt = last_thirty_eps.aggregate(
            models.Sum('audio_size'))['audio_size__sum'] or 0
        surge_amt -= surge_count * max_size

        remaining = max_size - surge_amt
        return 0 if remaining < 0 else remaining
import os

try:
    from urllib.request import urlretrieve
except ImportError:
    from urllib import urlretrieve

from django.core.management import BaseCommand
from django.conf import settings
from django.core.validators import URLValidator

from static_import.base import (get_libs, is_css, is_js, is_img)

url_validator = URLValidator()
base_dir = settings.BASE_DIR


class Command(BaseCommand):

    static = 'Url|Static File Name'
    help = 'download static files'

    @staticmethod
    def guess_path(name):
        if is_css(name):
            return os.path.join('css', name)
        elif is_js(name):
            return os.path.join('js', name)
        elif is_img(name):
            return os.path.join('img', name)
Esempio n. 23
0
    def validate(self, data):
        user = self.context['request'].user
        errors = {}

        # since 'file' is not a field on the model, we have to access it through request.data rather than data
        uploaded_file = self.context['request'].data.get('file')

        # handle is_private and private_reason:
        if self.instance:
            if not user.is_staff:
                # only staff can manually change private_reason in all cases
                data.pop('private_reason', None)

                # if updating privacy, make sure user is allowed to change private status
                if 'is_private' in data and self.instance.is_private != bool(
                        data['is_private']):
                    if self.instance.private_reason and self.instance.private_reason not in [
                            'user', 'old_policy'
                    ]:
                        errors['is_private'] = 'Cannot change link privacy.'
                    else:
                        data['private_reason'] = 'user' if data[
                            'is_private'] else None
        else:
            # for new links, set private_reason based on is_private
            data['private_reason'] = 'user' if data.get('is_private') else None

        # check submitted URL for new link
        if not self.instance:
            if not data.get('submitted_url'):
                errors['url'] = "URL cannot be empty."
            else:
                try:
                    validate = URLValidator()
                    temp_link = Link(submitted_url=data['submitted_url'])
                    validate(temp_link.ascii_safe_url)

                    # Don't force URL resolution validation if a file is provided
                    if not uploaded_file:
                        if not temp_link.ip:
                            errors['url'] = "Couldn't resolve domain."
                        elif not ip_in_allowed_ip_range(temp_link.ip):
                            errors['url'] = "Not a valid IP."
                        elif not temp_link.headers:
                            errors['url'] = "Couldn't load URL."
                        else:
                            # preemptively reject URLs that report a size over settings.MAX_ARCHIVE_FILE_SIZE
                            try:
                                if int(
                                        temp_link.headers.get(
                                            'content-length', 0)
                                ) > settings.MAX_ARCHIVE_FILE_SIZE:
                                    errors[
                                        'url'] = "Target page is too large (max size %sMB)." % (
                                            settings.MAX_ARCHIVE_FILE_SIZE /
                                            1024 / 1024)
                            except ValueError:
                                # content-length header wasn't an integer. Carry on.
                                pass
                except DjangoValidationError:
                    errors['url'] = "Not a valid URL."
                except TooManyRedirects:
                    errors['url'] = "URL caused a redirect loop."

        # check uploaded file
        if uploaded_file == '':
            errors['file'] = "File cannot be blank."
        elif uploaded_file:

            if self.instance and self.instance.is_archive_eligible():
                errors[
                    'file'] = "Archive contents cannot be replaced after 24 hours"

            else:
                mime_type = get_mime_type(uploaded_file.name)

                # Get mime type string from tuple
                if not mime_type or not mime_type_lookup[mime_type][
                        'valid_file'](uploaded_file):
                    errors['file'] = "Invalid file."
                elif uploaded_file.size > settings.MAX_ARCHIVE_FILE_SIZE:
                    errors['file'] = "File is too large."

        if errors:
            raise serializers.ValidationError(errors)

        return data
Esempio n. 24
0
    from django.urls import reverse
except ImportError:
    from django.core.urlresolvers import reverse
from django.utils.datastructures import MultiValueDictKeyError
from django.shortcuts import render, redirect
from django.http import HttpResponseBadRequest, HttpResponseRedirect
from django.views.decorators.csrf import csrf_exempt

from . import (saml2idp_metadata, exceptions, metadata, registry, xml_signing)

logger = logging.getLogger(__name__)

# The 'schemes' argument for the URLValidator was introduced in Django 1.6. This
# ensure that URL validation works in 1.5 as well.
try:
    URL_VALIDATOR = URLValidator(schemes=('http', 'https'))
except TypeError:
    URL_VALIDATOR = URLValidator()

BASE_TEMPLATE_DIR = 'saml2idp'


def _get_template_names(filename, processor=None):
    """
    Create a list of template names to use based on the processor name. This
    makes it possible to have processor-specific templates.
    """
    specific_templates = []
    if processor and processor.name:
        specific_templates = [
            os.path.join(BASE_TEMPLATE_DIR, processor.name, filename)
Esempio n. 25
0
    def post(self, request, *args, **kwargs):
        if not request.user.is_authenticated:
            return JsonResponse({
                'Status': False,
                'Error': 'Log in required'
            },
                                status=403)

        if request.user.type != 'shop':
            return JsonResponse(
                {
                    'Status': False,
                    'Error': 'Только для магазинов'
                }, status=403)

        url = request.data.get('url')
        if url:
            validate_url = URLValidator()
            try:
                validate_url(url)
            except ValidationError as e:
                return JsonResponse({'Status': False, 'Error': str(e)})
            else:
                stream = get(url).content

                data = load_yaml(stream, Loader=Loader)

                shop, _ = Shop.objects.get_or_create(name=data['shop'],
                                                     user_id=request.user.id)
                for category in data['categories']:
                    category_object, _ = Category.objects.get_or_create(
                        id=category['id'], name=category['name'])
                    category_object.shops.add(shop.id)
                    category_object.save()
                ProductInfo.objects.filter(shop_id=shop.id).delete()
                for item in data['goods']:
                    product, _ = Product.objects.get_or_create(
                        name=item['name'], category_id=item['category'])

                    product_info = ProductInfo.objects.create(
                        product_id=product.id,
                        external_id=item['id'],
                        model=item['model'],
                        price=item['price'],
                        price_rrc=item['price_rrc'],
                        quantity=item['quantity'],
                        shop_id=shop.id)
                    for name, value in item['parameters'].items():
                        parameter_object, _ = Parameter.objects.get_or_create(
                            name=name)
                        ProductParameter.objects.create(
                            product_info_id=product_info.id,
                            parameter_id=parameter_object.id,
                            value=value)

                return JsonResponse({'Status': True})

        return JsonResponse({
            'Status': False,
            'Errors': 'Не указаны все необходимые аргументы'
        })
Esempio n. 26
0
 def validate(self, value):
     # Use parents' handling of required fields, etc.
     super(MultiURLField, self).validate(value)
     for url in value:
         URLValidator(url)
Esempio n. 27
0
 (MaxValueValidator(0), 1, ValidationError),
 (MaxValueValidator(NOW), NOW + timedelta(days=1), ValidationError),
 (MinValueValidator(-10), -10, None),
 (MinValueValidator(-10), 10, None),
 (MinValueValidator(-10), 0, None),
 (MinValueValidator(NOW), NOW, None),
 (MinValueValidator(NOW), NOW + timedelta(days=1), None),
 (MinValueValidator(0), -1, ValidationError),
 (MinValueValidator(NOW), NOW - timedelta(days=1), ValidationError),
 (MaxLengthValidator(10), '', None),
 (MaxLengthValidator(10), 10 * 'x', None),
 (MaxLengthValidator(10), 15 * 'x', ValidationError),
 (MinLengthValidator(10), 15 * 'x', None),
 (MinLengthValidator(10), 10 * 'x', None),
 (MinLengthValidator(10), '', ValidationError),
 (URLValidator(), 'http://www.djangoproject.com/', None),
 (URLValidator(), 'HTTP://WWW.DJANGOPROJECT.COM/', None),
 (URLValidator(), 'http://localhost/', None),
 (URLValidator(), 'http://example.com/', None),
 (URLValidator(), 'http://www.example.com/', None),
 (URLValidator(), 'http://www.example.com:8000/test', None),
 (URLValidator(), 'http://valid-with-hyphens.com/', None),
 (URLValidator(), 'http://subdomain.example.com/', None),
 (URLValidator(), 'http://200.8.9.10/', None),
 (URLValidator(), 'http://200.8.9.10:8000/test', None),
 (URLValidator(), 'http://valid-----hyphens.com/', None),
 (URLValidator(), 'http://example.com?something=value', None),
 (URLValidator(),
  'http://example.com/index.php?something=value&another=value2', None),
 (URLValidator(), 'https://example.com/', None),
 (URLValidator(), 'ftp://example.com/', None),
Esempio n. 28
0
 def get_success_url(self):
     validate = URLValidator()
     refer = self.request.META.get('HTTP_REFERER', '/')
     validate(refer)
     return refer
Esempio n. 29
0
    def handle_async(self, *args, **options):
        self.stderr.write(
            "`fullfacilitysync` command is deprecated and will be removed in 0.13.0 in favor of `sync`, which accepts the same options."
            " Use `sync` command instead.")

        # validate url that is passed in
        try:
            URLValidator()((options["base_url"]))
        except ValidationError:
            raise CommandError(
                "Base URL is not valid. Please retry command and enter a valid URL."
            )

        # call this in case user directly syncs without migrating database
        if not ScopeDefinition.objects.filter():
            call_command("loaddata", "scopedefinitions")

        controller = MorangoProfileController("facilitydata")
        with self.start_progress(total=7) as progress_update:
            try:
                network_connection = controller.create_network_connection(
                    options["base_url"])
            except ConnectionError:
                raise CommandError(
                    "Can not connect to server with base URL: {}".format(
                        options["base_url"]))

            # if instance_ids are equal, this means device is trying to sync with itself, which we don't allow
            if (InstanceIDModel.get_or_create_current_instance()[0].id ==
                    network_connection.server_info["instance_id"]):
                raise CommandError(
                    "Device can not sync with itself. Please recheck base URL and try again."
                )

            progress_update(1)

            options["dataset_id"] = self.get_dataset_id(
                options["base_url"], options["dataset_id"])
            progress_update(1)

            (
                client_cert,
                server_cert,
                options["username"],
            ) = self.get_client_and_server_certs(
                options["username"],
                options["password"],
                options["dataset_id"],
                network_connection,
            )
            progress_update(1)

            sync_client = network_connection.create_sync_session(
                client_cert, server_cert, chunk_size=options["chunk_size"])
            progress_update(1)

            # pull from server and push our own data to server
            if not options["no_pull"]:
                sync_client.initiate_pull(Filter(options["dataset_id"]))
            if not options["no_push"]:
                sync_client.initiate_push(Filter(options["dataset_id"]))
            progress_update(1)

            self.create_superuser_and_provision_device(options["username"],
                                                       options["dataset_id"])
            progress_update(1)

            sync_client.close_sync_session()
            progress_update(1)
Esempio n. 30
0
 def validate_url(self, url):
     if url:
         URLValidator()(url)