Skip to content

Commit 0eda8aa

Browse files
authored
Various fixes to bring the example app up to speed (#55)
* various fixes to bring the example app up to speed * black format, changelog update * review feedback
1 parent 236dcd9 commit 0eda8aa

File tree

7 files changed

+40
-15
lines changed

7 files changed

+40
-15
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic
66
Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
# [0.22.3] - 2021-05-18
9+
### Fixed
10+
- [PR 55](https://github.com/salesforce/django-declarative-apis/pull/55) Backwards compatibility fix for field expansion headers, update example app
11+
812
# [0.22.2] - 2020-08-11
913
### Fixed
1014
- [PR](https://github.com/salesforce/django-declarative-apis/pull/48) Apply filters to dict values

django_declarative_apis/machinery/__init__.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,14 @@ def get_response(self):
183183
else:
184184
raise HttpStatusCode(data)
185185
else:
186+
try:
187+
x_expand = self.bound_endpoint.request.META.get("HTTP_X_EXPAND")
188+
except AttributeError:
189+
x_expand = ""
190+
186191
return (
187192
status_code,
188-
apply_filters_to_object(
189-
data,
190-
filter_def,
191-
self.bound_endpoint.request.META.get("HTTP_X_EXPAND"),
192-
),
193+
apply_filters_to_object(data, filter_def, x_expand),
193194
)
194195

195196
def __init__(self, endpoint_definition):

example/example/settings.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@
126126
DECLARATIVE_ENDPOINT_RESOURCE_ADAPTER = (
127127
"django_declarative_apis.adapters.EndpointResource"
128128
)
129-
DECLARATIVE_ENDPOINT_AUTHENTICATION_HANDLERS = (
130-
"django_declarative_apis.authentication.oauthlib.oauth1.TwoLeggedOauth1",
131-
)
129+
DECLARATIVE_ENDPOINT_AUTHENTICATION_HANDLERS = [
130+
(
131+
(
132+
None,
133+
"django_declarative_apis.authentication.oauthlib.oauth1.TwoLeggedOauth1Hint",
134+
),
135+
"django_declarative_apis.authentication.oauthlib.oauth1.TwoLeggedOauth1",
136+
)
137+
]

example/manage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
#!/usr/bin/env python
12
#
23
# Copyright (c) 2019, salesforce.com, inc.
34
# All rights reserved.
45
# SPDX-License-Identifier: BSD-3-Clause
56
# For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
67
#
78

8-
#!/usr/bin/env python
99
import os
1010
import sys
1111

example/myapp/models.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@
1010

1111

1212
class User(models.Model):
13-
consumer = models.ForeignKey(django_declarative_apis.models.OauthConsumer)
13+
consumer = models.ForeignKey(
14+
django_declarative_apis.models.OauthConsumer, on_delete=models.CASCADE
15+
)
1416
name = models.CharField(max_length=50, null=False, blank=False)

example/myapp/resources.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
# For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
#
77

8+
import json
9+
10+
import django.http
11+
812
from django_declarative_apis import machinery, models
913
from django_declarative_apis.machinery import filtering
1014

@@ -65,6 +69,10 @@ class PingDefinition(machinery.BaseEndpointDefinition):
6569
"""A basic "ping" endpoint
6670
"""
6771

72+
# filter definition for the resource. values will be masked by default (i.e. without this, the user would get
73+
# '{"ping": null}' in the response body.
74+
response_filter = {str: filtering.ALWAYS}
75+
6876
def is_authorized(self):
6977
"""User should always be authorized
7078
"""
@@ -73,8 +81,5 @@ def is_authorized(self):
7381
@property
7482
def resource(self):
7583
"""The endpoint resource
76-
77-
Endpoint resources can be implemented as either properties or machinery.endpoint_resources.
78-
The latter will require filters to be defined.
7984
"""
8085
return {"ping": "pong"}

example/myapp/urls.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
77

88
from django.conf.urls import url
99

10+
from django_declarative_apis import authentication
1011
from django_declarative_apis.adapters import resource_adapter
1112

1213
from myapp import resources
1314

1415

15-
class NoAuth:
16+
class NoAuth(authentication.Authenticator):
1617
"""A custom NoAuth class to treat all requests as authenticated
1718
1819
By default, django-declarative-apis requires authentication. This allows us to get around that.
@@ -22,13 +23,19 @@ class NoAuth:
2223
def is_authenticated(request):
2324
return True
2425

26+
def challenge(self, error):
27+
super().challenge(error)
28+
2529

2630
urlpatterns = [
2731
url(
2832
r"^me$",
2933
resource_adapter(get=resources.MeDefinition, post=resources.MeUpdateDefinition),
3034
),
3135
url(
32-
r"^ping$", resource_adapter(get=resources.PingDefinition, authentication=NoAuth)
36+
r"^ping$",
37+
resource_adapter(
38+
get=resources.PingDefinition, authentication={None: (NoAuth(),)}
39+
),
3340
),
3441
]

0 commit comments

Comments
 (0)