コード例 #1
0
    def test_datetime_timezone_awareness(self):
        # Addresses issue #24 - ensure that datetime strings terminating
        # in 'Z' are correctly handled.
        from rest_framework import serializers
        from rest_framework.renderers import JSONRenderer

        class PersonSerializer(serializers.ModelSerializer):
            class Meta:
                model = Person

        # Figure out what the date strings should look like based on the
        # serializer output.
        john = Person.objects.get(name="John")
        data = PersonSerializer(john).data
        datetime_str = JSONRenderer().render(parse_datetime(data['datetime_joined']) + datetime.timedelta(seconds=0.6)).decode('utf-8').strip('"')

        # This is more for documentation - DRF appends a 'Z' to timezone aware UTC datetimes when rendering:
        # https://github.com/tomchristie/django-rest-framework/blob/3.2.0/rest_framework/fields.py#L1002-L1006
        self.assertTrue(datetime_str.endswith('Z'))

        GET = {
            'datetime_joined__lte': datetime_str,
        }
        f = AllLookupsPersonDateFilter(GET, queryset=Person.objects.all())
        self.assertEqual(len(list(f)), 1)
        p = list(f)[0]
        self.assertEqual(p.name, "John")
コード例 #2
0
ファイル: views.py プロジェクト: guyuanlin/try-talk
	def perform_create(self, serializer):
		serializer.save(owner=self.request.user)

		# add push notification task for the created question
		renderer = JSONRenderer()
		question_data = renderer.render(data=serializer.data)
		tasks.push_question.delay(question_data)
コード例 #3
0
ファイル: mixins.py プロジェクト: Wealthymaury/Tornado-Django
	def _send_hook_request(self, obj, method):
		url = self._build_hook_url(obj)

		if method in ('POST', 'PUT'):
			serializer = self.get_serializer(obj)
			renderer = JSONRenderer()
			context = {'request': self.request}
			body = renderer.render(serializer.data, renderer_context=context)
		else:
			body = None

		headers = {
			'context-type': 'application/json',
			'X-Signature': self._build_hook_signarute(method, url, body)
		}

		try:
			print('Enviando peticion a :' + url + ' con metodo '+ method)
			response = requests.request(method, url, data=body, timeout=0.5, headers=headers)
			response.raise_for_status()
			print('Peticion enviada...')
		except requests.exceptions.ConnectionError:
			print('Error de conexion')
			# concexion rechazada
			pass
		except requests.exceptions.Timeout:
			print('Error de timeout')
			# timeout
			pass
		except requests.exceptions.RequestException, e:
			print('Error de 400 o 500', e.message)
			# error 4XX o 5XX
			pass
コード例 #4
0
ファイル: tasks.py プロジェクト: kubazarz/waw-data
def send_notifications(snapshot_time):
    positions = Position.objects.filter(snapshot_time=snapshot_time)

    serializer = PositionSerializer(positions, many=True)
    rendered = JSONRenderer().render(serializer.data)

    Group('trams/positions').send({'text': rendered.decode()})
コード例 #5
0
ファイル: views.py プロジェクト: jayoshih/content-curation
def channel(request, channel_id):
    channel = get_object_or_404(Channel, id=channel_id, deleted=False)
    channel_serializer =  ChannelSerializer(channel)

    accessible_channel_list = Channel.objects.filter( Q(deleted=False, public=True) | Q(deleted=False, editors__email__contains= request.user.email))
    accessible_channel_list = ChannelListSerializer.setup_eager_loading(accessible_channel_list)
    accessible_channel_list_serializer = ChannelListSerializer(accessible_channel_list, many=True)

    channel_list = accessible_channel_list.filter(editors__email__contains= request.user.email).exclude(id=channel_id).values("id", "name")
    fileformats = get_or_set_cached_constants(FileFormat, FileFormatSerializer)
    licenses = get_or_set_cached_constants(License, LicenseSerializer)
    formatpresets = get_or_set_cached_constants(FormatPreset, FormatPresetSerializer)
    contentkinds = get_or_set_cached_constants(ContentKind, ContentKindSerializer)

    channel_tags = ContentTag.objects.filter(channel = channel)
    channel_tags_serializer = TagSerializer(channel_tags, many=True)

    json_renderer = JSONRenderer()

    return render(request, 'channel_edit.html', {"channel" : json_renderer.render(channel_serializer.data),
                                                "channel_id" : channel_id,
                                                "channel_name": channel.name,
                                                "accessible_channels" : json_renderer.render(accessible_channel_list_serializer.data),
                                                "channel_list" : channel_list,
                                                "fileformat_list" : fileformats,
                                                 "license_list" : licenses,
                                                 "fpreset_list" : formatpresets,
                                                 "ckinds_list" : contentkinds,
                                                 "ctags": json_renderer.render(channel_tags_serializer.data),
                                                 "current_user" : json_renderer.render(CurrentUserSerializer(request.user).data)})
コード例 #6
0
ファイル: tests.py プロジェクト: gzbender/test_smyt
    def test_create_object(self):
        jr = JSONRenderer()

        for name, model in models.generated_models.models.items():
            serializer = models.generated_models.model_serializer(name)
            obj = G(model)
            pk = model._meta.pk.name
            # PUT
            data = serializer(N(model)).data
            if pk in data:
                del data[pk]
            url = reverse(self.view_name, kwargs={'model': name, 'pk': obj.id})
            response = self.client.put(url, jr.render(data), content_type='application/json')
            response_data = response.data
            if pk in response_data:
                del response_data[pk]
            self.assertDictEqual(data, response_data)
            # PATCH
            orig_data = serializer(obj).data
            patch_data = {}
            for key, val in orig_data.items():
                if key != pk and val != data.get(key):
                    patch_data = {
                        key: val
                    }
                    data[key] = val
                    break
            response = self.client.patch(url, jr.render(patch_data), content_type='application/json')
            response_data = response.data
            if pk in response_data:
                del response_data[pk]
            self.assertDictEqual(data, response_data)
