Skip to content

Commit 189fb08

Browse files
committed
lend form; misc
1 parent 7433f19 commit 189fb08

File tree

8 files changed

+166
-41
lines changed

8 files changed

+166
-41
lines changed

context_processors.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# from django.conf import settings # import the settings file
2+
import tww
3+
4+
def settings_processor(request):
5+
# d = {}
6+
# for key in dir(settings):
7+
# if not key.startswith('__'):
8+
# d[key] = getattr(settings, key)
9+
# return d
10+
return {'settings':tww.settings.__dict__}

loans/models.py

Lines changed: 72 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
from django.db import models
1111
from django.contrib.auth.models import User
1212

13+
# class MyModel(models.Model):
14+
# def __unicode__(self):
15+
# return unicode(self.id)
16+
1317
class Currency(models.Model):
1418
id = models.AutoField(primary_key=True, db_column='ID')
1519
name = models.CharField(max_length=150, db_column='Name')
@@ -163,13 +167,12 @@ class Meta:
163167
def __unicode__(self):
164168
return self.name
165169

166-
def picture_path(self):
167-
return get_picture_path('Cooperatives', self.id)
168-
picture_path = property(picture_path)
169-
170-
def thumb_path(self):
171-
return get_thumb_path('Cooperatives', self.id)
172-
thumb_path = property(thumb_path)
170+
# def picture_path(self):
171+
# return get_picture_path('Cooperatives', self.id)
172+
# picture_path = property(picture_path)
173+
# def thumb_path(self):
174+
# return get_thumb_path('Cooperatives', self.id)
175+
# thumb_path = property(thumb_path)
173176

174177
class ExchangeRate(models.Model):
175178
id = models.AutoField(primary_key=True, db_column='ID')
@@ -354,15 +357,18 @@ def __unicode__(self):
354357
except Cooperative.DoesNotExist:
355358
return 'Loan ' + unicode(self.id)
356359

357-
def picture_path(self):
358-
paths = get_picture_paths('Loans', self.id) or get_picture_paths('Cooperatives', self.cooperative.id)
359-
return paths['picture']
360-
picture_path = property(picture_path)
360+
# def picture_path(self):
361+
# paths = get_picture_paths('Loans', self.id) or get_picture_paths('Cooperatives', self.cooperative.id)
362+
# return paths['picture']
363+
# picture_path = property(picture_path)
364+
# def thumb_path(self):
365+
# paths = get_picture_paths('Loans', self.id) or get_picture_paths('Cooperatives', self.cooperative.id)
366+
# return paths['thumb']
367+
# thumb_path = property(thumb_path)
361368

362-
def thumb_path(self):
369+
def picture_paths(self):
363370
paths = get_picture_paths('Loans', self.id) or get_picture_paths('Cooperatives', self.cooperative.id)
364-
return paths['thumb']
365-
thumb_path = property(thumb_path)
371+
return paths
366372

367373
def get_description(self, language_code='EN'):
368374
return get_translation('Loans', 'Description', self.id, language_code)
@@ -756,7 +762,7 @@ def __unicode__(self):
756762

757763

758764
class UserAccount(models.Model):
759-
user = models.OneToOneField(User)
765+
user = models.OneToOneField(User, related_name='user_account')
760766
balance = models.DecimalField(max_digits=12, decimal_places=2, default=0)
761767
loans = models.ManyToManyField(Loan, through='UserLoanContribution')
762768

@@ -785,14 +791,23 @@ class UserLoanContribution(models.Model):
785791

786792
import re
787793

788-
def get_picture_paths(table_name, id):
794+
def get_picture_paths(table_name, id, order_by=('priority','media_path')):
789795
try:
790-
image = Media.objects.filter(context_table=table_name, context_id=id).order_by('priority','media_path')[0]
796+
image = Media.objects.filter(context_table=table_name, context_id=id).order_by(*order_by)[0]
791797
except IndexError:
792798
return
793-
# insert ".thumb" into image path
794-
thumb_path = re.sub(r'(\.[^.]+)$', r'.thumb\1', image.media_path)
795-
return {'picture':image.media_path, 'thumb':thumb_path}
799+
# insert various things into file name
800+
thumb = re.sub(r'(\.[^.]+)$', r'.thumb\1', image.media_path)
801+
small = re.sub(r'(\.[^.]+)$', r'.small\1', image.media_path)
802+
medium = re.sub(r'(\.[^.]+)$', r'.medium\1', image.media_path)
803+
large = re.sub(r'(\.[^.]+)$', r'.large\1', image.media_path)
804+
return {
805+
'full':image.media_path,
806+
'thumb':thumb,
807+
'small':small,
808+
'medium':medium,
809+
'large':large,
810+
}
796811

