views.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334
  1. # Copyright 2015 The Distro Tracker Developers
  2. # See the COPYRIGHT file at the top-level directory of this distribution and
  3. # at http://deb.li/DTAuthors
  4. #
  5. # This file is part of Distro Tracker. It is subject to the license terms
  6. # in the LICENSE file found in the top-level directory of this
  7. # distribution and at http://deb.li/DTLicense. No part of Distro Tracker,
  8. # including this file, may be copied, modified, propagated, or distributed
  9. # except according to the terms contained in the LICENSE file.
  10. """Views for the :mod:`distro_tracker.vendor.debian` app."""
  11. from __future__ import unicode_literals
  12. from django.utils.http import urlencode
  13. from django.views.generic import View
  14. from django.http import HttpResponseBadRequest
  15. from django.shortcuts import redirect
  16. class CodeSearchView(View):
  17. BASE_URL = 'https://codesearch.debian.net/search'
  18. def get(self, request):
  19. if 'query' not in request.GET or 'package' not in request.GET:
  20. return HttpResponseBadRequest('Both package and query are required '
  21. 'parameters')
  22. q = request.GET.get('query')
  23. if q == "":
  24. return HttpResponseBadRequest('Empty query is not allowed')
  25. package = request.GET.get('package')
  26. search = q + ' package:' + package
  27. url = self.BASE_URL + '?' + urlencode({'q': search})
  28. return redirect(url)