Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
216baa55
Commit
216baa55
authored
Jan 27, 2011
by
tom christie tom@tomchristie.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Login/Logout and FlyWheel API link in HTML emitter
parent
e227c38b
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
49 additions
and
13 deletions
+49
-13
examples/settings.py
+4
-0
examples/urls.py
+2
-0
flywheel/emitters.py
+13
-2
flywheel/resource.py
+0
-2
flywheel/templates/emitter.html
+20
-9
flywheel/utils.py
+10
-0
No files found.
examples/settings.py
View file @
216baa55
# Django settings for src project.
import
os
BASE_DIR
=
os
.
path
.
dirname
(
__file__
)
DEBUG
=
True
TEMPLATE_DEBUG
=
DEBUG
...
...
@@ -81,6 +84,7 @@ TEMPLATE_DIRS = (
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os
.
path
.
join
(
BASE_DIR
,
'templates'
)
)
INSTALLED_APPS
=
(
...
...
examples/urls.py
View file @
216baa55
...
...
@@ -7,6 +7,8 @@ urlpatterns = patterns('',
(
r'pygments-example/'
,
include
(
'pygments_api.urls'
)),
(
r'^blog-post-example/'
,
include
(
'blogpost.urls'
)),
(
r'^object-store-example/'
,
include
(
'objectstore.urls'
)),
(
r'^accounts/login/$'
,
'django.contrib.auth.views.login'
),
(
r'^accounts/logout/$'
,
'django.contrib.auth.views.logout'
),
(
r'^admin/doc/'
,
include
(
'django.contrib.admindocs.urls'
)),
(
r'^admin/'
,
include
(
admin
.
site
.
urls
)),
)
flywheel/emitters.py
View file @
216baa55
from
django.conf
import
settings
from
django.template
import
RequestContext
,
loader
from
django
import
forms
from
flywheel.response
import
NoContent
from
utils
import
dict2xml
from
utils
import
dict2xml
,
url_resolves
import
string
try
:
import
json
...
...
@@ -12,6 +13,7 @@ except ImportError:
class
BaseEmitter
(
object
):
media_type
=
None
...
...
@@ -118,13 +120,22 @@ class DocumentingTemplateEmitter(BaseEmitter):
content
=
self
.
_get_content
(
self
.
resource
,
output
)
form_instance
=
self
.
_get_form_instance
(
self
.
resource
)
if
url_resolves
(
settings
.
LOGIN_URL
)
and
url_resolves
(
settings
.
LOGOUT_URL
):
login_url
=
"
%
s?next=
%
s"
%
(
settings
.
LOGIN_URL
,
self
.
resource
.
request
.
path
)
logout_url
=
"
%
s?next=
%
s"
%
(
settings
.
LOGOUT_URL
,
self
.
resource
.
request
.
path
)
else
:
login_url
=
None
logout_url
=
None
template
=
loader
.
get_template
(
self
.
template
)
context
=
RequestContext
(
self
.
resource
.
request
,
{
'content'
:
content
,
'resource'
:
self
.
resource
,
'request'
:
self
.
resource
.
request
,
'response'
:
self
.
resource
.
response
,
'form'
:
form_instance
'form'
:
form_instance
,
'login_url'
:
login_url
,
'logout_url'
:
logout_url
,
})
ret
=
template
.
render
(
context
)
...
...
flywheel/resource.py
View file @
216baa55
...
...
@@ -7,9 +7,7 @@ from flywheel.response import status, Response, ResponseException
from
decimal
import
Decimal
import
re
from
itertools
import
chain
# TODO: Display user login in top panel: http://stackoverflow.com/questions/806835/django-redirect-to-previous-page-after-login
# TODO: Figure how out references and named urls need to work nicely
# TODO: POST on existing 404 URL, PUT on existing 404 URL
#
...
...
flywheel/templates/emitter.html
View file @
216baa55
...
...
@@ -5,6 +5,11 @@
<head>
<style>
pre
{
border
:
1px
solid
black
;
padding
:
1em
;
background
:
#ffd
}
body
{
margin
:
0
;
border
:
0
;
padding
:
0
;}
span
.api
{
margin
:
0.5em
1em
}
span
.auth
{
float
:
right
;
margin-right
:
1em
}
div
.header
{
margin
:
0
;
border
:
0
;
padding
:
0.25em
0
;
background
:
#ddf
}
div
.content
{
margin
:
0
1em
;}
div
.action
{
border
:
1px
solid
black
;
padding
:
0.5em
1em
;
margin-bottom
:
0.5em
;
background
:
#ddf
}
ul
.accepttypes
{
float
:
right
;
list-style-type
:
none
;
margin
:
0
;
padding
:
0
}
ul
.accepttypes
li
{
display
:
inline
;}
...
...
@@ -17,14 +22,19 @@
<title>
API - {{ resource.name }}
</title>
</head>
<body>
<div
class=
'header'
>
<span
class=
'api'
><a
href=
'http://www.thewebhaswon.com/flywheel/'
>
FlyWheel API
</a></span>
<span
class=
'auth'
>
{% if user.is_active %}Welcome, {{ user }}.{% if logout_url %}
<a
href=
'{{ logout_url }}'
>
Log out
</a>
{% endif %}{% else %}Not logged in {% if login_url %}
<a
href=
'{{ login_url }}'
>
Log in
</a>
{% endif %}{% endif %}
</span>
</div>
<div
class=
'content'
>
<h1>
{{ resource.name }}
</h1>
<p>
{{ resource.description|linebreaksbr }}
</p>
<pre><b>
{{ response.status }} {{ response.status_text }}
</b>
{% autoescape off %}
{% for key, val in response.headers.items %}
<b>
{{ key }}:
</b>
{{ val|urlize_quoted_links }}
{% endfor %}
{{ content|urlize_quoted_links }}
</pre>
{% endautoescape %}
{% for key, val in response.headers.items %}
<b>
{{ key }}:
</b>
{{ val|urlize_quoted_links }}
{% endfor %}
{{ content|urlize_quoted_links }}
</pre>
{% endautoescape %}
{% if 'GET' in resource.allowed_methods %}
{% if 'GET' in resource.allowed_methods %}
<div
class=
'action'
>
<a
href=
'{{ request.path }}'
>
GET
</a>
<ul
class=
"accepttypes"
>
...
...
@@ -36,14 +46,14 @@
</ul>
<div
class=
"clearing"
></div>
</div>
{% endif %}
{% endif %}
{% comment %} *** Only display the POST/PUT/DELETE forms if we have a bound form, and if method ***
{% comment %} *** Only display the POST/PUT/DELETE forms if we have a bound form, and if method ***
*** tunneling via POST forms is enabled. ***
*** (We could display only the POST form if method tunneling is disabled, but I think ***
*** the user experience would be confusing, so we simply turn all forms off. *** {% endcomment %}
{% if resource.METHOD_PARAM and form %}
{% if resource.METHOD_PARAM and form %}
{% if 'POST' in resource.allowed_methods %}
<div
class=
'action'
>
<form
action=
"{{ request.path }}"
method=
"post"
>
...
...
@@ -92,7 +102,7 @@
</form>
</div>
{% endif %}
{% endif %}
{% endif %}
</div>
</body>
</html>
\ No newline at end of file
flywheel/utils.py
View file @
216baa55
...
...
@@ -2,11 +2,21 @@ import re
import
xml.etree.ElementTree
as
ET
from
django.utils.encoding
import
smart_unicode
from
django.utils.xmlutils
import
SimplerXMLGenerator
from
django.core.urlresolvers
import
resolve
try
:
import
cStringIO
as
StringIO
except
ImportError
:
import
StringIO
def
url_resolves
(
url
):
"""Return True if the given URL is mapped to a view in the urlconf, False otherwise."""
try
:
resolve
(
url
)
except
:
return
False
return
True
# From piston
def
coerce_put_post
(
request
):
"""
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment