Exemplo n.º 1
0
def preview_snippet(request):
    """
    Build a snippet using info from the POST parameters, and preview that
    snippet on a mock about:home page.
    """
    try:
        template_id = int(request.POST.get('template_id', None))
    except (TypeError, ValueError):
        return HttpResponseBadRequest()

    template = get_object_or_none(SnippetTemplate, id=template_id)
    data = request.POST.get('data', None)

    # Validate that data is JSON.
    try:
        json.loads(data)
    except (TypeError, ValueError):
        data = None

    # If your parameters are wrong, I have no sympathy for you.
    if data is None or template is None:
        return HttpResponseBadRequest()

    # Build a snippet that isn't saved so we can render it.
    snippet = Snippet(template=template, data=data)
    return render(request, 'base/preview.html', {
        'snippet': snippet,
        'client': PREVIEW_CLIENT,
        'preview': True
    })
Exemplo n.º 2
0
def preview_snippet(request):
    """
    Build a snippet using info from the POST parameters, and preview that
    snippet on a mock about:home page.
    """
    try:
        template_id = int(request.POST.get('template_id', None))
    except (TypeError, ValueError):
        return HttpResponseBadRequest()

    template = get_object_or_none(SnippetTemplate, id=template_id)
    data = request.POST.get('data', None)

    # Validate that data is JSON.
    try:
        json.loads(data)
    except (TypeError, ValueError):
        data = None

    # If your parameters are wrong, I have no sympathy for you.
    if data is None or template is None:
        return HttpResponseBadRequest()

    # Build a snippet that isn't saved so we can render it.
    snippet = Snippet(template=template, data=data)
    return render(request, 'base/preview.html', {
        'snippet': snippet,
        'client': PREVIEW_CLIENT,
        'preview': True
    })
Exemplo n.º 3
0
def preview_snippet(request):
    """
    Build a snippet using info from the POST parameters, and preview that
    snippet on a mock about:home page.
    """
    try:
        template_id = int(request.POST.get('template_id', None))
    except (TypeError, ValueError):
        return HttpResponseBadRequest()

    template = get_object_or_none(SnippetTemplate, id=template_id)
    data = request.POST.get('data', None)

    # Validate that data is JSON.
    try:
        json.loads(data)
    except (TypeError, ValueError):
        data = None

    # If your parameters are wrong, I have no sympathy for you.
    if data is None or template is None:
        return HttpResponseBadRequest()

    # Build a snippet that isn't saved so we can render it.
    snippet = Snippet(template=template, data=data)

    if strtobool(request.POST.get('activity_stream', 'false')):
        template_name = 'base/preview_as.jinja'
        preview_client = Client('5', 'Firefox', '57.0', 'default', 'default',
                                'en-US', 'release', 'default', 'default',
                                'default')
    else:
        template_name = 'base/preview.jinja'
        preview_client = Client('4', 'Firefox', '24.0', 'default', 'default',
                                'en-US', 'release', 'default', 'default',
                                'default')

    skip_boilerplate = request.POST.get('skip_boilerplate', 'false')
    skip_boilerplate = strtobool(skip_boilerplate)
    if skip_boilerplate:
        template_name = 'base/preview_without_shell.jinja'

    return render(
        request, template_name, {
            'snippets_json': json.dumps([snippet.to_dict()]),
            'client': preview_client,
            'preview': True,
            'current_firefox_major_version':
            util.current_firefox_major_version(),
        })
Exemplo n.º 4
0
def preview_snippet(request):
    """
    Build a snippet using info from the POST parameters, and preview that
    snippet on a mock about:home page.
    """
    try:
        template_id = int(request.POST.get('template_id', None))
    except (TypeError, ValueError):
        return HttpResponseBadRequest()

    template = get_object_or_none(SnippetTemplate, id=template_id)
    data = request.POST.get('data', None)

    # Validate that data is JSON.
    try:
        json.loads(data)
    except (TypeError, ValueError):
        data = None

    # If your parameters are wrong, I have no sympathy for you.
    if data is None or template is None:
        return HttpResponseBadRequest()

    # Build a snippet that isn't saved so we can render it.
    snippet = Snippet(template=template, data=data)

    if strtobool(request.POST.get('activity_stream', 'false')):
        template_name = 'base/preview_as.jinja'
        preview_client = Client(5, 'Firefox', '57.0', 'default', 'default', 'en-US',
                                'release', 'default', 'default', 'default')
    else:
        template_name = 'base/preview.jinja'
        preview_client = Client(4, 'Firefox', '24.0', 'default', 'default', 'en-US',
                                'release', 'default', 'default', 'default')

    skip_boilerplate = request.POST.get('skip_boilerplate', 'false')
    skip_boilerplate = strtobool(skip_boilerplate)
    if skip_boilerplate:
        template_name = 'base/preview_without_shell.jinja'

    return render(request, template_name, {
        'snippets_json': json.dumps([snippet.to_dict()]),
        'client': preview_client,
        'preview': True,
        'current_firefox_major_version': util.current_firefox_major_version(),
    })
