Esempio n. 1
0
    def post(self, request, *args, **kwargs):
        try:
            url = request.POST["url"]
            url = validate_domain(url)

            # Get or create the service matching this domain
            service, created = Service.get_or_create(url=url, name=url)
            service.save()

            # Select a domain to use for the identity
            # Create a list of possible domains
            domains = [cred["DOMAIN"] for cred in settings.MAILCREDENTIALS]
            # Shuffle it
            shuffle(domains)
            # Iterate through it
            for identityDomain in domains:
                # If the domain has not yet been used, stop the loop, otherwise try the next
                if (Identity.objects.filter(service_id=service.pk).filter(
                        mail__contains=identityDomain).count() == 0):
                    break
            # At this point, we have either selected a domain that has not yet been used for the
            # provided service, or the service already has at least one identity for each domain,
            # in which case we have picked one domain at random (by shuffling the list first).

            # Create an identity and save it
            ident = Identity.create(service, identityDomain)
            ident.save()

            if created:
                create_service_cache(service, force=True)

            # Return the created identity
            r = JsonResponse({
                "site": url,
                "email": ident.mail,
                "first": ident.first_name,
                "last": ident.surname,
                "gender": "Male" if ident.gender else "Female",
            })
        except KeyError:
            logger.warning(
                "BookmarkletApiView.post: Malformed request received, missing url.",
                extra={"request": request},
            )
            r = JsonResponse({"error": "No URL passed"})
        except AssertionError:
            # Invalid URL passed
            logger.warning(
                "BookmarkletApiView.post: Malformed request received, malformed URL.",
                extra={"request": request},
            )
            r = JsonResponse({"error": "Invalid URL passed."})
        r["Access-Control-Allow-Origin"] = "*"
        return r
Esempio n. 2
0
    def setUp(self):
        """
        create a service and a identity
        """
        service = Service.create('nbcnews.com', 'NBCNews')
        identity = Identity.create(service, 'newsletterme.de')
        identity.surname = "cartier"
        identity.first_name = "cathy"
        identity.mail = '*****@*****.**'
        identity.save()

        Scanword.objects.create(type="word", word="Subscribe")
Esempio n. 3
0
    def post(self, request, *args, **kwargs):
        try:
            domain = request.POST['domain']
            # Format domain. Will also ensure that the domain is valid, and return None on invalid domains
            domain = validate_domain(domain)
        except KeyError:
            # Someone is messing with us. Log this.
            logger.warning(
                'IdentityView.post: Malformed POST request received',
                extra={'request': request})
            # Send them back to the homepage with a slap on the wrist
            # TODO: Add code to display a warning on homepage
            return redirect('Home')
        # Check if people are messing with us
        except AssertionError:
            # Someone may be messing with us. Save it, just in case.
            logger.info("IdentityView.post: Invalid URL passed",
                        extra={
                            'request': request,
                            'domain': domain
                        })
            # Send them back to the homepage with a slap on the wrist
            # TODO: Add code to display a warning on homepage
            return redirect('Home')

        # Get or create service
        service, created = Service.get_or_create(url=domain, name=domain)
        service.save()

        # Select a domain to use for the identity
        # Create a list of possible domains
        domains = [cred["DOMAIN"] for cred in settings.MAILCREDENTIALS]
        # Shuffle it
        shuffle(domains)
        # Iterate through it
        for identityDomain in domains:
            # If the domain has not yet been used, stop the loop, otherwise try the next
            if Identity.objects.filter(service_id=service.pk).filter(
                    mail__contains=identityDomain).count() == 0:
                break
        # At this point, we have either selected a domain that has not yet been used for the
        # provided service, or the service already has at least one identity for each domain,
        # in which case we have picked one domain at random (by shuffling the list first).

        # Create an identity and save it
        ident = Identity.create(service, identityDomain)
        ident.save()

        if created:
            create_service_cache(service, force=True)

        # Display the result to the user
        return render(request, 'identity/identity.html', {'ident': ident})
Esempio n. 4
0
    def post(self, request, *args, **kwargs):
        try:
            # Get service from database
            body_unicode = request.body.decode("utf-8")
            body = json.loads(body_unicode)
            domain = body["domain"]
        except KeyError:
            # No service kwarg is set, warn
            logger.warn(
                "ServiceMetaView.post: Malformed POST request received",
                extra={"request": request},
            )
            # TODO: Add code to display a warning on homepage
            return JsonResponse(convertForJsonResponse({"success": False}))
        try:
            # Format domain. Will also ensure that the domain is valid, and return None on invalid domains
            domain = validate_domain(domain)
        except KeyError:
            # Someone is messing with us. Log this.
            logger.warning(
                "IdentityView.post: Malformed POST request received",
                extra={"request": request},
            )
            # Send them back to the homepage with a slap on the wrist
            # TODO: Add code to display a warning on homepage
            return JsonResponse(convertForJsonResponse({"success": False}))
        # Check if people are messing with us
        except AssertionError:
            # Someone may be messing with us. Save it, just in case.
            logger.info(
                "IdentityView.post: Invalid URL passed",
                extra={"request": request, "domain": domain},
            )
            # Send them back to the homepage with a slap on the wrist
            # TODO: Add code to display a warning on homepage
            return JsonResponse(convertForJsonResponse({"success": False}))

        # Get or create service
        service, created = Service.get_or_create(url=domain, name=domain)
        service.save()

        # Select a domain to use for the identity
        # Create a list of possible domains
        domains = [cred["DOMAIN"] for cred in settings.MAILCREDENTIALS]
        # Shuffle it
        shuffle(domains)
        # Iterate through it
        for identityDomain in domains:
            # If the domain has not yet been used, stop the loop, otherwise try the next
            if (
                Identity.objects.filter(service_id=service.pk)
                .filter(mail__contains=identityDomain)
                .count()
                == 0
            ):
                break
        # At this point, we have either selected a domain that has not yet been used for the
        # provided service, or the service already has at least one identity for each domain,
        # in which case we have picked one domain at random (by shuffling the list first).

        # Create an identity and save it
        ident = Identity.create(service, identityDomain)
        ident.save()

        if created:
            create_service_cache(service, force=True)

        # Display the result to the user
        return JsonResponse(convertForJsonResponse(ident))
Esempio n. 5
0
 def inline_create_identity(self, request, obj, parent_obj=None):
     from identity.models import Identity
     i = Identity()
     i.save()
     i.identity.add(obj)
     return HttpResponseRedirect(i.get_admin_change_url())