コード例 #1
0
 def list_per_dimension(self, request: HttpRequest, system_name: str):
     """
     List Units grouped by dimension, filter on key, translated
     """
     language = validate_language(
         request.GET.get('language', request.LANGUAGE_CODE))
     try:
         key = request.GET.get('key', None)
         user = request.user if \
             hasattr(request, 'user') and \
             request.user.is_authenticated else None
         us = UnitSystem(system_name=system_name,
                         fmt_locale=language,
                         user=user,
                         key=key)
         dimensions = [
             Dimension(unit_system=us, code=code)
             for code in DIMENSIONS.keys()
         ]
         serializer = DimensionWithUnitsSerializer(
             dimensions, many=True, context={'request': request})
         return Response(serializer.data)
     except UnitSystemNotFound as e:
         logging.warning(str(e))
         return Response('Invalid Unit System',
                         status=status.HTTP_404_NOT_FOUND)
コード例 #2
0
 def list(self, request: HttpRequest, system_name: str):
     """
     List Units, ordered, filtered by key or dimension,
     translated in language
     """
     language = validate_language(
         request.GET.get('language', request.LANGUAGE_CODE))
     ordering = request.GET.get('ordering', 'name')
     descending = False
     if ordering and ordering[0] == '-':
         ordering = ordering[1:]
         descending = True
     if ordering not in ['code', 'name']:
         ordering = 'name'
     try:
         key = request.GET.get('key', None)
         user = request.user if \
             hasattr(request, 'user') and \
             request.user.is_authenticated else None
         us = UnitSystem(system_name=system_name,
                         fmt_locale=language,
                         user=user,
                         key=key)
         units = []
         dimension_param = request.GET.get(key='dimension')
         if dimension_param:
             try:
                 dimension = Dimension(unit_system=us, code=dimension_param)
                 units = dimension.units(user=user, key=key)
             except DimensionNotFound:
                 return Response('Invalid dimension filter',
                                 status=status.HTTP_400_BAD_REQUEST)
         else:
             available_units = us.available_unit_names()
             if available_units:
                 units = [
                     us.unit(unit_name=unit_name)
                     for unit_name in available_units
                 ]
         units = sorted(units,
                        key=lambda x: getattr(x, ordering),
                        reverse=descending)
         serializer = UnitSerializer(units,
                                     many=True,
                                     context={'request': request})
         return Response(serializer.data)
     except UnitSystemNotFound:
         return Response('Invalid Unit System',
                         status=status.HTTP_404_NOT_FOUND)
コード例 #3
0
 def dimensions(self, request, system_name):
     """
     List Dimensions of a UnitSystem
     """
     language = validate_language(
         request.GET.get('language', request.LANGUAGE_CODE))
     ordering = request.GET.get('ordering', 'name')
     try:
         us = UnitSystem(system_name=system_name, fmt_locale=language)
         serializer = DimensionSerializer(
             us.available_dimensions(ordering=ordering),
             many=True,
             context={'request': request})
         return Response(serializer.data, content_type="application/json")
     except UnitSystemNotFound as e:
         return Response("Unknown unit system: " + str(e),
                         status=HTTP_404_NOT_FOUND)
コード例 #4
0
 def compatible_units(self, request: HttpRequest, system_name: str,
                      unit_name: str):
     """
     List compatible Units
     """
     language = validate_language(
         request.GET.get('language', request.LANGUAGE_CODE))
     ordering = request.GET.get('ordering', 'name')
     descending = False
     if ordering and ordering[0] == '-':
         ordering = ordering[1:]
         descending = True
     if ordering not in ['code', 'name']:
         ordering = 'name'
     try:
         key = request.GET.get('key', None)
         user = request.user if \
             hasattr(request, 'user') and \
             request.user.is_authenticated else None
         us = UnitSystem(system_name=system_name,
                         fmt_locale=language,
                         user=user,
                         key=key)
         unit = us.unit(unit_name=unit_name)
         compatible_units = sorted([
             us.unit(unit_name=cunit)
             for cunit in map(str, unit.unit.compatible_units())
         ],
                                   key=lambda x: getattr(x, ordering),
                                   reverse=descending)
         serializer = UnitSerializer(compatible_units,
                                     many=True,
                                     context={'request': request})
         return Response(serializer.data, content_type="application/json")
     except (UnitSystemNotFound, UnitNotFound):
         return Response("Unknown unit", status=HTTP_404_NOT_FOUND)