コード例 #7
0
ファイル: views.py プロジェクト: bellhops/django-crud-filters
    def process_response(self, request, response):
        if 'HTTP_ACCEPT' in request.META.keys() and 'text/html' in request.META['HTTP_ACCEPT']:
            try:
                path = resolve(request.path)
            except Resolver404:
                pass
            else:
                if hasattr(path.func, 'cls') and hasattr(path.func.cls, 'crud_model'):
                    request._request = request

                    if not hasattr(response, 'data'):
                        content = {"Error": response.content}
                    else:
                        renderer = JSONRenderer()
                        renderer_context = {'indent': 4}
                        content = renderer.render(response.data, "application/json", renderer_context)

                    renderer_context = {
                        'content': content,
                        'request': request,
                        'response': response,
                        'args': {},
                        'kwargs': {}
                    }
                    return TemplateResponse(request, "api.html", renderer_context).render()
        return response
コード例 #8
0
ファイル: views.py プロジェクト: aronasorman/content-curation
def channel_page(request, channel, allow_edit=False):
    channel_serializer =  ChannelSerializer(channel)

    channel_list = Channel.objects.select_related('main_tree').prefetch_related('editors').prefetch_related('viewers')\
                            .exclude(id=channel.pk).filter(Q(deleted=False) & (Q(editors=request.user) | Q(viewers=request.user)))\
                            .annotate(is_view_only=Case(When(editors=request.user, then=Value(0)),default=Value(1),output_field=IntegerField()))\
                            .distinct().values("id", "name", "is_view_only").order_by('name')

    fileformats = get_or_set_cached_constants(FileFormat, FileFormatSerializer)
    licenses = get_or_set_cached_constants(License, LicenseSerializer)
    formatpresets = get_or_set_cached_constants(FormatPreset, FormatPresetSerializer)
    contentkinds = get_or_set_cached_constants(ContentKind, ContentKindSerializer)

    json_renderer = JSONRenderer()

    return render(request, 'channel_edit.html', {"allow_edit":allow_edit,
                                                "channel" : json_renderer.render(channel_serializer.data),
                                                "channel_id" : channel.pk,
                                                "channel_name": channel.name,
                                                "channel_list" : channel_list,
                                                "fileformat_list" : fileformats,
                                                "license_list" : licenses,
                                                "fpreset_list" : formatpresets,
                                                "ckinds_list" : contentkinds,
                                                "current_user" : json_renderer.render(CurrentUserSerializer(request.user).data),
                                                "preferences" : request.user.preferences,
                                            })
コード例 #9
0
    def test_render_dict_abc_obj(self):
        class Dict(MutableMapping):
            def __init__(self):
                self._dict = dict()

            def __getitem__(self, key):
                return self._dict.__getitem__(key)

            def __setitem__(self, key, value):
                return self._dict.__setitem__(key, value)

            def __delitem__(self, key):
                return self._dict.__delitem__(key)

            def __iter__(self):
                return self._dict.__iter__()

            def __len__(self):
                return self._dict.__len__()

            def keys(self):
                return self._dict.keys()

        x = Dict()
        x["key"] = "string value"
        x[2] = 3
        ret = JSONRenderer().render(x)
        data = json.loads(ret.decode("utf-8"))
        self.assertEquals(data, {"key": "string value", "2": 3})
コード例 #10
0
ファイル: views.py プロジェクト: nenodias/django-essencial
 def _send_hook_request(self, obj, method):
     url = self._build_hook_url(obj)
     if method in ('POST', 'PUT'):
         #compõe o corpo
         serializer = self.get_serializer(obj)
         rendererer = JSONRenderer()
         context = {'request': self.request}
         body = rendererer.render(serializer.data, renderer_context=context)
     else:
         body = None
     headers = {
         'content-type': 'application/json',
         'X-Signature': self._build_hook_signature(method, url, body)
     }
     try:
         s = requests.Session()
         requisicao = requests.Request(method, url, data=body, headers=headers)
         prepared = requisicao.prepare()
         response = s.send(prepared, timeout=0.5)
         response.raise_for_status()
     except requests.exceptions.ConnectionError:
         # Host não pôde ser resolvido ou a conexão foi recusada
         pass
     except requests.exceptions.Timeout:
         # Solicitação expirou
         pass
     except requests.exceptions.RequestException:
         # Servidor respondeu com código de status 4XX ou 5XX
         pass
コード例 #11
0
    def test_render_dict_abc_obj(self):
        class Dict(MutableMapping):
            def __init__(self):
                self._dict = dict()

            def __getitem__(self, key):
                return self._dict.__getitem__(key)

            def __setitem__(self, key, value):
                return self._dict.__setitem__(key, value)

            def __delitem__(self, key):
                return self._dict.__delitem__(key)

            def __iter__(self):
                return self._dict.__iter__()

            def __len__(self):
                return self._dict.__len__()

            def keys(self):
                return self._dict.keys()

        x = Dict()
        x['key'] = 'string value'
        x[2] = 3
        ret = JSONRenderer().render(x)
        data = json.loads(ret.decode('utf-8'))
        self.assertEquals(data, {'key': 'string value', '2': 3})
コード例 #12
0
ファイル: views.py プロジェクト: acecodes/acescrum
 def _send_hook_request(self, obj, method):
     url = self._build_hook_url(obj)
     if method in ('post', 'put'):
         # Build body
         serializer = self.get_serializer(obj)
         renderer = JSONRenderer()
         context = {'request': self.request}
         body = renderer.render(serializer.data, renderer_context=context)
     else:
         body = None
     headers = {
         'content-type': 'application/json',
         'X-Signature': self._build_hook_signature(method, url, body),
     }
     try:
         response = requests.request(method, url,
                                     timeout=0.5, headers=headers)
         response.raise_for_status()
     except requests.exceptions.ConnectionError:
         """Host cannot be resolved or connection refused"""
         pass
     except requests.exceptions.Timeout:
         """Request timed out"""
         pass
     except requests.exceptions.RequestException:
         """Server response of 4XX or 5XX"""
         pass
コード例 #13
0
ファイル: views.py プロジェクト: cheshire-howe/scrumboard
    def _send_hook_request(self, obj, method):
        url = self._build_hook_url(obj)

        if method in ('POST', 'PUT'):
            # Build the body
            serializer = self.get_serializer(obj)
            renderer = JSONRenderer()
            context = {'request': self.request}
            body = renderer.render(serializer.data, renderer_context=context)
        else:
            body = None
        headers = {
            'content-type': 'application/json',
            'X-Signature': self._build_hook_signature(method, url, body)
        }

        try:
            response = requests.request(method, url, data=body,
                                        timeout=0.5, headers=headers)
            response.raise_for_status()
        except requests.exceptions.ConnectionError:
            # Host could not be resolved or connection refused
            pass
        except requests.exceptions.Timeout:
            # Request timed out
            pass
        except requests.exceptions.RequestException:
            # Server responded with 4XX or 5XX status code
            pass