Exemplo n.º 5
0
def preview_snippet(request):
    """
    Build a snippet using info from the POST parameters, and preview that
    snippet on a mock about:home page.
    """
    try:
        template_id = int(request.POST.get('template_id', None))
    except (TypeError, ValueError):
        return HttpResponseBadRequest()

    template = get_object_or_none(SnippetTemplate, id=template_id)
    data = request.POST.get('data', None)

    # Validate that data is JSON.
    try:
        json.loads(data)
    except (TypeError, ValueError):
        data = None

    # If your parameters are wrong, I have no sympathy for you.
    if data is None or template is None:
        return HttpResponseBadRequest()

    # Build a snippet that isn't saved so we can render it.
    snippet = Snippet(template=template, data=data)

    skip_boilerplate = request.POST.get('skip_boilerplate', 'false')
    skip_boilerplate = strtobool(skip_boilerplate)

    template_name = 'base/preview_without_shell.jinja' if skip_boilerplate else 'base/preview.jinja'
    current_firefox_version = (
        version_list(product_details.firefox_history_major_releases)[0].split('.', 1)[0])

    return render(request, template_name, {
        'snippets_json': json.dumps([snippet.to_dict()]),
        'client': PREVIEW_CLIENT,
        'preview': True,
        'current_firefox_version': current_firefox_version,
    })
Exemplo n.º 6
0
def preview_snippet(request):
    """
    Build a snippet using info from the POST parameters, and preview that
    snippet on a mock about:home page.
    """
    try:
        template_id = int(request.POST.get('template_id', None))
    except (TypeError, ValueError):
        return HttpResponseBadRequest()

    template = get_object_or_none(SnippetTemplate, id=template_id)
    data = request.POST.get('data', None)

    # Validate that data is JSON.
    try:
        json.loads(data)
    except (TypeError, ValueError):
        data = None

    # If your parameters are wrong, I have no sympathy for you.
    if data is None or template is None:
        return HttpResponseBadRequest()

    # Build a snippet that isn't saved so we can render it.
    snippet = Snippet(template=template, data=data)

    skip_boilerplate = request.POST.get('skip_boilerplate', 'false')
    skip_boilerplate = strtobool(skip_boilerplate)

    template_name = 'base/preview_without_shell.html' if skip_boilerplate else 'base/preview.html'

    return render(
        request, template_name, {
            'snippets_json': json.dumps([snippet.to_dict()]),
            'client': PREVIEW_CLIENT,
            'preview': True,
        })
Exemplo n.º 7
0
 def test_does_not_exist(self):
     """Return None if no matching object exists."""
     value = get_object_or_none(Snippet, name='Does not exist')
     self.assertEqual(value, None)
Exemplo n.º 8
0
 def test_exists(self):
     """If no exceptions occur, return the matched object."""
     video = SnippetFactory.create(name='exists')
     value = get_object_or_none(Snippet, name='exists')
     self.assertEqual(value, video)
Exemplo n.º 9
0
 def test_multiple_objects_returned(self):
     """Return None if multiple objects are returned."""
     SnippetFactory.create(data='{"multiple": 1}')
     SnippetFactory.create(data='{"multiple": 1}')
     value = get_object_or_none(Snippet, data='{"multiple": 1}')
     self.assertEqual(value, None)
Exemplo n.º 10
0
 def test_does_not_exist(self):
     """Return None if no matching object exists."""
     value = get_object_or_none(Snippet, name='Does not exist')
     self.assertEqual(value, None)
Exemplo n.º 11
0
 def test_exists(self):
     """If no exceptions occur, return the matched object."""
     video = SnippetFactory.create(name='exists')
     value = get_object_or_none(Snippet, name='exists')
     self.assertEqual(value, video)
Exemplo n.º 12
0
 def test_multiple_objects_returned(self):
     """Return None if multiple objects are returned."""
     SnippetFactory.create(data='{"multiple": 1}')
     SnippetFactory.create(data='{"multiple": 1}')
     value = get_object_or_none(Snippet, data='{"multiple": 1}')
     self.assertEqual(value, None)