Skip to content

Commit

Permalink
Merge branch 'master' of git.tsar.su:grw/django-sheets
Browse files Browse the repository at this point in the history
Conflicts:
	README.rst
  • Loading branch information
george.whewell committed Jan 19, 2015
2 parents 79ad331 + 2798bc1 commit cf2b4f5
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 20 deletions.
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ django-sheets
:target: https://requires.io/github/georgewhewell/django-sheets/requirements/?branch=master
:alt: Requirements Status


Use Google Sheets as context variables in Django templates

Documentation
Expand Down
13 changes: 8 additions & 5 deletions docs/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
Installation
============

At the command line::
To install the latest release from PyPI using pip::

$ easy_install django-sheets
$ pip install django-sheets

Or, if you have virtualenvwrapper installed::
Add ``sheets`` to ``INSTALLED_APPS`` in settings.py::

$ mkvirtualenv django-sheets
$ pip install django-sheets
INSTALLED_APPS = (
...
'sheets',
...
)
34 changes: 32 additions & 2 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,36 @@
Usage
========

To use django-sheets in a project::
To use django-sheets, you need to have a key to a publically-accessible
Google Sheets spreadsheet. To find it, open your sheet and select
File -> Publish to the web.

import django-sheets
Press Share, and select the link. You'll find the 44-character key as
part of the link. For example, the sample link:

https://docs.google.com/spreadsheets/d/**1bJNR7SLqpzWJNvstNcFR4gtS-M7Bmn0D1X2lGTJPvGM**/pubhtml

Load the django-sheets template tags in your template::

{% load sheets %}

Assign the CSV data to a variable using the ``{% csv %}`` tag::

{% csv <key> as <variable_name> %}

Try it using the sample key above::

{% load sheets %}
{% csv "1bJNR7SLqpzWJNvstNcFR4gtS-M7Bmn0D1X2lGTJPvGM" as uk500 %}
<table>
{% for row in uk500 %}
<tr>
{% for cell in row %}
<td>{{ cell }}</td>
{% endfor %}
</td>
{% endfor %}

View the output, you should see::

bleh
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
],
)
11 changes: 8 additions & 3 deletions sheets/templatetags/sheets.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import unicode_literals
from django import template

import csv
Expand All @@ -21,8 +22,12 @@ def get_sheet(key):


def read_csv(csv_content):
reader = csv.reader(csv_content.text)
return [row for row in reader]
return csv.reader(
csv_content.splitlines(),
delimiter=str(','),
quotechar=str('"'),
quoting=csv.QUOTE_MINIMAL,
)


@register.assignment_tag(name='csv')
Expand All @@ -35,4 +40,4 @@ def csv_tag(key):
if response_data is None:
return None