コード例 #14
0
 def test_with_content_type_args(self):
     """
     Test JSON rendering with additional content type arguments supplied.
     """
     obj = {'foo': ['bar', 'baz']}
     renderer = JSONRenderer()
     content = renderer.render(obj, 'application/json; indent=2')
     self.assertEqual(strip_trailing_whitespace(content.decode('utf-8')), _indented_repr)
コード例 #15
0
 def test_compact(self):
     renderer = JSONRenderer()
     data = OrderedDict([('a', 1), ('b', 2)])
     context = {'indent': 4}
     assert (
         renderer.render(data, renderer_context=context) ==
         b'{\n    "a": 1,\n    "b": 2\n}'
     )
コード例 #16
0
 def test_with_content_type_args(self):
     """
     Test JSON rendering with additional content type arguments supplied.
     """
     obj = {"foo": ["bar", "baz"]}
     renderer = JSONRenderer()
     content = renderer.render(obj, "application/json; indent=2")
     self.assertEqual(strip_trailing_whitespace(content.decode("utf-8")), _indented_repr)
コード例 #17
0
ファイル: serializers.py プロジェクト: ofpiyush/hedwig-py
def emit_hedwig_serializer_data(action, module_name, serializer_name, serializer):
    if not bool(hedwig_rest_framework_settings.SERIALIZER_SIGNALS):
        return

    routing_key = '.'.join([project_name, module_name, 'serializer', serializer_name, action,
                            str(serializer.instance.pk)])
    json_renderer = JSONRenderer()
    hedwig_emitter.emit(routing_key, json_renderer.render(serializer.data))
コード例 #18
0
    def get_context_data(self, **kwargs):
        # Add some serialized json for bootstrapping the client-side app
        renderer = JSONRenderer()
        kwargs['project_json'] = renderer.render(serializers.ProjectSerializer(self.get_project()).data)
        kwargs['scheme_json'] = renderer.render(serializers.SchemeSerializer(self.object).data)
        kwargs['user_json'] = renderer.render(serializers.UserSerializer(self.request.user).data)

        return super(SchemeEditorView, self).get_context_data(**kwargs)
コード例 #19
0
 def test_u2028_u2029(self):
     # The \u2028 and \u2029 characters should be escaped,
     # even when the non-escaping unicode representation is used.
     # Regression test for #2169
     obj = {'should_escape': '\u2028\u2029'}
     renderer = JSONRenderer()
     content = renderer.render(obj, 'application/json')
     self.assertEqual(content, '{"should_escape":"\\u2028\\u2029"}'.encode('utf-8'))
コード例 #20
0
def _handle_error(status_code):
    info = {
        'status': status_code
    }

    renderer = JSONRenderer()
    content_type = '{media}; charset={charset}'.format(media=renderer.media_type, charset=renderer.charset)
    return HttpResponse(renderer.render(info), content_type=content_type, status=status_code)
コード例 #21
0
 def test_without_content_type_args(self):
     """
     Test basic JSON rendering.
     """
     obj = {'foo': ['bar', 'baz']}
     renderer = JSONRenderer()
     content = renderer.render(obj, 'application/json')
     # Fix failing test case which depends on version of JSON library.
     self.assertEqual(content.decode('utf-8'), _flat_repr)
コード例 #22
0
 def test_without_content_type_args(self):
     """
     Test basic JSON rendering.
     """
     obj = {"foo": ["bar", "baz"]}
     renderer = JSONRenderer()
     content = renderer.render(obj, "application/json")
     # Fix failing test case which depends on version of JSON library.
     self.assertEqual(content.decode("utf-8"), _flat_repr)
コード例 #23
0
ファイル: views.py プロジェクト: Omegaice/SmartLoad
 def render(self, data=None, accepted_media_type=None, renderer_context=None):
     if isinstance(data, bytes):
         return data
     else:
         jr = JSONRenderer()
         self.media_type = jr.media_type
         self.format = jr.format
         self.charset = jr.charset
         self.render_style = jr.render_style
         return jr.render(data, accepted_media_type=accepted_media_type, renderer_context=renderer_context)
コード例 #24
0
ファイル: topics.py プロジェクト: upconsulting/jhb-explorer
def topics_json(request):
    """
    Generates a JSON response containing topic data.
    """
    queryset = Topic.objects.all()

    serializer = TopicSerializer(queryset, many=True)
    renderer = JSONRenderer()
    response_data = renderer.render(serializer.data,
                                    'application/json; indent=4')
    return HttpResponse(response_data, content_type="application/json")
コード例 #25
0
ファイル: auth.py プロジェクト: parkouss/treeherder
    def authenticate(self, request):

        if not self.auth_detected(request):
            return None

        user = request.resolver_match.kwargs.get('project') or request.query_params['user']
        project_credentials = OAuthCredentials.get_credentials(user)

        if not project_credentials:
            raise exceptions.ValidationError(
                'project {0} has no OAuth credentials'.format(user)
            )
        parameters = OAuthCredentials.get_parameters(request.query_params)

        oauth_consumer_key = parameters['oauth_consumer_key']

        if oauth_consumer_key != project_credentials['consumer_key']:
            raise exceptions.AuthenticationFailed(
                'oauth_consumer_key does not match credentials for project {0}'.format(user)
            )

        uri = '{0}://{1}{2}'.format(
            settings.TREEHERDER_REQUEST_PROTOCOL,
            request.get_host(),
            request.path
        )
        # Construct the OAuth request based on the django request object
        json_renderer = JSONRenderer()
        req_obj = oauth.Request(
            method=request.method,
            url=uri,
            parameters=parameters,
            body=json_renderer.render(request.data),
        )
        server = oauth.Server()
        token = oauth.Token(key='', secret='')
        # Get the consumer object
        cons_obj = oauth.Consumer(
            oauth_consumer_key,
            project_credentials['consumer_secret']
        )
        # Set the signature method
        server.add_signature_method(oauth.SignatureMethod_HMAC_SHA1())

        try:
            # verify oauth django request and consumer object match
            server.verify_request(req_obj, cons_obj, token)
        except oauth.Error:
            raise exceptions.AuthenticationFailed(
                'Client authentication failed for project {0}'.format(user)
            )
        request.legacy_oauth_authenticated = True
        return (DummyUser(), None)
