Added saving of cards to Trello.

This commit is contained in:
Robert Kolner committed 2013-04-25 01:43:08 +02:00
1 parent 535e83e31b
commit 3e70b15809
5 files changed
+68 -11

No files matched your search

+25 -2
View File
@@ -3,6 +3,8 @@ from django.http import HttpResponse, Http404
from django.template import Context, Template
from django.template.loader import get_template
from django.views.generic import TemplateView
from settings import TRELLO_API_KEY, TRELLO_TOKEN
import trello
class BaseFormView(TemplateView):
template_name = None
@@ -30,11 +32,32 @@ class BaseFormView(TemplateView):
response = template.render(Context(context_data))
return HttpResponse(response)
self._save_data(params)
return HttpResponse('<html><head><title>Suksess!</title></head><body><p>Bestillingen har blitt sendt.</p></body></html>')
order = self._save_data(params)
return HttpResponse('<html><head><title>Suksess!</title></head><body><p>Bestillingen har blitt sendt.<br/>card_id = {}</p></body></html>'.format(order.id))
def _get_errors(self, params):
return {}
def _save_data(self, params):
raise NotImplementedError
def _save_to_trello(self, card_name, card_description, card_due, card_colour="green", board_id = "51784f4b1dd3f4c53e006fb4", list_name='Bestillinger'):
client = trello.TrelloClient(api_key=TRELLO_API_KEY, token=TRELLO_TOKEN)
board = client.get_board(board_id)
all_lists = board.all_lists()
order_list = filter(lambda x: x.name == list_name and (x.fetch() or x.closed == False), all_lists)
if len(order_list) == 0:
order_list.append(board.add_list(list_name))
order_list = order_list[0]
card = order_list.add_card(card_name, card_description)
# Set card deadline
card._set_remote_attribute('due', card_due)
# Set colour of the card. We have to do this like this, because there is no shortcut and _set_remote_attribute() uses PUT
card.client.fetch_json('/cards/'+card.id+'/labels', http_method="POST", post_args = { 'value' : card_colour })
return card.id
+17 -8
View File
@@ -1,8 +1,12 @@
# Django settings for bestilling project.
# Django settings for bestilling project. Fill out variables marked with TODO to make it work.
import os
DEBUG = True
TEMPLATE_DEBUG = DEBUG
# Path of our project:
PROJECT_PATH = os.path.realpath(os.path.join(os.path.dirname(__file__), os.pardir))
ADMINS = (
# ('Your Name', 'your_email@example.com'),
)
@@ -11,16 +15,20 @@ MANAGERS = ADMINS
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'ENGINE': 'django.db.backends.', # TODO Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'bestillingsskjema', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'kak',
'PASSWORD': 'kak',
'USER': '',
'PASSWORD': '',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
TRELLO_API_KEY = "" #TODO
TRELLO_TOKEN = "" #TODO
# Hosts/domain names that are valid for this site; required if DEBUG is False
# See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts
ALLOWED_HOSTS = []
@@ -72,6 +80,7 @@ STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
PROJECT_PATH + '/static/',
)
# List of finder classes that know how to find static files in
@@ -111,6 +120,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.
PROJECT_PATH + '/templates/',
)
INSTALLED_APPS = (
@@ -120,10 +130,9 @@ INSTALLED_APPS = (
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
# 'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'django.contrib.admin',
'django.contrib.admindocs',
'design'
)
# A sample logging configuration. The only tangible logging
+1
View File
@@ -12,3 +12,4 @@ class DesignOrder(models.Model):
marger = models.CharField(max_length=50)
content = models.TextField(blank=True, null=True)
created = models.DateTimeField(auto_now_add=True)
trello_card_id = models.CharField(max_length=40)
+24 -1
View File
@@ -1,3 +1,5 @@
#-*- encoding: utf-8 -*-
from datetime import datetime
from bestilling.baseform import BaseFormView
@@ -44,9 +46,28 @@ class DesignForm(BaseFormView):
return dict((error, True) for error, has_happened in errors.iteritems() if has_happened is True)
def _save_data(self, params):
description = u"""\
Forening: {client}
Navn: {contact_name}
Epost: {contact_email}
Tlf: {contact_number}
Format: {format}
Papirstr.: {paper_size}
Farger: {colour}
Marger: {marger}
Beskrivelse: {content}""".format(**params)
deadline = datetime.strptime(params['deadline'], '%Y-%m-%d')
card_id = self._save_to_trello(
# board_id = "",
card_name = params['client'],
card_description = description,
card_due=datetime.strftime(deadline, '%m/%d/%y'),
)
order = DesignOrder(
client = params['client'],
deadline = datetime.strptime(params['deadline'], '%Y-%m-%d').date(),
deadline = deadline.date(),
contact_name = params['contact_name'],
contact_email = params['contact_email'],
contact_number = params['contact_number'],
@@ -55,5 +76,7 @@ class DesignForm(BaseFormView):
colour = params['colour'],
marger = params['marger'],
content = params['content'],
trello_card_id = str(card_id)
)
order.save()
return order
+1
View File
@@ -6,4 +6,5 @@ distribute==0.6.34
mimeparse==0.1.3
psycopg2==2.4.5
python-dateutil==1.5
py-trello==0.1.3
wsgiref==0.1.2