10
10
from django .db import models
11
11
from django .contrib .auth .models import User
12
12
13
+ # class MyModel(models.Model):
14
+ # def __unicode__(self):
15
+ # return unicode(self.id)
16
+
13
17
class Currency (models .Model ):
14
18
id = models .AutoField (primary_key = True , db_column = 'ID' )
15
19
name = models .CharField (max_length = 150 , db_column = 'Name' )
@@ -163,13 +167,12 @@ class Meta:
163
167
def __unicode__ (self ):
164
168
return self .name
165
169
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)
173
176
174
177
class ExchangeRate (models .Model ):
175
178
id = models .AutoField (primary_key = True , db_column = 'ID' )
@@ -354,15 +357,18 @@ def __unicode__(self):
354
357
except Cooperative .DoesNotExist :
355
358
return 'Loan ' + unicode (self .id )
356
359
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)
361
368
362
- def thumb_path (self ):
369
+ def picture_paths (self ):
363
370
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
366
372
367
373
def get_description (self , language_code = 'EN' ):
368
374
return get_translation ('Loans' , 'Description' , self .id , language_code )
@@ -756,7 +762,7 @@ def __unicode__(self):
756
762
757
763
758
764
class UserAccount (models .Model ):
759
- user = models .OneToOneField (User )
765
+ user = models .OneToOneField (User , related_name = 'user_account' )
760
766
balance = models .DecimalField (max_digits = 12 , decimal_places = 2 , default = 0 )
761
767
loans = models .ManyToManyField (Loan , through = 'UserLoanContribution' )
762
768
@@ -785,14 +791,23 @@ class UserLoanContribution(models.Model):
785
791
786
792
import re
787
793
788
- def get_picture_paths (table_name , id ):
794
+ def get_picture_paths (table_name , id , order_by = ( 'priority' , 'media_path' ) ):
789
795
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 ]
791
797
except IndexError :
792
798
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
+ }
796
811
797
812
# def get_thumb_path(table_name, id):
798
813
# 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):
802
817
# return thumb_path
803
818
804
819
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 )
806
821
try :
807
822
content = translations .get (language__code = language_code ).translated_content
808
823
except Translation .DoesNotExist :
809
824
content = translations .order_by ('language__priority' )[0 ].translated_content
810
825
return content
811
826
812
827
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
+
0 commit comments