コード例 #26
0
ファイル: views.py プロジェクト: matthewcpp/tight_budget
 def get(self, request):
     budget = Budget.objects.order_by("-updated_time")[:1][0]
     categories = budget.categories.all();
     
     budget_serializer = BudgetSerializer(budget)
     categories_serializer = CategorySerializer(categories, many=True)
     
     json_renderer = JSONRenderer();
     budget_json_data = json_renderer.render(budget_serializer.data)
     categories_json_data = json_renderer.render(categories_serializer.data)
     
     return TemplateResponse(request, 'budget_ui/index.html', {"budget_data": budget_json_data, "categories_data": categories_json_data})
コード例 #27
0
ファイル: pages.py プロジェクト: LocalGround/localground
 def get_context_data(self, map_slug, *args, **kwargs):
     renderer = JSONRenderer()
     context = super(PublicView, self).get_context_data(
         *args, **kwargs)
     if self._has_access():
         projectJSON = ProjectDetailSerializer(
             self.map.project, context={'request': {}}).data
         mapJSON = MapSerializerDetail(
             self.map, context={'request': {}}).data
         context.update({
             'project': renderer.render(projectJSON),
             'map': renderer.render(mapJSON)
         })
     return context
コード例 #28
0
ファイル: ui.py プロジェクト: AmirKremer/openmuni-budgets
    def get_context_data(self, **kwargs):

        context = super(ToolDetailView, self).get_context_data(**kwargs)
        user = self.request.user
        renderer = JSONRenderer()
        user_object = {}

        # add logged in user
        if user.is_authenticated():
            user_object = AccountMin(user).data

        context['user_json'] = renderer.render(user_object)

        return context
コード例 #29
0
ファイル: pages.py プロジェクト: LocalGround/localground
    def get_context_data(self, project_id, *args, **kwargs):
        from localground.apps.site.api.serializers import \
            ProjectDetailSerializer
        from rest_framework.renderers import JSONRenderer

        context = super(MainView, self).get_context_data(
            *args, **kwargs)

        project = Project.objects.get(id=project_id)
        serializer = ProjectDetailSerializer(
            project,
            context={'request': {}}
        )
        renderer = JSONRenderer()
        context.update({'project': renderer.render(serializer.data)})
        return context
コード例 #30
0
ファイル: ui.py プロジェクト: AmirKremer/openmuni-budgets
    def get_context_data(self, **kwargs):

        context = super(ToolDetailBaseView, self).get_context_data(**kwargs)
        state = {}
        renderer = JSONRenderer()

        try:
            state_id = self.kwargs.get('state')
            state = serializers.State(models.State.objects.get(id=state_id), context={'request': self.request}).data
        except models.State.DoesNotExist:
            pass

        context['tool_json'] = renderer.render(serializers.Tool(self.object, context={'request': self.request}).data)
        context['state_json'] = renderer.render(state)

        return context
コード例 #31
0
 def serialize_to_json(self):
     serializer = NotificationSerializer(self)
     return JSONRenderer().render(serializer.data)
コード例 #32
0
ファイル: views.py プロジェクト: huaguang/rest_im
 def __init__(self, data, token = None, **kwargs):
     content = JSONRenderer().render(data)
     kwargs['content_type'] = 'application/json'
     super(JsonResponse, self).__init__(content, **kwargs)
     if token:
         super(JsonResponse, self).set_cookie('token', token)
コード例 #33
0
ファイル: views.py プロジェクト: vidyar/nodeshot
def index(request):
    # django-rest-framework serializer context
    serializer_context = {'request': request}
    # load models
    layers = Layer.objects.published()
    status = Status.objects.all()
    menu = MenuItem.objects.published().filter(parent=None).accessible_to(
        request.user)
    # initialize serializers
    layers = LayerDetailSerializer(layers,
                                   many=True,
                                   context=serializer_context).data
    status = StatusListSerializer(status,
                                  many=True,
                                  context=serializer_context).data
    menu = MenuSerializer(menu, many=True, context=serializer_context).data
    # initialize user serializer if authenticated
    if request.user.is_authenticated():
        user = ProfileOwnSerializer(request.user,
                                    many=False,
                                    context=serializer_context).data
    else:
        user = {}
    # add link legend
    if LINKS_ENABLED:
        status += links_legend
    # initialize django-rest-framework JSON renderer
    json_renderer = JSONRenderer()
    # template context
    context = {
        # preloaded data
        'layers':
        json_renderer.render(layers),
        'legend':
        json_renderer.render(status),
        'menu':
        json_renderer.render(menu),
        'user':
        json_renderer.render(user),
        # settings
        'SITE_NAME':
        ui_settings.settings.SITE_NAME,
        'SITE_URL':
        ui_settings.settings.SITE_URL,
        'MAP_CENTER':
        ui_settings.MAP_CENTER,
        'MAP_ZOOM':
        ui_settings.MAP_ZOOM,
        'LEAFLET_OPTIONS':
        json.dumps(ui_settings.LEAFLET_OPTIONS),
        'DISABLE_CLUSTERING_AT_ZOOM':
        ui_settings.DISABLE_CLUSTERING_AT_ZOOM,
        'MAX_CLUSTER_RADIUS':
        ui_settings.MAX_CLUSTER_RADIUS,
        'DATETIME_FORMAT':
        ui_settings.DATETIME_FORMAT,
        'DATE_FORMAT':
        ui_settings.DATE_FORMAT,
        'ADDRESS_SEARCH_TRIGGERS':
        ui_settings.ADDRESS_SEARCH_TRIGGERS,
        'LOGO':
        ui_settings.LOGO,
        # participation settings
        'VOTING_ENABLED':
        ui_settings.VOTING_ENABLED,
        'RATING_ENABLED':
        ui_settings.RATING_ENABLED,
        'COMMENTS_ENABLED':
        ui_settings.COMMENTS_ENABLED,
        'CONTACTING_ENABLED':
        ui_settings.CONTACTING_ENABLED,
        # map features (TODO: currently unimplemented)
        'MAP_3D_ENABLED':
        ui_settings.MAP_3D_ENABLED,
        'MAP_TOOLS_ENABLED':
        ui_settings.MAP_TOOLS_ENABLED,
        'MAP_PREFERENCES_ENABLED':
        ui_settings.MAP_PREFERENCES_ENABLED,
        # social auth settings
        'SOCIAL_AUTH_ENABLED':
        ui_settings.SOCIAL_AUTH_ENABLED,
        'FACEBOOK_ENABLED':
        ui_settings.FACEBOOK_ENABLED,
        'GOOGLE_ENABLED':
        ui_settings.GOOGLE_ENABLED,
        'GITHUB_ENABLED':
        ui_settings.GITHUB_ENABLED,
        # websockets settings
        'WEBSOCKETS':
        ui_settings.WEBSOCKETS,
        # profiles settings
        'REGISTRATION_OPEN':
        ui_settings.REGISTRATION_OPEN,
        # additional node fields
        'NODES_HSTORE_SCHEMA':
        json.dumps(NODES_HSTORE_SCHEMA if NODES_HSTORE_SCHEMA else []),
        # analytics
        'GOOGLE_ANALYTICS_UA':
        ui_settings.GOOGLE_ANALYTICS_UA,
        'GOOGLE_ANALYTICS_OPTIONS':
        json.dumps(ui_settings.GOOGLE_ANALYTICS_OPTIONS),
        'PIWIK_ANALYTICS_BASE_URL':
        ui_settings.PIWIK_ANALYTICS_BASE_URL,
        'PIWIK_ANALYTICS_SITE_ID':
        ui_settings.PIWIK_ANALYTICS_SITE_ID,
        # miscellaneous
        'ADDITIONAL_GEOJSON_URLS':
        json.dumps(ui_settings.ADDITIONAL_GEOJSON_URLS),
        'PRIVACY_POLICY_LINK':
        ui_settings.PRIVACY_POLICY_LINK,
        'TERMS_OF_SERVICE_LINK':
        ui_settings.TERMS_OF_SERVICE_LINK,
        # networking
        'LINKS_ENABLED':
        LINKS_ENABLED,
        # metrics
        'METRICS_ENABLED':
        ui_settings.METRICS_ENABLED
    }
    return render(request, 'index.html', context)
