Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
rip out product_details (no longer works with this django version)
Browse files Browse the repository at this point in the history
  • Loading branch information
oremj committed Dec 15, 2018
1 parent d80b94a commit ff76943
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 138 deletions.
88 changes: 42 additions & 46 deletions apps/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_GET, require_POST

from product_details import product_details
from lib import product_details

from api.decorators import has_perm_or_basicauth, logged_in_or_basicauth
from mirror.models import Location, Mirror, OS, Product, ProductAlias
Expand All @@ -17,8 +17,8 @@


def _get_command_list():
templates = os.listdir(os.path.join(os.path.dirname(__file__), 'templates',
'api', 'docs'))
templates = os.listdir(
os.path.join(os.path.dirname(__file__), 'templates', 'api', 'docs'))
# cut out the command names only
commands = [t[:-5] for t in templates if t.endswith('.html')]
commands.sort()
Expand All @@ -28,8 +28,8 @@ def _get_command_list():
def docindex(request):
"""API Doc Index"""
data = {'commands': _get_command_list()}
return render_to_response('api/index.html', data, context_instance=
RequestContext(request))
return render_to_response(
'api/index.html', data, context_instance=RequestContext(request))


@require_GET
Expand All @@ -42,14 +42,15 @@ def docs(request, command):
# XXX special cases are ugly
if command in ('product_add', 'product_language_add',
'product_language_delete'):
langs = product_details.languages.keys()
langs = product_details('languages').keys()
oses = OS.objects.order_by('name')
os_list = [os.name for os in oses]
data.update({'languages': langs,
'oses': os_list})
data.update({'languages': langs, 'oses': os_list})

return render_to_response('api/docs/%s.html' % command, data,
context_instance=RequestContext(request))
return render_to_response(
'api/docs/%s.html' % command,
data,
context_instance=RequestContext(request))


@has_perm_or_basicauth('mirror.view_uptake', HTTP_AUTH_REALM)
Expand All @@ -61,8 +62,8 @@ def uptake(request):
fuzzy = request.GET.get('fuzzy', False)
xml = XMLRenderer()
if not product and not os:
return xml.error('product and/or os are required GET parameters.',
errno=101)
return xml.error(
'product and/or os are required GET parameters.', errno=101)

product_names = None
if product:
Expand Down Expand Up @@ -131,7 +132,7 @@ def product_add(request):

# check languages
langs = request.POST.getlist('languages')
locales = product_details.languages.keys()
locales = product_details('languages').keys()
ssl_only = bool(request.POST.get('ssl_only', False) == "true")
if [l for l in langs if l not in locales]:
return xml.error('invalid language code(s)', errno=103)
Expand Down Expand Up @@ -166,9 +167,7 @@ def product_delete(request):
prodname = request.POST.get('product', None)
if not (prod_id or prodname):
return xml.error(
'Either product_id or product is required.',
errno=101
)
'Either product_id or product is required.', errno=101)
try:
if prod_id:
prod = Product.objects.get(pk=prod_id)
Expand Down Expand Up @@ -209,7 +208,7 @@ def product_language_add(request):

# check languages
langs = request.POST.getlist('languages')
locales = product_details.languages.keys()
locales = product_details('languages').keys()
if [l for l in langs if l not in locales]:
return xml.error('Invalid language code(s)', errno=103)
if prod.languages.filter(lang__in=langs):
Expand Down Expand Up @@ -255,7 +254,7 @@ def product_language_delete(request):
prod.languages.all().delete()
return xml.success('Deleted all languages from product %s' % prod.name)

locales = product_details.languages.keys()
locales = product_details('languages').keys()
if [l for l in langs if l not in locales]:
return xml.error('Invalid language code(s)', errno=103)

Expand All @@ -265,8 +264,8 @@ def product_language_delete(request):
return xml.error(e)
products = Product.objects.filter(pk=prod.pk)

return xml.success('Deleted languages %s from product %s' % (
', '.join(langs), prod.name))
return xml.success(
'Deleted languages %s from product %s' % (', '.join(langs), prod.name))


