Example #1
0
    def create(self, validated_data):
        # First, pop out 'email' since it's not in the Response.
        validated_data.pop('email', None)

        # Strip string data
        for key in ['url', 'description', 'category', 'product',
                    'channel', 'version', 'platform', 'locale',
                    'manufacturer', 'device', 'country', 'source',
                    'campaign', 'user_agent']:
            if key in validated_data:
                validated_data[key] = validated_data[key].strip()

        # If there's a user agent, infer all the things from
        # the user agent and truncate it.
        user_agent = validated_data.get('user_agent')
        if user_agent:
            browser = parse_ua(user_agent)
            validated_data['browser'] = browser.browser[:30]
            validated_data['browser_version'] = browser.browser_version[:30]
            bp = browser.platform
            if bp == 'Windows':
                bp += (' ' + browser.platform_version)
            validated_data['browser_platform'] = bp[:30]
            validated_data['user_agent'] = user_agent[:USER_AGENT_LENGTH]

        return Response.objects.create(api=1, **validated_data)
Example #2
0
    def create(self, validated_data):
        # First, pop out 'email' since it's not in the Response.
        validated_data.pop('email', None)

        # Strip string data
        for key in [
                'url', 'description', 'category', 'product', 'channel',
                'version', 'platform', 'locale', 'manufacturer', 'device',
                'country', 'source', 'campaign', 'user_agent'
        ]:
            if key in validated_data:
                validated_data[key] = validated_data[key].strip()

        # If there's a user agent, infer all the things from
        # the user agent.
        user_agent = validated_data.get('user_agent')
        if user_agent:
            browser = parse_ua(user_agent)
            validated_data['browser'] = browser.browser
            validated_data['browser_version'] = browser.browser_version
            bp = browser.platform
            if browser.platform == 'Windows':
                bp += (' ' + browser.platform_version)
            validated_data['browser_platform'] = bp

        return Response.objects.create(api=1, **validated_data)
Example #3
0
    def restore_object(self, attrs, instance=None):
        # Note: instance should never be anything except None here
        # since we only accept POST and not PUT/PATCH.

        opinion = Response(
            happy=attrs['happy'],
            url=attrs['url'].strip(),
            description=attrs['description'].strip(),
            category=attrs['category'].strip(),
            product=attrs['product'].strip(),
            channel=attrs['channel'].strip(),
            version=attrs['version'].strip(),
            platform=attrs['platform'].strip(),
            locale=attrs['locale'].strip(),
            manufacturer=attrs['manufacturer'].strip(),
            device=attrs['device'].strip(),
            country=attrs['country'].strip(),
            source=attrs['source'].strip(),
            campaign=attrs['campaign'].strip(),
            api=1,  # Hard-coded api version number
        )

        # If there's a user agent, infer all the things from the user
        # agent.
        user_agent = attrs['user_agent'].strip()
        if user_agent:
            opinion.user_agent = user_agent
            browser = parse_ua(user_agent)
            opinion.browser = browser.browser
            opinion.browser_version = browser.browser_version
            opinion.browser_platform = browser.platform
            if browser.platform == 'Windows':
                opinion.browser_platform += (' ' + browser.platform_version)

        # If there is an email address, stash it on this instance so
        # we can save it later in .save() and so it gets returned
        # correctly in the response. This doesn't otherwise affect the
        # Response model instance.
        opinion.email = attrs.get('email', '').strip()

        slop = {}
        data_items = sorted(self.context['request'].DATA.items())
        for key, val in data_items:
            if key in attrs:
                continue
            # Restrict key to 20 characters
            key = key[:20]

            # Restrict value to 100 characters
            val = val[:100]
            slop[key] = val

            # Only collect 20 pairs max
            if len(slop) >= 20:
                break

        opinion.slop = slop

        return opinion