コード例 #34
0
    def recomm_overall(self, request):

        # 사용자의 좋아요 리스트에 있는 웹툰 목록
        user = get_object_or_404(get_user_model(), pk=request.user.user_id)
        user_id = get_user_model().objects.values('user_id').get(
            username=user.username)
        favorite_webtoons = Webtoon.objects.filter(
            like_users=user_id['user_id'])

        # 웹툰 전체 목록
        webtoons = Webtoon.objects.all()

        # 1. 사용자 장르 벡터와 웹툰 장르 벡터의 유사도가 높은 순서
        if len(favorite_webtoons) != 0:
            # 사용자의 장르 벡터
            serializer = WebtoonSerializer(favorite_webtoons, many=True)
            webtoon_data = serializer.data
            user_genres_matrix = genre_recomm.user_to_matrix(webtoon_data)

            # 웹툰들의 장르 벡터
            df_webtoon = genre_recomm.webtoon_to_dataframe(webtoons)
            genre_mat = genre_recomm.webtoon_to_matrix(df_webtoon)

            # 각각의 유사도 계산
            similarity = genre_recomm.cal_similarity(user_genres_matrix,
                                                     genre_mat)

            # 유사도 순 정렬
            sorted_similarity = sorted(similarity.items(),
                                       reverse=True,
                                       key=lambda item: item[1])

            # id값에 해당하는 웹툰 정보 응답
            genre_result = []
            for i in range(10):
                # 유사도 높은 웹툰의 리스트는 0부터 시작하기 때문에 idx에 1씩 더해준다.
                webtoon = Webtoon.objects.get(
                    webtoon_number=sorted_similarity[i][0] + 1)
                serializer = WebtoonSerializer(webtoon)
                genre_result.append(serializer.data)
        else:
            genre_result = []

    # 2. 좋아요한 웹툰의 작가가 쓴 다른 웹툰들
        artists_result = []

        for favorite_webtoon in favorite_webtoons:
            writer = favorite_webtoon.webtoon_writer
            # 이미 존재하는 웹툰의 id값
            already_num = favorite_webtoon.webtoon_number

            for webtoon in webtoons:
                # 좋아요한 웹툰의 작가가 그린 다른 웹툰 and 이미 좋아요 한 웹툰
                if webtoon.webtoon_writer in writer and webtoon.webtoon_number != already_num:
                    serializer = WebtoonSerializer(webtoon)
                    # json 형태로 변환
                    artist_json = JSONRenderer().render(serializer.data)
                    artist_stream = io.BytesIO(artist_json)
                    artist_data = JSONParser().parse(artist_stream)
                    # key-value 추가
                    artist_data['favorite_writer'] = writer

                    artists_result.append(artist_data)

    # 3. 좋아요한 웹툰과 줄거리 유사도 높은 웹툰들
        df_webtoon = summary_recomm.to_dataframe(webtoons)
        overview_sim_sorted_ind = summary_recomm.tokenizer(df_webtoon)

        summmary_result = []

        for favorite_webtoon in favorite_webtoons:
            title = favorite_webtoon.webtoon_name

            # 찜목록에 있는 모든 웹툰리스트들과 가장 유사도가 높은 3개를 출력
            similar_webtoons = summary_recomm.find_sim_movie_ver2(
                df_webtoon, overview_sim_sorted_ind, '{}'.format(title), 3)

            similar_webtoons_json = json.loads(similar_webtoons)

            for result in similar_webtoons_json:
                webtoon = Webtoon.objects.get(
                    webtoon_number=result['webtoon_number'])
                serializer = WebtoonSerializer(webtoon)
                # json 형태로 변환
                summary_json = JSONRenderer().render(serializer.data)
                summary_stream = io.BytesIO(summary_json)
                summary_data = JSONParser().parse(summary_stream)
                # key-value 추가
                summary_data['similar_webtoon'] = title

                summmary_result.append(summary_data)

    # 4. 다른 사람들이 높게 평가한 웹툰들
        score_webtoons = Webtoon.objects.order_by("-webtoon_score")[:10]
        score_serializer = WebtoonSerializer(score_webtoons, many=True)
        score_result = score_serializer.data

        # 5. 무작위 추천 웹툰(계속해서 비슷한 것만 나오게 하는 것 방지)
        random_webtoon = Webtoon.objects.order_by("?")[:5]
        random_serializer = WebtoonSerializer(random_webtoon, many=True)
        random_result = random_serializer.data

        # 6. 좋아요한 웹툰과 줄거리 유사가 낮은 웹툰들
        df_webtoon = summary_recomm.to_dataframe(webtoons)
        overview_sim_sorted_ind = summary_recomm.opposition_tokenizer(
            df_webtoon)

        opposition_result = []

        for favorite_webtoon in favorite_webtoons:
            title = favorite_webtoon.webtoon_name

            # 찜목록에 있는 모든 웹툰리스트들과 가장 유사도가 낮은 30개를 출력
            unsimilar_webtoons = summary_recomm.find_sim_movie_ver2(
                df_webtoon, overview_sim_sorted_ind, '{}'.format(title), 30)
            results = json.loads(unsimilar_webtoons)

            # 30개중 3개씩 랜덤추출
            results = random.sample(results, 3)

            for result in results:
                webtoon = Webtoon.objects.get(
                    webtoon_number=result['webtoon_number'])
                opposition_serializer = WebtoonSerializer(webtoon)
                # json 형태로 변환
                opposition_json = JSONRenderer().render(
                    opposition_serializer.data)
                opposition_stream = io.BytesIO(opposition_json)
                opposition_data = JSONParser().parse(opposition_stream)
                # key-value 추가
                opposition_data['opposition_webtoon'] = title

                opposition_result.append(opposition_data)

    # 응답
        return Response([
            {
                "이런 장르의 작품을 좋아하시네요!": genre_result
            },
            {
                "좋아하는 작가의 다른 작품": artists_result
            },
            {
                "좋아하는 작품과 줄거리 유사한": summmary_result
            },
            {
                "다른 사람들이 좋은 평가를 한": score_result
            },
            {
                "이런건 어떤가요?": random_result
            },
            {
                "평소에는 보지 않지만 어떠세요?": opposition_result
            },
        ])