return read_csv(response_data)
return read_csv(response_data.text)
1 change: 1 addition & 0 deletions tests/sample_output.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<table><tr><td>first_name</td><td>last_name</td><td>company_name</td><td>address</td><td>city</td><td>county</td><td>postal</td><td>phone1</td><td>phone2</td><td>email</td><td>web</td></tr><tr><td>Aleshia</td><td>Tomkiewicz</td><td>Alan D Rosenburg Cpa Pc</td><td>14 Taylor St</td><td>St. Stephens Ward</td><td>Kent</td><td>CT2 7PP</td><td>01835-703597</td><td>01944-369967</td><td>atomkiewicz@hotmail.com</td><td>http://www.alandrosenburgcpapc.co.uk</td></tr><tr><td>Evan</td><td>Zigomalas</td><td>Cap Gemini America</td><td>5 Binney St</td><td>Abbey Ward</td><td>Buckinghamshire</td><td>HP11 2AX</td><td>01937-864715</td><td>01714-737668</td><td>evan.zigomalas@gmail.com</td><td>http://www.capgeminiamerica.co.uk</td></tr><tr><td>France</td><td>Andrade</td><td>Elliott, John W Esq</td><td>8 Moor Place</td><td>East Southbourne and Tuckton W</td><td>Bournemouth</td><td>BH6 3BE</td><td>01347-368222</td><td>01935-821636</td><td>france.andrade@hotmail.com</td><td>http://www.elliottjohnwesq.co.uk</td></tr><tr><td>Ulysses</td><td>Mcwalters</td><td>Mcmahan, Ben L</td><td>505 Exeter Rd</td><td>Hawerby cum Beesby</td><td>Lincolnshire</td><td>DN36 5RP</td><td>01912-771311</td><td>01302-601380</td><td>ulysses@hotmail.com</td><td>http://www.mcmahanbenl.co.uk</td></tr><tr><td>Tyisha</td><td>Veness</td><td>Champagne Room</td><td>5396 Forth Street</td><td>Greets Green and Lyng Ward</td><td>West Midlands</td><td>B70 9DT</td><td>01547-429341</td><td>01290-367248</td><td>tyisha.veness@hotmail.com</td><td>http://www.champagneroom.co.uk</td></tr><tr><td>Eric</td><td>Rampy</td><td>Thompson, Michael C Esq</td><td>9472 Lind St</td><td>Desborough</td><td>Northamptonshire</td><td>NN14 2GH</td><td>01969-886290</td><td>01545-817375</td><td>erampy@rampy.co.uk</td><td>http://www.thompsonmichaelcesq.co.uk</td></tr><tr><td>Marg</td><td>Grasmick</td><td>Wrangle Hill Auto Auct &amp; Slvg</td><td>7457 Cowl St #70</td><td>Bargate Ward</td><td>Southampton</td><td>SO14 3TY</td><td>01865-582516</td><td>01362-620532</td><td>marg@hotmail.com</td><td>http://www.wranglehillautoauctslvg.co.uk</td></tr><tr><td>Laquita</td><td>Hisaw</td><td>In Communications Inc</td><td>20 Gloucester Pl #96</td><td>Chirton Ward</td><td>Tyne &amp; Wear</td><td>NE29 7AD</td><td>01746-394243</td><td>01590-982428</td><td>laquita@yahoo.com</td><td>http://www.incommunicationsinc.co.uk</td></tr><tr><td>Lura</td><td>Manzella</td><td>Bizerba Usa Inc</td><td>929 Augustine St</td><td>Staple Hill Ward</td><td>South Gloucestershire</td><td>BS16 4LL</td><td>01907-538509</td><td>01340-713951</td><td>lura@hotmail.com</td><td>http://www.bizerbausainc.co.uk</td></tr></table>
1 change: 0 additions & 1 deletion tests/sample_output.txt

This file was deleted.

29 changes: 20 additions & 9 deletions tests/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,25 @@
from django import template
from django.test import SimpleTestCase

sample_template = \
"""{% spaceless %}
{% load sheets %}
{% csv key as uk500 %}
<table>
{% for row in uk500 %}
<tr>
{% for cell in row %}
<td>{{ cell }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
{% endspaceless %}"""
sample_key = '1bJNR7SLqpzWJNvstNcFR4gtS-M7Bmn0D1X2lGTJPvGM'
sample_template = (
"{% load sheets %}{% csv key as csvrows %}"
"{% for row in csvrows %}{% for cell in row %}"
"{{ cell }}{% endfor %}{% endfor %}")

sample_response = os.path.join(
os.path.dirname(__file__), 'sample_response.csv')
sample_output = os.path.join(
os.path.dirname(__file__), 'sample_output.txt')
os.path.dirname(__file__), 'sample_output.html')

gdocs_format = \
'https://docs.google.com/spreadsheets/d/{key}/export?format=csv&id={key}'
Expand All @@ -50,19 +59,21 @@ def test_404(self):
body='404 Not Found', content_type='html/text', status=404,
match_querystring=True)
t = template.Template(sample_template)
self.assertEqual(t.render(template.Context({'key': 'test'})), '')
self.assertEqual(
t.render(template.Context({'key': 'test'})),
'<table></table>')
self.assertEqual(1, len(responses.calls))

@responses.activate
def test_sample(self):
def test_sample_mocked(self):
responses.add(
responses.GET, gdocs_format.format(key=sample_key),
body=open(sample_response, 'rt', encoding='utf-8').read(),
match_querystring=True, status=200)
t = template.Template(sample_template)
output = t.render(template.Context({'key': sample_key}))
self.assertEqual(1, len(responses.calls))
self.assertEqual(
responses.calls[0].request.url,
gdocs_format.format(key=sample_key))
self.assertEqual(1, len(responses.calls))
self.assertEqual(output, open(sample_output).read())

0 comments on commit cf2b4f5

Please sign in to comment.