Example #4
0
    def restore_object(self, attrs, instance=None):
        # Note: instance should never be anything except None here
        # since we only accept POST and not PUT/PATCH.

        opinion = Response(
            happy=attrs['happy'],
            url=attrs['url'].strip(),
            description=attrs['description'].strip(),
            category=attrs['category'].strip(),
            product=attrs['product'].strip(),
            channel=attrs['channel'].strip(),
            version=attrs['version'].strip(),
            platform=attrs['platform'].strip(),
            locale=attrs['locale'].strip(),
            manufacturer=attrs['manufacturer'].strip(),
            device=attrs['device'].strip(),
            country=attrs['country'].strip(),
            source=attrs['source'].strip(),
            campaign=attrs['campaign'].strip(),
            api=1,  # Hard-coded api version number
        )

        # If there's a user agent, infer all the things from the user
        # agent.
        user_agent = attrs['user_agent'].strip()
        if user_agent:
            opinion.user_agent = user_agent
            browser = parse_ua(user_agent)
            opinion.browser = browser.browser
            opinion.browser_version = browser.browser_version
            opinion.browser_platform = browser.platform
            if browser.platform == 'Windows':
                opinion.browser_platform += (' ' + browser.platform_version)

        # If there is an email address, stash it on this instance so
        # we can save it later in .save() and so it gets returned
        # correctly in the response. This doesn't otherwise affect the
        # Response model instance.
        opinion.email = attrs.get('email', '').strip()

        slop = {}
        data_items = sorted(self.context['request'].DATA.items())
        for key, val in data_items:
            if key in attrs:
                continue
            # Restrict key to 20 characters
            key = key[:20]

            # Restrict value to 100 characters
            val = val[:100]
            slop[key] = val

            # Only collect 20 pairs max
            if len(slop) >= 20:
                break

        opinion.slop = slop

        return opinion
Example #5
0
    def check_ua(self, case):
        """Check and individual user agent test case."""
        parsed = browsers.parse_ua(case['user_agent'])
        assert(parsed is not None)

        expected = tuple(case[key] for key in
                         ['browser', 'browser_version',
                          'platform', 'platform_version',
                          'mobile'])

        eq_(parsed, expected)
Example #6
0
class ResponseFactory(factory.DjangoModelFactory):
    class Meta:
        model = Response

    happy = True
    url = u''
    description = u'So awesome!'

    user_agent = USER_AGENT
    browser = factory.LazyAttribute(
        lambda a: unicode(browsers.parse_ua(a.user_agent).browser))
    browser_version = factory.LazyAttribute(
        lambda a: unicode(browsers.parse_ua(a.user_agent).browser_version))
    platform = factory.LazyAttribute(
        lambda a: unicode(browsers.parse_ua(a.user_agent).platform))

    product = factory.LazyAttribute(lambda a: unicode(
        Response.infer_product(browsers.parse_ua(a.user_agent))))
    channel = u'stable'
    version = factory.LazyAttribute(
        lambda a: unicode(browsers.parse_ua(a.user_agent).browser_version))
    locale = u'en-US'
    api = None
Example #7
0
    def check_ua(self, case):
        """Check and individual user agent test case."""
        parsed = browsers.parse_ua(case['user_agent'])
        assert(parsed is not None)

        expected = tuple(case[key] for key in
                         ['browser', 'browser_version',
                          'platform', 'platform_version',
                          'mobile'])

        msg = ('UA Detection failed. UA="{0}", parsed="{1}" '
               'expected="{2}"'.format(
                   case['user_agent'], parsed, expected))
        assert parsed == expected, msg
Example #8
0
def create_simple(happy, description, url=u'', ua=FIREFOX_LINUX_17,
                  locale=u'en-US'):
    parsed = browsers.parse_ua(ua)
    data = {
        'happy': happy,
        'url': url,
        'description': description,

        'user_agent': ua,
        'browser': parsed.browser,
        'browser_version': parsed.browser_version,
        'platform': parsed.platform,
        'locale': locale,
    }
    obj = simple(**data)
    obj.save()
    return obj