コード例 #35
0
ファイル: files.py プロジェクト: toccotedd/studio
def subtitle_upload(request):
    # File will be converted to VTT format
    ext = file_formats.VTT
    language_id = request.META.get('HTTP_LANGUAGE')
    content_file = request.FILES.values()[0]

    with NamedTemporaryFile() as temp_file:
        try:
            converter = build_subtitle_converter(
                unicode(content_file.read(), 'utf-8'))
            convert_language_code = language_id

            # We're making the assumption here that language the user selected is truly the caption
            # file's language if it's unknown
            if len(converter.get_language_codes()) == 1 \
                    and converter.has_language(LANGUAGE_CODE_UNKNOWN):
                converter.replace_unknown_language(language_id)

            # determine if the request language exists by another code, otherwise we can't continue
            if not converter.has_language(convert_language_code):
                for language_code in converter.get_language_codes():
                    language = getlang_by_alpha2(language_code)
                    if language and language.code == language_id:
                        convert_language_code = language_code
                        break
                else:
                    return HttpResponseBadRequest(
                        "Language '{}' not present in subtitle file".format(
                            language_id))

            converter.write(temp_file.name, convert_language_code)
        except InvalidSubtitleFormatError as ex:
            return HttpResponseBadRequest(
                "Subtitle conversion failed: {}".format(ex))

        temp_file.seek(0)
        converted_file = DjFile(temp_file)

        checksum = get_hash(converted_file)
        size = converted_file.size
        request.user.check_space(size, checksum)

        file_object = File(
            file_size=size,
            file_on_disk=converted_file,
            checksum=checksum,
            file_format_id=ext,
            original_filename=request.FILES.values()[0]._name,
            preset_id=request.META.get('HTTP_PRESET'),
            language_id=language_id,
            uploaded_by=request.user,
        )
        file_object.save()

    return HttpResponse(
        json.dumps({
            "success":
            True,
            "filename":
            str(file_object),
            "file":
            JSONRenderer().render(FileSerializer(file_object).data)
        }))
コード例 #36
0
 def test_outgoingtransaction_serializer_renders(self):
     obj = OutgoingTransaction.objects.last()
     serializer = OutgoingTransactionSerializer(obj)
     serializer.data
     self.assertTrue(JSONRenderer().render(serializer.data))