797812
# def get_thumb_path(table_name, id):
798813
# images = Media.objects.filter(context_table=table_name, context_id=id).order_by('-priority','media_path')
@@ -802,15 +817,47 @@ def get_picture_paths(table_name, id):
802817
# return thumb_path
803818

804819
def get_translation(table_name, column_name, id, language_code='EN'):
805-
translations = Translation.objects.filter(remote_table=table_name, remote_id=id, remote_column_name=column_name)
820+
translations = Translation.objects.filter(remote_table=table_name, remote_column_name=column_name, remote_id=id)
806821
try:
807822
content = translations.get(language__code=language_code).translated_content
808823
except Translation.DoesNotExist:
809824
content = translations.order_by('language__priority')[0].translated_content
810825
return content
811826

812827
def first_or_none(some_list):
813-
try:
814-
some_list[0]
815-
except IndexError:
816-
pass
828+
try: some_list[0]
829+
except IndexError: pass
830+
831+
832+
## Forms ##
833+
834+
from django import forms
835+
836+
class LendForm(forms.Form):
837+
AMOUNT_CHOICES = (
838+
(25, '$25'),
839+
(50, '$50'),
840+
(100, '$100'),
841+
(0, 'Other amount'),
842+
)
843+
amount = forms.DecimalField(widget=forms.Select(
844+
choices=AMOUNT_CHOICES,
845+
attrs={'onchange':'show_hide_other()'}))
846+
# amount = forms.TypedChoiceField(choices=AMOUNT_CHOICES, coerce='decimal')
847+
other_amount = forms.DecimalField(max_digits=12, decimal_places=2, required=False)
848+
849+
def clean(self):
850+
cleaned_data = super(LendForm, self).clean()
851+
amount = cleaned_data.get('amount')
852+
other_amount = cleaned_data.get('other_amount')
853+
854+
if amount == 0 and not other_amount:
855+
# raise forms.ValidationError('Please enter an amount in the "Other amount" field.')
856+
if not 'other_amount' in self._errors:
857+
self._errors['other_amount'] = self.error_class([u'Please enter an amount in the "Other amount" field.'])
858+
if 'other_amount' in cleaned_data:
859+
del cleaned_data['other_amount']
860+
861+
# Always return the full collection of cleaned data.
862+
return cleaned_data
863+

loans/views.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import re, locale
2-
from django.shortcuts import render_to_response, get_object_or_404, redirect
3-
from django.http import Http404
2+
from django.shortcuts import render_to_response, get_object_or_404, redirect, render
3+
from django.http import Http404, HttpResponseRedirect
44
from django.db import connection
55
# from django.db.models import Q
66
from django.contrib.auth import logout
7+
from django.contrib.auth.views import login
78
from django.contrib.auth.decorators import login_required
89
from loans.models import *
910

@@ -30,13 +31,9 @@ def loan_detail(request, loan_id):
3031
l = get_object_or_404(Loan, pk=loan_id)
3132
l.short_description = l.get_short_description(language_code='EN')
3233
l.description = l.get_description(language_code='EN')
34+
l.picture_path = l.picture_paths()['large']
3335
return render_to_response('loans/loan_detail.html', {'loan': l})
3436

35-
@login_required
36-
def lend_form(request, loan_id):
37-
l = get_object_or_404(Loan, pk=loan_id)
38-
return render_to_response('loans/lend_form.html', {'loan': l})
39-
4037
@login_required
4138
def user_profile(request):
4239
user_account = UserAccount.objects.get_or_create(user=request.user)[0]
@@ -45,4 +42,37 @@ def user_profile(request):
4542
def logout_view(request):
4643
logout(request)
4744
return redirect('/accounts/login?message=loggedOut')
45+
# return redirect('login_view', message="You have been logged out.")
4846
# return render_to_response('registration/login.html', {'message': "You have been logged out."})
47+
48+
@login_required
49+
def lend_form(request, loan_id):
50+
l = get_object_or_404(Loan, pk=loan_id)
51+
l.picture_path = l.picture_paths()['medium']
52+
if request.method == 'POST': # If the form has been submitted...
53+
form = LendForm(request.POST)
54+
if form.is_valid(): # All validation rules pass
55+
amount = form.cleaned_data['amount']
56+
other_amount = form.cleaned_data['other_amount']
57+
if amount == 0:
58+
amount = other_amount
59+
u = UserAccount.objects.get(user=request.user)
60+
c = UserLoanContribution(
61+
user = u,
62+
loan = l,
63+
amount = amount,
64+
balance = amount,
65+
)
66+
c.save()
67+
u.balance -= amount
68+
u.save()
69+
return redirect('loans.views.user_profile') # Redirect after POST
70+
# else:
71+
# if 'other_amount' not in form.errors:
72+
# form.errors['other_amount'] = form.errors['__all__']
73+
else:
74+
form = LendForm()
75+
76+
return render(request, 'loans/lend_form.html', {'form': form, 'loan': l})
77+
# return render_to_response('loans/lend_form.html', {'loan': l})
78+