@require_GET
Expand Down Expand Up @@ -306,8 +305,8 @@ def location_add(request):
osname = request.POST.get('os', None)
path = request.POST.get('path', None)
if not (prodname and osname and path):
return xml.error('product, os, and path are required POST parameters.',
errno=101)
return xml.error(
'product, os, and path are required POST parameters.', errno=101)

try:
product = Product.objects.get(name=prodname)
Expand Down Expand Up @@ -399,45 +398,36 @@ def create_update_alias(request):
if 'alias' in form.errors:
if 'required' in form.errors['alias'][0]:
return xml.error(
'alias name is required.',
errno=form.E_ALIAS_REQUIRED
)
'alias name is required.', errno=form.E_ALIAS_REQUIRED)

if 'same name' in form.errors['alias'][0]:
return xml.error(
('You cannot create an alias with the same name as a '
'product'),
errno=form.E_ALIAS_PRODUCT_MATCH
)
errno=form.E_ALIAS_PRODUCT_MATCH)
if 'related_product' in form.errors:
if 'required' in form.errors['related_product'][0]:
return xml.error(
'related_product name is required.',
errno=form.E_RELATED_NAME_REQUIRED
)
errno=form.E_RELATED_NAME_REQUIRED)
if 'same name as an existing' in form.errors['related_product'][0]:
return xml.error(
'You cannot create alias with the same name as a product',
errno=form.E_ALIAS_PRODUCT_MATCH
)
errno=form.E_ALIAS_PRODUCT_MATCH)
if 'invalid' in form.errors['related_product'][0]:
return xml.error(
'You must specify a valid product to match with an alias',
errno=form.E_PRODUCT_DOESNT_EXIST
)
errno=form.E_PRODUCT_DOESNT_EXIST)

return xml.error(
'There was a problem validating the data provided',
errno=form.E_ALIAS_GENERAL_VALIDATION_ERROR
)
errno=form.E_ALIAS_GENERAL_VALIDATION_ERROR)

alias = form.cleaned_data['alias']
redirect = form.cleaned_data['related_product']

alias_obj, created = ProductAlias.objects.get_or_create(
alias=alias,
defaults={'related_product': redirect}
)
alias=alias, defaults={'related_product': redirect})

if not created:
alias_obj.related_product = redirect
Expand Down Expand Up @@ -528,10 +518,12 @@ def prepare_uptake_fake(self, products, oses):

def prepare_uptake(self, uptake):
"""Product uptake"""
content_map = {'product': 'location__product__name',
'os': 'location__os__name',
'available': 'available',
'total': 'total'}
content_map = {
'product': 'location__product__name',
'os': 'location__os__name',
'available': 'available',
'total': 'total'
}

root = self.doc.createElement('mirror_uptake')
self.doc.appendChild(root)
Expand All @@ -549,10 +541,14 @@ def success(self, message, render=True):

def error(self, message, errno=0, render=True):
"""Prepare an error message"""
return self.message(message, type='error', number=errno,
render=render, status=400)

def message(self, message, type='info', number=None, render=True,
return self.message(
message, type='error', number=errno, render=render, status=400)

def message(self,
message,
type='info',
number=None,
render=True,
status=200):
"""Prepare a single message"""
root = self.doc.createElement(type)
Expand Down
7 changes: 7 additions & 0 deletions apps/lib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import requests
import sys


def product_details(item):
res = requests.get(
'https://product-details.mozilla.org/1.0/%s.json' % item)
return res.json()


def docstring_trim(docstring):
"""Dedent a docstring. Code copied from PEP 257."""
if not docstring:
Expand Down
4 changes: 2 additions & 2 deletions apps/lib/tests.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from django import test

from product_details import product_details
from lib import product_details


class LanguageTestCase(test.TestCase):
"""Make sure our language list does what it should."""

def test_ja_jp_mac(self):
"""ja-JP-mac is not a default language, but we need it."""
assert 'ja-JP-mac' in product_details.languages.keys()
assert 'ja-JP-mac' in product_details('languages').keys()
Loading

0 comments on commit ff76943

Please sign in to comment.