コード例 #37
0
ファイル: tasks.py プロジェクト: swipswaps/courtlistener
def write_json_to_disk(courts, obj_type_str, obj_class, court_attr, serializer,
                       bulk_dir):
    """Write all items to disk as json files inside directories named by
    jurisdiction.

    The main trick is that we identify if we are creating a bulk archive
    from scratch. If so, we iterate over everything. If not, we only
    iterate over items that have been modified since the last good date.

    We deal with two kinds of bulk data. The first is jurisdiction-centric, in
    which we want to make bulk data for that particular jurisdiction, such as
    opinions or PACER data, or whatever. The second is non-jurisdiction-
    specific, like people or schools. For jurisdiction-specific data, we make
    jurisdiction directories to put the data into. Otherwise, we do not.

    :param courts: Court objects that you expect to make data for.
    :param obj_type_str: A string to use for the directory name of a type of
    data. For example, for clusters, it's 'clusters'.
    :param obj_class: The actual class to make a bulk data for.
    :param court_attr: A string that can be used to find the court attribute
    on an object. For example, on clusters, this is currently docket.court_id.
    :param serializer: A DRF serializer to use to generate the data.
    :param bulk_dir: A directory to place the serialized JSON data into.

    :returns int: The number of items generated
    """
    # Are there already bulk files?
    history = BulkJsonHistory(obj_type_str, bulk_dir)
    last_good_date = history.get_last_good_date()
    history.add_current_attempt_and_save()

    if court_attr is not None:
        # Create a directory for every jurisdiction, if they don't already
        # exist. This does not clobber.
        for court in courts:
            mkdir_p(join(
                bulk_dir,
                obj_type_str,
                court.pk,
            ))
    else:
        # Make a directory for the object type.
        mkdir_p(join(bulk_dir, obj_type_str))

    if last_good_date is not None:
        print(
            "   - Incremental data found. Assuming it's good and using it...")
        qs = obj_class.objects.filter(date_modified__gte=last_good_date)
    else:
        print("   - Incremental data not found. Working from scratch...")
        qs = obj_class.objects.all()

    if qs.count() == 0:
        print("   - No %s-type items in the DB or none that have changed. All "
              "done here." % obj_type_str)
        history.mark_success_and_save()
        return 0
    else:
        if type(qs[0].pk) == int:
            item_list = queryset_generator(qs)
        else:
            # Necessary for Court objects, which don't have ints for ids.
            item_list = qs

        i = 0
        renderer = JSONRenderer()
        r = RequestFactory().request()
        r.META[
            'SERVER_NAME'] = 'www.courtlistener.com'  # Else, it's testserver
        r.META['SERVER_PORT'] = '443'  # Else, it's 80
        r.META['wsgi.url_scheme'] = 'https'  # Else, it's http.
        r.version = 'v3'
        r.versioning_scheme = URLPathVersioning()
        context = dict(request=r)
        for item in item_list:
            if i % 1000 == 0:
                print("Completed %s items so far." % i)
            json_str = renderer.render(
                serializer(item, context=context).data,
                accepted_media_type='application/json; indent=2',
            )

            if court_attr is not None:
                loc = join(bulk_dir, obj_type_str,
                           deepgetattr(item, court_attr), '%s.json' % item.pk)
            else:
                # A non-jurisdiction-centric object.
                loc = join(bulk_dir, obj_type_str, '%s.json' % item.pk)

            with open(loc, 'wb') as f:
                f.write(json_str)
            i += 1

        print('   - %s %s json files created.' % (i, obj_type_str))

        history.mark_success_and_save()
        return i
コード例 #38
0
 def test_render_queryset_values_list(self):
     o = DummyTestModel.objects.create(name='dummy')
     qs = DummyTestModel.objects.values_list('id', 'name')
     ret = JSONRenderer().render(qs)
     data = json.loads(ret.decode('utf-8'))
     self.assertEqual(data, [[o.id, o.name]])
コード例 #39
0
 def test_proper_encoding(self):
     obj = {'countries': ['United Kingdom', 'France', 'España']}
     renderer = JSONRenderer()
     content = renderer.render(obj, 'application/json')
     self.assertEqual(content, '{"countries":["United Kingdom","France","España"]}'.encode('utf-8'))
コード例 #40
0
 def test_indented(self):
     renderer = JSONRenderer()
     data = OrderedDict([('a', 1), ('b', 2)])
     assert renderer.render(data) == b'{"a":1,"b":2}'
コード例 #41
0
 def test_read_skill(self):  # output - json
     skill = Skill.objects.get(pk=self.skill_id)
     serializer = SkillSerializer(skill)
     json = JSONRenderer().render(serializer.data)
     self.assertIsNotNone(json)
コード例 #42
0
ファイル: views.py プロジェクト: jonboiser/studio
def get_user_pending_channels(request):
    pending_list = Invitation.objects.select_related('channel').select_related(
        'sender').filter(invited=request.user)
    invitation_serializer = InvitationSerializer(pending_list, many=True)

    return HttpResponse(JSONRenderer().render(invitation_serializer.data))
コード例 #43
0
ファイル: views.py プロジェクト: jonboiser/studio
def get_user_channels(request):
    channel_list = Channel.objects.prefetch_related('editors').prefetch_related('viewers').filter(Q(deleted=False) & (Q(editors=request.user.pk) | Q(viewers=request.user.pk)))\
                    .annotate(is_view_only=Case(When(editors=request.user, then=Value(0)),default=Value(1),output_field=IntegerField()))
    channel_serializer = ChannelListSerializer(channel_list, many=True)

    return HttpResponse(JSONRenderer().render(channel_serializer.data))
コード例 #44
0
 def test_long_form(self):
     renderer = JSONRenderer()
     renderer.compact = False
     data = OrderedDict([('a', 1), ('b', 2)])
     assert renderer.render(data) == b'{"a": 1, "b": 2}'
コード例 #45
0
 def set_item(self, key: str, value: [Service], ttl):
     if not value:
         return
     serializer = ServiceSerializer(value, many=True)
     json = JSONRenderer().render(serializer.data)
     self.redis.set(key, json, ex=ttl)
コード例 #46
0
 def test_render_lazy_strings(self):
     """
     JSONRenderer should deal with lazy translated strings.
     """
     ret = JSONRenderer().render(_('test'))
     self.assertEqual(ret, b'"test"')
コード例 #47
0
from django.utils.six import BytesIO
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser

from status.api.serializers import StatusSerializer
from status.api.serializers import CustomSerializer

from status.models import Status
'''
    Serialize a single objects
'''

obj = Status.objects.first()
serializer = StatusSerializer(obj)
serializer.data
json_data = JSONRenderer().render(serializer.data)
print(json_data)

stream = BytesIO(json_data)
data = JSONParser().parse(stream)
print(data)
'''
    Serialize a queryset
'''

qs = Status.objects.all()
serializer2 = StatusSerializer(qs, many=True)
serializer2.data
json_data2 = JSONRenderer().render(serializer2.data)
print(json_data2)
コード例 #48
0
    def get(self,request):

        # print(dir(request.user.userinfo))
        ser = UserInfoSerializer(request.user.userinfo)
        res = JSONRenderer().render(ser.data)
        return HttpResponse(res)