templates/registration/login.html renamed to templates/accounts/login.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@
77
<p>Your username and password didn't match. Please try again.</p>
88
{% endif %}
99

10+
{# {% if message %} #}
11+
{# <p><b>{{ message }}</b></p> #}
12+
{# {% endif %} #}
13+
1014
{% if request.GET.message == 'loggedOut' %}
1115
<p><b>You have been logged out.</b></p>
1216
{% endif %}
1317

1418
<form method="post" action="{% url 'django.contrib.auth.views.login' %}">
1519
{% csrf_token %}
1620
<table>
21+
{# {{ form.as_table }} #}
1722
<tr>
1823
<td>{{ form.username.label_tag }}</td>
1924
<td>{{ form.username }}</td>

templates/accounts/profile.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ <h1>{{ user.first_name }} {{ user.last_name }} ({{ user.username }})</h1>
1010

1111
<p>Balance: ${{ account.balance }}</p>
1212

13-
<p><a href="/accounts/logout">Log out</a></p>
13+
<p><a href="{% url loans.views.logout_view %}">Log out</a></p>
1414

1515
{% endblock content %}

templates/base.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
<head>
44
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
55
<title>{% block title %}The Working World{% endblock %}</title>
6+
<script src="{{ settings.PATHS.jquery }}"></script>
7+
{% block head %}{% endblock %}
68
</head>
79
<body>
810
{% block content %}{% endblock %}

templates/loans/lend_form.html

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@
22

33
{% block title %}{{ loan }}: Lend{% endblock %}
44

5+
{% block head %}
6+
<script>
7+
function show_hide_other(speed) {
8+
if (!speed) speed = 'fast'
9+
amount = $('#id_amount').val()
10+
if (amount == "0")
11+
$('#div_other_amount').show(speed)
12+
else $('#div_other_amount').hide(speed)
13+
}
14+
$(document).ready(function() {
15+
show_hide_other('')
16+
})
17+
</script>
18+
{% endblock %}
19+
520
{% block content %}
621

722
<h1>{{ loan }}: Lend</h1>
@@ -16,12 +31,28 @@ <h1>{{ loan }}: Lend</h1>
1631
<div class="short_description">
1732
{{ loan.short_description|default:'' }}
1833
</div>
19-
<div class="description">
20-
<h3>Details</h3>
21-
{{ loan.description|default:'<span style="font-style:italic;" class="notfound">No description found.</span>' }}
22-
</div>
23-
<div class="lend">
24-
<a class="button" href="{% url loans.views.lend_form loan.id %}">Lend</a>
34+
35+
<div class="lend_form">
36+
{# <ul class="errorlist"> #}
37+
{# {% for key, value in form.errors %} #}
38+
{# <li>{{key}}: {{ value }}</li> #}
39+
{# {% endfor %} #}
40+
{# </ul> #}
41+
{# {% if form.form_errors %}<p class="error_message"><strong>{{ form.form_errors }}</strong></p>{% endif %} #}
42+
<form method="post" action="{% url loans.views.lend_form loan.id %}">
43+
{% csrf_token %}
44+
{{ form.amount.errors }}
45+
{{ form.other_amount.errors }}
46+
<div id="div_amount">
47+
{{ form.amount.label_tag }}:
48+
{{ form.amount }}
49+
</div>
50+
<div id="div_other_amount">
51+
{{ form.other_amount.label_tag }}:
52+
{{ form.other_amount }}
53+
</div>
54+
<input type="submit" value="Lend">
55+
</form>
2556
</div>
2657

2758
{% endblock %}

urls.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
(r'^admin/', include(admin.site.urls)),
1515

1616
(r'^$', 'home'),
17-
(r'^accounts/login/$', 'django.contrib.auth.views.login'),
17+
(r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name':'accounts/login.html'}),
1818
# (r'^accounts/logout/$', 'django.contrib.auth.views.logout_then_login'),
1919
(r'^accounts/logout/$', 'loans.views.logout_view'),
2020
)
@@ -34,4 +34,4 @@
3434
(r'^loans/(?P<loan_id>\d+)/$', 'loan_detail'),
3535
(r'^loans/(?P<loan_id>\d+)/lend$', 'lend_form'),
3636
(r'^accounts/profile/$', 'user_profile')
37-
)
37+
)

0 commit comments

Comments
 (0)