Example #9
0
def response(**kwargs):
    """Model maker for feedback.models.Response."""
    ua = kwargs.pop('ua', USER_AGENT)
    parsed = browsers.parse_ua(ua)
    defaults = {
        'prodchan': 'firefox.desktop.stable',
        'happy': True,
        'url': u'',
        'description': u'So awesome!',
        'user_agent': ua,
        'browser': parsed.browser,
        'browser_version': parsed.browser_version,
        'platform': parsed.platform,
        'locale': u'en-US',
    }

    defaults.update(kwargs)
    return Response(**defaults)
Example #10
0
def create_simple(happy,
                  description,
                  url=u'',
                  ua=FIREFOX_LINUX_17,
                  locale=u'en-US'):
    parsed = browsers.parse_ua(ua)
    data = {
        'happy': happy,
        'url': url,
        'description': description,
        'user_agent': ua,
        'browser': parsed.browser,
        'browser_version': parsed.browser_version,
        'platform': parsed.platform,
        'locale': locale,
    }
    obj = simple(**data)
    obj.save()
    return obj
Example #11
0
def response(**kwargs):
    """Model maker for feedback.models.Response."""
    ua = kwargs.pop('ua', USER_AGENT)
    parsed = browsers.parse_ua(ua)
    defaults = {
        'prodchan': 'firefox.desktop.stable',

        'happy': True,
        'url': u'',
        'description': u'So awesome!',

        'user_agent': ua,
        'browser': parsed.browser,
        'browser_version': parsed.browser_version,
        'platform': parsed.platform,
        'locale': u'en-US',
    }

    defaults.update(kwargs)
    return Response(**defaults)
Example #12
0
    def create(self, validated_data):
        # First, pop out 'email' since it's not in the Response.
        validated_data.pop("email", None)

        # Strip string data
        for key in [
            "url",
            "description",
            "category",
            "product",
            "channel",
            "version",
            "platform",
            "locale",
            "manufacturer",
            "device",
            "country",
            "source",
            "campaign",
            "user_agent",
        ]:
            if key in validated_data:
                validated_data[key] = validated_data[key].strip()

        # If there's a user agent, infer all the things from
        # the user agent.
        user_agent = validated_data.get("user_agent")
        if user_agent:
            browser = parse_ua(user_agent)
            validated_data["browser"] = browser.browser
            validated_data["browser_version"] = browser.browser_version
            bp = browser.platform
            if browser.platform == "Windows":
                bp += " " + browser.platform_version
            validated_data["browser_platform"] = bp

        return Response.objects.create(api=1, **validated_data)
Example #13
0
 def sub_prodchan(self, ua, prodchan):
     # _get_prodchan checks request.BROWSER to decide what to do, so
     # give it a mocked object that has that.
     fake_req = NonCallableMock(BROWSER=parse_ua(ua))
     eq_(prodchan, _get_prodchan(fake_req))
Example #14
0
 def test_error_handling(self):
     """Make sure that good, bad, and ugly UAs don't break the parser."""
     path = os.path.join(os.path.dirname(__file__), 'bad_user_agents.txt')
     with open(path) as f:
         for ua in f:
             browsers.parse_ua(ua)
Example #15
0
 def sub_prodchan(self, ua, prodchan):
     # _get_prodchan checks request.BROWSER to decide what to do, so
     # give it a mocked object that has that.
     fake_req = NonCallableMock(BROWSER=parse_ua(ua))
     eq_(prodchan, _get_prodchan(fake_req))
Example #16
0
 def process_request(self, request):
     ua = request.META.get('HTTP_USER_AGENT', '')
     request.BROWSER = parse_ua(ua)
Example #17
0
 def test_error_handling(self):
     """Make sure that good, bad, and ugly UAs don't break the parser."""
     path = os.path.join(os.path.dirname(__file__), 'bad_useragents.txt')
     with open(path) as f:
         for ua in f:
             browsers.parse_ua(ua)
Example #18
0
 def process_request(self, request):
     ua = request.META.get('HTTP_USER_AGENT', '')
     request.BROWSER = parse_ua(ua)