コード例 #49
0
ファイル: files.py プロジェクト: toccotedd/studio
def file_create(request):
    if request.method != 'POST':
        return HttpResponseBadRequest(
            "Only POST requests are allowed on this endpoint.")

    original_filename, ext = os.path.splitext(request.FILES.values()[0]._name)
    size = request.FILES.values()[0]._size
    contentfile = DjFile(request.FILES.values()[0])
    checksum = get_hash(contentfile)
    request.user.check_space(size, checksum)

    presets = FormatPreset.objects.filter(
        allowed_formats__extension__contains=ext[1:].lower())
    kind = presets.first().kind
    preferences = json.loads(
        request.POST.get('content_defaults', None) or "{}")

    license = License.objects.filter(
        license_name=preferences.get('license')).first(
        )  # Use filter/first in case preference hasn't been set
    license_id = license.pk if license else None
    new_node = ContentNode(
        title=original_filename,
        kind=kind,
        license_id=license_id,
        author=preferences.get('author') or "",
        aggregator=preferences.get('aggregator') or "",
        provider=preferences.get('provider') or "",
        copyright_holder=preferences.get('copyright_holder'),
        parent_id=settings.ORPHANAGE_ROOT_ID,
    )
    if license and license.is_custom:
        new_node.license_description = preferences.get('license_description')
    # The orphanage is not an actual tree but just a long list of items.
    with ContentNode.objects.disable_mptt_updates():
        new_node.save()
    file_object = File(
        file_on_disk=contentfile,
        checksum=checksum,
        file_format_id=ext[1:].lower(),
        original_filename=request.FILES.values()[0]._name,
        contentnode=new_node,
        file_size=size,
        uploaded_by=request.user,
    )
    file_object.save()

    if kind.pk == content_kinds.VIDEO:
        file_object.preset_id = guess_video_preset_by_resolution(
            str(file_object.file_on_disk))
    elif presets.filter(supplementary=False).count() == 1:
        file_object.preset = presets.filter(supplementary=False).first()
    file_object.save()

    thumbnail = None
    try:
        if preferences.get('auto_derive_video_thumbnail') and new_node.kind_id == content_kinds.VIDEO \
                or preferences.get('auto_derive_audio_thumbnail') and new_node.kind_id == content_kinds.AUDIO \
                or preferences.get('auto_derive_html5_thumbnail') and new_node.kind_id == content_kinds.HTML5 \
                or preferences.get('auto_derive_document_thumbnail') and new_node.kind_id == content_kinds.DOCUMENT:
            thumbnail = generate_thumbnail_from_node(new_node, set_node=True)
            request.user.check_space(thumbnail.file_size, thumbnail.checksum)
    except Exception:
        if thumbnail:
            thumbnail.delete()

    return HttpResponse(
        json.dumps({
            "success":
            True,
            "node":
            JSONRenderer().render(ContentNodeEditSerializer(new_node).data)
        }))
コード例 #50
0
ファイル: jinja2.py プロジェクト: isabella232/intake-1
def to_json(data):
    return mark_safe(JSONRenderer().render(data).decode('utf-8'))
コード例 #51
0
ファイル: Client.py プロジェクト: crazytuzi/OnlineJudgeBE
import requests
from . import Token
from . import DRSalgorithm
import json
from rest_framework.renderers import JSONRenderer
from submissions.models import Submissions, SubmissionToken
from problems.models import Problems
DRS = DRSalgorithm.DRSalgorithm()
jr = JSONRenderer()


def http_post(data):
    jdata = json.loads(jr.render(data).decode("utf-8"))
    problem = Problems.objects.get(pk=jdata["problem"])
    if problem.parent_problem is not None:
        jdata["problem"] = problem.parent_problem.id
    submissionToken = SubmissionToken.objects.create(
        submission=Submissions.objects.get(pk=jdata["id"]),
        token=Token.generate_token(Token.key)
    )
    jdata["id"] = submissionToken.id
    jdata["token"] = submissionToken.token
    url = DRS.getUrl()
    req = requests.post(url, json=jdata)
    DRS.update(url, req.elapsed.total_seconds())
コード例 #52
0
def getAllBooks(request):
	books = Book.objects.all()
	data = [BookSerializer(book).data for book in books]
	return HttpResponse(JSONRenderer().render({'books':data}))
コード例 #53
0
def student_list(request):
    stu = Student.objects.all()
    serializer = StudentSerializer(stu, many=True)
    # many =True says that it's a queryset more then one data
    json_data = JSONRenderer().render(serializer.data)
    return HttpResponse(json_data, content_type='application/json')
コード例 #54
0
def getOnePage(request):
	path = request.path.split('/api/pages/')[1]
	page = Link.objects.get(id=path)
	return HttpResponse(JSONRenderer().render({'page':LinkSerializer(page).data}))
コード例 #55
0
 def get(self, request, id):
     res = Goods.objects.filter(id=id).first()
     res = GoodsDetailSerializer(res)
     res2 = JSONRenderer().render(res.data)
     print(res2)
     return HttpResponse(res2)
コード例 #56
0
def getAllPages(request):
	pages = Link.objects.all()
	data = [LinkSerializer(link).data for link in pages]
	return HttpResponse(JSONRenderer().render({'pages':data}))
コード例 #57
0
def SuccessJsonResponse(serialized_dict=None):
    json_obj = {'success': True}
    if serialized_dict:
        json_obj = dict(json_obj, **serialized_dict)
    return HttpResponse(JSONRenderer().render(json_obj),
                        content_type='application/json')
コード例 #58
0
def getOneBook(request):
	path = request.path.split('/api/books/')[1]
	book = Book.objects.get(id=path)
	return HttpResponse(JSONRenderer().render({'book':BookSerializer(book).data}))
コード例 #59
0
 def __init__(self, data, **kwargs):
     content = JSONRenderer().render(data)
     kwargs['content_type'] = 'application/json'
     super(JSONResponse, self).__init__(content, **kwargs)
コード例 #60
0
# Create your views here.
snippet = Snippet(code='foo = "bar"\n')
snippet.save()

snippet = Snippet(code='print("hello, world")\n')
snippet.save()


serializer = SnippetSerializer(snippet)
serializer.data
# {'id': 2, 'title': '', 'code': 'print("hello, world")\n', 'linenos': False, 'language': 'python', 'style': 'friendly'}



content = JSONRenderer().render(serializer.data)
content
# b'{"id": 2, "title": "", "code": "print(\\"hello, world\\")\\n", "linenos": false, "language": "python", "style": "friendly"}'


@csrf_exempt
def snippet_list(request):
    """
    List all code snippets, or create a new snippet.
    """
    if request.method == 'GET':
        snippets = Snippet.objects.all()
        serializer = SnippetSerializer(snippets, many=True)
        return JsonResponse(serializer.data, safe=False)

    elif request.method == 'POST':