2016-08-15 20:12:27 +00:00
#external library imports
2013-06-03 16:31:39 +00:00
import logging
2014-10-13 23:21:30 +00:00
import re
2016-10-20 19:24:47 +00:00
import unicodedata
2013-06-03 16:31:39 +00:00
2017-11-28 02:45:45 +00:00
from datetime import date
2013-06-03 16:31:39 +00:00
from decimal import Decimal as D
2016-08-15 20:12:27 +00:00
#django imports
2011-10-03 16:36:04 +00:00
from django import forms
2011-11-29 15:48:37 +00:00
from django . conf import settings
2013-06-03 16:31:39 +00:00
from django . contrib . auth . models import User
2012-05-01 14:56:19 +00:00
from django . forms . widgets import RadioSelect
2011-11-21 03:23:51 +00:00
from django . forms . extras . widgets import SelectDateWidget
2013-06-03 16:31:39 +00:00
from django . utils . translation import ugettext_lazy as _
2011-11-18 14:22:21 +00:00
2013-06-03 16:31:39 +00:00
from selectable . forms import (
AutoCompleteSelectWidget ,
AutoCompleteSelectField
)
2011-11-18 14:22:21 +00:00
2016-08-15 20:12:27 +00:00
#regluit imports
2014-01-31 20:45:02 +00:00
2013-06-03 16:31:39 +00:00
from regluit . core . models import (
UserProfile ,
RightsHolder ,
Claim ,
Campaign ,
2016-05-26 16:19:33 +00:00
Identifier ,
2013-06-17 22:53:21 +00:00
Offer ,
2013-06-03 16:31:39 +00:00
Premium ,
Ebook ,
2013-06-17 22:53:21 +00:00
EbookFile ,
2013-06-03 16:31:39 +00:00
Edition ,
PledgeExtra ,
Work ,
Press ,
2013-07-26 22:30:45 +00:00
Libpref ,
2013-06-03 16:31:39 +00:00
TWITTER ,
FACEBOOK ,
2014-11-07 19:47:12 +00:00
UNGLUEITAR
2013-06-03 16:31:39 +00:00
)
2013-10-15 20:18:30 +00:00
from regluit . libraryauth . models import Library
2016-08-15 22:28:39 +00:00
from regluit . core . parameters import (
LIBRARY ,
REWARDS ,
BUY2UNGLUE ,
THANKS ,
)
2013-07-17 14:34:01 +00:00
from regluit . core . lookups import (
OwnerLookup ,
WorkLookup ,
2015-01-14 20:07:54 +00:00
SubjectLookup ,
2013-07-17 14:34:01 +00:00
)
2017-08-07 20:13:22 +00:00
from regluit . core . validation import test_file
2016-08-15 20:12:27 +00:00
from regluit . utils . fields import ISBNField
2017-08-07 20:13:22 +00:00
2017-07-25 13:28:14 +00:00
2017-07-26 21:33:38 +00:00
from . bibforms import EditionForm , IdentifierForm
2017-11-28 01:12:42 +00:00
from . rh_forms import (
2017-11-28 02:45:45 +00:00
CCDateForm ,
2017-11-28 01:12:42 +00:00
CloneCampaignForm ,
2017-11-28 02:45:45 +00:00
date_selector ,
DateCalculatorForm ,
2017-11-28 01:12:42 +00:00
EditManagersForm ,
2017-11-28 02:45:45 +00:00
ManageCampaignForm ,
2017-11-28 01:12:42 +00:00
OpenCampaignForm ,
RightsHolderForm ,
UserClaimForm
)
2017-06-20 15:08:14 +00:00
from questionnaire . models import Questionnaire
2012-03-16 20:24:55 +00:00
2011-11-30 02:02:51 +00:00
logger = logging . getLogger ( __name__ )
2015-09-21 17:01:27 +00:00
2016-05-26 16:19:33 +00:00
class SurveyForm ( forms . Form ) :
label = forms . CharField ( max_length = 64 , required = True )
survey = forms . ModelChoiceField ( Questionnaire . objects . all ( ) , widget = RadioSelect ( ) , empty_label = None , required = True , )
isbn = ISBNField (
2016-08-15 20:12:27 +00:00
label = _ ( " ISBN " ) ,
max_length = 17 ,
2016-05-26 16:19:33 +00:00
required = False ,
help_text = _ ( " 13 digits, no dash. " ) ,
error_messages = {
' invalid ' : _ ( " This must be a valid ISBN-13. " ) ,
}
)
2016-08-15 20:12:27 +00:00
2016-05-26 16:19:33 +00:00
def clean_isbn ( self ) :
isbn = self . cleaned_data [ ' isbn ' ]
if not isbn :
return ' '
try :
self . work = Identifier . objects . get ( type = ' isbn ' , value = isbn ) . work
return isbn
except Identifier . DoesNotExist :
self . work = None
raise forms . ValidationError ( ' That ISBN is not in our database ' )
2013-06-17 22:53:21 +00:00
class EbookFileForm ( forms . ModelForm ) :
2017-07-25 13:28:14 +00:00
file = forms . FileField ( max_length = 16777216 )
2016-09-23 18:53:54 +00:00
version_label = forms . CharField ( max_length = 512 , required = False )
2017-07-25 13:28:14 +00:00
new_version_label = forms . CharField ( required = False )
2016-08-15 20:12:27 +00:00
2014-01-15 13:32:55 +00:00
def __init__ ( self , campaign_type = BUY2UNGLUE , * args , * * kwargs ) :
super ( EbookFileForm , self ) . __init__ ( * args , * * kwargs )
self . campaign_type = campaign_type
if campaign_type == BUY2UNGLUE :
2016-08-15 20:12:27 +00:00
self . fields [ ' format ' ] . widget = forms . HiddenInput ( )
2014-01-31 20:45:02 +00:00
if campaign_type == THANKS :
2016-08-15 20:12:27 +00:00
self . fields [ ' format ' ] . widget = forms . Select (
choices = ( ( ' pdf ' , ' PDF ' ) , ( ' epub ' , ' EPUB ' ) , ( ' mobi ' , ' MOBI ' ) )
)
2016-09-23 18:53:54 +00:00
def clean_version_label ( self ) :
new_label = self . data . get ( ' new_version_label ' , ' ' )
return new_label if new_label else self . cleaned_data [ ' version_label ' ]
2014-01-15 13:32:55 +00:00
def clean_format ( self ) :
if self . campaign_type is BUY2UNGLUE :
return ' epub '
else :
logger . info ( " EbookFileForm " + self . cleaned_data . get ( ' format ' , ' ' ) )
return self . cleaned_data . get ( ' format ' , ' ' )
2016-08-15 20:12:27 +00:00
2014-01-31 20:45:02 +00:00
def clean ( self ) :
2017-08-07 20:13:22 +00:00
fformat = self . cleaned_data [ ' format ' ]
2016-08-15 20:12:27 +00:00
the_file = self . cleaned_data . get ( ' file ' , None )
2017-08-07 20:13:22 +00:00
test_file ( the_file , fformat )
2014-01-31 20:45:02 +00:00
return self . cleaned_data
2014-01-15 13:32:55 +00:00
2013-06-17 22:53:21 +00:00
class Meta :
model = EbookFile
2014-01-15 13:32:55 +00:00
widgets = { ' edition ' : forms . HiddenInput }
2017-08-08 14:02:25 +00:00
fields = ( ' file ' , ' format ' , ' edition ' )
2017-07-25 13:28:14 +00:00
2012-02-28 22:28:33 +00:00
class EbookForm ( forms . ModelForm ) :
2017-07-25 13:28:14 +00:00
file = forms . FileField ( max_length = 16777216 , required = False )
2016-08-26 14:28:23 +00:00
url = forms . CharField ( required = False , widget = forms . TextInput ( attrs = { ' size ' : 60 } , ) )
2017-07-25 13:28:14 +00:00
version_label = forms . CharField ( required = False )
new_version_label = forms . CharField ( required = False )
2012-02-28 22:28:33 +00:00
class Meta :
model = Ebook
2017-04-17 17:47:40 +00:00
#exclude = ('created', 'download_count', 'active', 'filesize', 'version_iter')
fields = ( ' url ' , ' format ' , ' provider ' , ' version_label ' , ' rights ' , ' edition ' , ' user ' )
2016-08-15 20:12:27 +00:00
widgets = {
' edition ' : forms . HiddenInput ,
' user ' : forms . HiddenInput ,
' provider ' : forms . HiddenInput ,
2012-02-28 22:28:33 +00:00
}
2016-09-23 18:53:54 +00:00
def clean_version_label ( self ) :
new_label = self . data . get ( ' new_version_label ' , ' ' )
return new_label if new_label else self . cleaned_data [ ' version_label ' ]
2017-07-25 13:28:14 +00:00
2012-02-28 22:28:33 +00:00
def clean_provider ( self ) :
2016-10-05 22:02:03 +00:00
url = self . cleaned_data [ ' url ' ]
new_provider = Ebook . infer_provider ( url )
if url and not new_provider :
2017-04-17 17:47:51 +00:00
raise forms . ValidationError ( _ ( " At this time, ebook URLs must point at Internet Archive, Wikisources, Wikibooks, Hathitrust, Project Gutenberg, raw files at Github, Google Books, or OApen. " ) )
2016-10-05 22:02:03 +00:00
return new_provider if new_provider else " Unglue.it "
2016-08-15 20:12:27 +00:00
2012-03-05 21:03:57 +00:00
def clean_url ( self ) :
2016-08-26 14:28:23 +00:00
url = self . cleaned_data [ ' url ' ]
2012-03-05 21:03:57 +00:00
try :
Ebook . objects . get ( url = url )
except Ebook . DoesNotExist :
return url
raise forms . ValidationError ( _ ( " There ' s already an ebook with that url. " ) )
2016-08-15 20:12:27 +00:00
2016-08-26 14:28:23 +00:00
def clean ( self ) :
2017-04-17 17:47:40 +00:00
format = self . cleaned_data . get ( ' format ' , ' ' )
2016-08-26 14:28:23 +00:00
the_file = self . cleaned_data . get ( ' file ' , None )
url = self . cleaned_data . get ( ' url ' , None )
2017-09-26 14:36:51 +00:00
test_file ( the_file , format )
2016-08-26 14:28:23 +00:00
if not the_file and not url :
raise forms . ValidationError ( _ ( " Either a link or a file is required. " ) )
if the_file and url :
self . cleaned_data [ ' url ' ] = ' '
return self . cleaned_data
2011-10-03 16:36:04 +00:00
class ProfileForm ( forms . ModelForm ) :
2016-08-15 20:12:27 +00:00
clear_facebook = forms . BooleanField ( required = False )
clear_twitter = forms . BooleanField ( required = False )
clear_goodreads = forms . BooleanField ( required = False )
2011-10-03 16:36:04 +00:00
class Meta :
model = UserProfile
2013-03-15 01:42:00 +00:00
fields = ' tagline ' , ' librarything_id ' , ' home_url ' , ' clear_facebook ' , ' clear_twitter ' , ' clear_goodreads ' , ' avatar_source '
2011-10-03 16:36:04 +00:00
widgets = {
2012-02-02 18:55:21 +00:00
' tagline ' : forms . Textarea ( attrs = { ' rows ' : 5 , ' onKeyUp ' : " counter(this, 140) " , ' onBlur ' : " counter(this, 140) " } ) ,
2011-10-03 16:36:04 +00:00
}
2013-03-15 01:42:00 +00:00
def __init__ ( self , * args , * * kwargs ) :
profile = kwargs . get ( ' instance ' )
super ( ProfileForm , self ) . __init__ ( * args , * * kwargs )
choices = [ ]
for choice in self . fields [ ' avatar_source ' ] . choices :
if choice [ 0 ] == FACEBOOK and not profile . facebook_id :
pass
elif choice [ 0 ] == TWITTER and not profile . twitter_id :
pass
else :
choices . append ( choice )
self . fields [ ' avatar_source ' ] . choices = choices
def clean ( self ) :
# check that if a social net is cleared, we're not using it a avatar source
2016-08-15 20:12:27 +00:00
if self . cleaned_data . get ( " clear_facebook " , False ) and self . cleaned_data . get ( " avatar_source " , None ) == FACEBOOK :
self . cleaned_data [ " avatar_source " ] == UNGLUEITAR
if self . cleaned_data . get ( " clear_twitter " , False ) and self . cleaned_data . get ( " avatar_source " , None ) == TWITTER :
self . cleaned_data [ " avatar_source " ] == UNGLUEITAR
2013-03-15 01:42:00 +00:00
return self . cleaned_data
2011-10-11 17:03:40 +00:00
2012-08-14 01:00:28 +00:00
def getTransferCreditForm ( maximum , data = None , * args , * * kwargs ) :
class TransferCreditForm ( forms . Form ) :
recipient = AutoCompleteSelectField (
OwnerLookup ,
label = ' Recipient ' ,
widget = AutoCompleteSelectWidget ( OwnerLookup ) ,
required = True ,
error_messages = { ' required ' : ' Please ensure the recipient is a valid Unglue.it account. ' } ,
)
amount = forms . IntegerField (
required = True ,
2016-08-15 20:12:27 +00:00
min_value = 1 ,
2012-08-14 01:00:28 +00:00
max_value = maximum ,
label = " Transfer Amount " ,
2016-08-15 20:12:27 +00:00
error_messages = {
' min_value ' : ' Transfer amount must be positive ' ,
' max_value ' : ' You only have %(limit_value)s available for transfer '
} ,
2012-08-14 01:00:28 +00:00
)
return TransferCreditForm ( data = data )
2012-12-13 03:35:35 +00:00
class WorkForm ( forms . Form ) :
2016-08-15 20:12:27 +00:00
other_work = forms . ModelChoiceField ( queryset = Work . objects . all ( ) ,
widget = forms . HiddenInput ( ) ,
required = True ,
2012-12-13 03:35:35 +00:00
error_messages = { ' required ' : ' Missing work to merge with. ' } ,
)
2016-08-15 20:12:27 +00:00
work = None
2012-12-13 03:35:35 +00:00
def clean_other_work ( self ) :
2016-08-15 20:12:27 +00:00
if self . cleaned_data [ " other_work " ] . id == self . work . id :
2012-12-13 03:35:35 +00:00
raise forms . ValidationError ( _ ( " You can ' t merge a work into itself " ) )
return self . cleaned_data [ " other_work " ]
def __init__ ( self , work = None , * args , * * kwargs ) :
super ( WorkForm , self ) . __init__ ( * args , * * kwargs )
2016-08-15 20:12:27 +00:00
self . work = work
2012-12-13 03:35:35 +00:00
class OtherWorkForm ( WorkForm ) :
other_work = AutoCompleteSelectField (
WorkLookup ,
2013-04-24 18:30:39 +00:00
label = ' Other Work (title) ' ,
2012-12-13 03:35:35 +00:00
widget = AutoCompleteSelectWidget ( WorkLookup ) ,
required = True ,
error_messages = { ' required ' : ' Missing work to merge with. ' } ,
2016-08-15 20:12:27 +00:00
)
2012-12-13 03:35:35 +00:00
def __init__ ( self , * args , * * kwargs ) :
super ( OtherWorkForm , self ) . __init__ ( * args , * * kwargs )
self . fields [ ' other_work ' ] . widget . update_query_parameters ( { ' language ' : self . work . language } )
2016-08-15 20:12:27 +00:00
2012-03-26 19:31:41 +00:00
class CustomPremiumForm ( forms . ModelForm ) :
class Meta :
model = Premium
fields = ' campaign ' , ' amount ' , ' description ' , ' type ' , ' limit '
2016-08-15 20:12:27 +00:00
widgets = {
2012-10-04 16:08:40 +00:00
' description ' : forms . Textarea ( attrs = { ' cols ' : 80 , ' rows ' : 4 } ) ,
2012-03-26 19:31:41 +00:00
' campaign ' : forms . HiddenInput ,
' type ' : forms . HiddenInput ( attrs = { ' value ' : ' XX ' } ) ,
' limit ' : forms . TextInput ( attrs = { ' value ' : ' 0 ' } ) ,
}
2016-07-24 22:50:48 +00:00
def clean_type ( self ) :
return ' CU '
2016-08-15 20:12:27 +00:00
2013-06-17 22:53:21 +00:00
class OfferForm ( forms . ModelForm ) :
class Meta :
model = Offer
2014-02-25 19:29:54 +00:00
fields = ' work ' , ' price ' , ' license '
2016-08-15 20:12:27 +00:00
widgets = {
2013-06-17 22:53:21 +00:00
' work ' : forms . HiddenInput ,
2013-10-11 16:50:59 +00:00
' license ' : forms . HiddenInput ,
2013-06-17 22:53:21 +00:00
}
2016-08-15 20:12:27 +00:00
2011-11-20 02:12:18 +00:00
2013-08-16 19:49:44 +00:00
class CampaignPurchaseForm ( forms . Form ) :
anonymous = forms . BooleanField ( required = False , label = _ ( " Make this purchase anonymous, please " ) )
offer_id = forms . IntegerField ( required = False )
2016-08-15 20:12:27 +00:00
offer = None
2013-10-15 20:18:30 +00:00
library_id = forms . IntegerField ( required = False )
library = None
2016-08-15 20:12:27 +00:00
copies = forms . IntegerField ( required = False , min_value = 1 )
2014-12-16 19:18:51 +00:00
give_to = forms . EmailField ( required = False )
give_message = forms . CharField ( required = False , max_length = 512 , )
2016-08-15 20:12:27 +00:00
2013-08-16 19:49:44 +00:00
def clean_offer_id ( self ) :
offer_id = self . cleaned_data [ ' offer_id ' ]
try :
2016-08-15 20:12:27 +00:00
self . offer = Offer . objects . get ( id = offer_id )
2013-08-16 19:49:44 +00:00
except Offer . DoesNotExist :
raise forms . ValidationError ( _ ( " Sorry, that offer is not valid. " ) )
2013-10-15 20:18:30 +00:00
def clean_library_id ( self ) :
library_id = self . cleaned_data [ ' library_id ' ]
if library_id :
try :
self . library = Library . objects . get ( id = library_id )
except Library . DoesNotExist :
raise forms . ValidationError ( _ ( " Sorry, that Library is not valid. " ) )
2016-08-15 20:12:27 +00:00
2014-12-16 19:18:51 +00:00
def clean_copies ( self ) :
2016-08-15 20:12:27 +00:00
copies = self . cleaned_data . get ( ' copies ' , 1 )
2014-12-16 19:18:51 +00:00
return copies if copies else 1
2016-08-15 20:12:27 +00:00
2014-12-18 06:07:59 +00:00
def clean_anonymous ( self ) :
if self . data . get ( ' give ' , False ) :
return True
else :
return self . cleaned_data [ ' anonymous ' ]
2016-08-15 20:12:27 +00:00
2013-10-15 20:18:30 +00:00
def clean ( self ) :
if self . offer . license == LIBRARY :
if not self . library :
raise forms . ValidationError ( _ ( " No library specified. " ) )
2014-12-16 19:18:51 +00:00
if self . data . get ( ' give ' , False ) :
2016-08-15 20:12:27 +00:00
if not self . cleaned_data . get ( ' give_to ' , None ) :
2014-12-16 19:18:51 +00:00
raise forms . ValidationError ( _ ( " Gift recipient email is needed. " ) )
else :
if ' give_to ' in self . _errors :
del self . _errors [ ' give_to ' ]
2013-10-15 20:18:30 +00:00
return self . cleaned_data
2013-08-16 19:49:44 +00:00
def amount ( self ) :
2016-08-15 20:12:27 +00:00
return self . offer . price * self . cleaned_data . get ( ' copies ' , 1 ) if self . offer else None
2013-08-16 19:49:44 +00:00
@property
def trans_extra ( self ) :
2013-10-15 20:18:30 +00:00
pe = PledgeExtra ( anonymous = self . cleaned_data [ ' anonymous ' ] ,
2013-08-20 02:54:43 +00:00
offer = self . offer )
2013-10-15 20:18:30 +00:00
if self . library :
2016-08-15 20:12:27 +00:00
pe . extra [ ' library_id ' ] = self . library . id
pe . extra [ ' copies ' ] = self . cleaned_data . get ( ' copies ' , 1 )
2014-12-16 19:18:51 +00:00
if self . data . get ( ' give ' , False ) :
2016-08-15 20:12:27 +00:00
pe . extra [ ' give_to ' ] = self . cleaned_data [ ' give_to ' ]
pe . extra [ ' give_message ' ] = self . cleaned_data [ ' give_message ' ]
2013-10-15 20:18:30 +00:00
return pe
2013-08-16 19:49:44 +00:00
2014-02-20 03:18:23 +00:00
class CampaignThanksForm ( forms . Form ) :
2016-08-15 20:12:27 +00:00
anonymous = forms . BooleanField (
required = False ,
label = _ ( " Make this contribution anonymous, please " )
)
2014-02-20 03:18:23 +00:00
preapproval_amount = forms . DecimalField (
2014-05-19 16:09:47 +00:00
required = True ,
2014-02-20 03:18:23 +00:00
min_value = D ( ' 1.00 ' ) ,
2016-08-15 20:12:27 +00:00
max_value = D ( ' 2000.00 ' ) ,
decimal_places = 2 ,
2014-02-20 03:18:23 +00:00
label = " Pledge Amount " ,
)
@property
def trans_extra ( self ) :
pe = PledgeExtra ( anonymous = self . cleaned_data [ ' anonymous ' ] )
2017-02-13 18:33:26 +00:00
class DonationForm ( forms . Form ) :
amount = forms . DecimalField (
required = True ,
min_value = D ( ' 1.00 ' ) ,
max_value = D ( ' 20000.00 ' ) ,
decimal_places = 2 ,
label = " Donation Amount " ,
)
2014-02-20 03:18:23 +00:00
2011-10-11 17:03:40 +00:00
class CampaignPledgeForm ( forms . Form ) :
2011-11-20 02:12:18 +00:00
preapproval_amount = forms . DecimalField (
2012-03-15 21:54:38 +00:00
required = False ,
min_value = D ( ' 1.00 ' ) ,
2016-08-15 20:12:27 +00:00
max_value = D ( ' 2000.00 ' ) ,
decimal_places = 2 ,
2011-11-20 02:12:18 +00:00
label = " Pledge Amount " ,
)
2013-08-16 19:49:44 +00:00
def amount ( self ) :
return self . cleaned_data [ " preapproval_amount " ] if self . cleaned_data else None
2016-08-15 20:12:27 +00:00
2012-10-03 20:02:51 +00:00
anonymous = forms . BooleanField ( required = False , label = _ ( " Make this pledge anonymous, please " ) )
2016-08-15 20:12:27 +00:00
ack_name = forms . CharField (
required = False ,
max_length = 64 ,
label = _ ( " What name should we display? " )
)
2012-08-01 12:52:39 +00:00
ack_dedication = forms . CharField ( required = False , max_length = 140 , label = _ ( " Your dedication: " ) )
2011-11-30 02:02:51 +00:00
premium_id = forms . IntegerField ( required = False )
2016-08-15 20:12:27 +00:00
premium = None
2012-09-07 13:46:38 +00:00
@property
2013-08-16 19:49:44 +00:00
def trans_extra ( self ) :
2012-09-07 13:46:38 +00:00
return PledgeExtra ( anonymous = self . cleaned_data [ ' anonymous ' ] ,
ack_name = self . cleaned_data [ ' ack_name ' ] ,
ack_dedication = self . cleaned_data [ ' ack_dedication ' ] ,
premium = self . premium )
2016-08-15 20:12:27 +00:00
2012-03-15 21:54:38 +00:00
def clean_preapproval_amount ( self ) :
2012-08-31 07:16:04 +00:00
preapproval_amount = self . cleaned_data [ ' preapproval_amount ' ]
if preapproval_amount is None :
2012-03-15 21:54:38 +00:00
raise forms . ValidationError ( _ ( " Please enter a pledge amount. " ) )
2012-08-31 07:16:04 +00:00
return preapproval_amount
2016-08-15 20:12:27 +00:00
2012-09-07 13:46:38 +00:00
def clean_premium_id ( self ) :
premium_id = self . cleaned_data [ ' premium_id ' ]
try :
2016-08-15 20:12:27 +00:00
self . premium = Premium . objects . get ( id = premium_id )
if self . premium . limit > 0 :
if self . premium . limit < = self . premium . premium_count :
2012-09-07 13:46:38 +00:00
raise forms . ValidationError ( _ ( " Sorry, that premium is fully subscribed. " ) )
except Premium . DoesNotExist :
raise forms . ValidationError ( _ ( " Sorry, that premium is not valid. " ) )
2016-08-15 20:12:27 +00:00
2011-11-30 02:02:51 +00:00
def clean ( self ) :
# check on whether the preapproval amount is < amount for premium tier. If so, put an error message
2012-10-13 14:13:46 +00:00
preapproval_amount = self . cleaned_data . get ( " preapproval_amount " )
if preapproval_amount is None :
# preapproval_amount failed validation, that error is the relevant one
return self . cleaned_data
2012-10-14 15:33:13 +00:00
elif self . premium is None :
2016-08-15 20:12:27 +00:00
raise forms . ValidationError ( _ ( " Please select a premium. " ) )
2012-10-13 14:13:46 +00:00
elif preapproval_amount < self . premium . amount :
logger . info ( " raising form validating error " )
raise forms . ValidationError ( _ ( " Sorry, you must pledge at least $ %s to select that premium. " % ( self . premium . amount ) ) )
2012-09-07 13:46:38 +00:00
return self . cleaned_data
2011-11-30 02:02:51 +00:00
2014-09-13 20:19:47 +00:00
class TokenCCMixin ( forms . Form ) :
2014-08-30 17:28:13 +00:00
stripe_token = forms . CharField ( required = True , widget = forms . HiddenInput ( ) )
2012-10-15 03:41:17 +00:00
2014-09-13 20:19:47 +00:00
class BaseCCMixin ( forms . Form ) :
2012-10-02 13:32:33 +00:00
work_id = forms . IntegerField ( required = False , widget = forms . HiddenInput ( ) )
2016-08-15 20:12:27 +00:00
preapproval_amount = forms . DecimalField (
2011-12-03 00:37:27 +00:00
required = False ,
2016-08-15 20:12:27 +00:00
min_value = D ( ' 1.00 ' ) ,
max_value = D ( ' 100000.00 ' ) ,
decimal_places = 2 ,
2013-09-06 17:58:23 +00:00
label = " Amount " ,
2011-12-03 00:37:27 +00:00
)
2014-09-13 20:19:47 +00:00
class UserCCMixin ( forms . Form ) :
username = forms . CharField ( max_length = 30 , required = True , widget = forms . HiddenInput ( ) )
class PlainCCForm ( TokenCCMixin , forms . Form ) :
pass
class BaseCCForm ( BaseCCMixin , TokenCCMixin , forms . Form ) :
pass
2011-12-03 00:37:27 +00:00
2014-02-20 03:18:23 +00:00
class AnonCCForm ( BaseCCForm ) :
email = forms . CharField ( max_length = 30 , required = False , widget = forms . TextInput ( ) )
2014-09-13 20:19:47 +00:00
class CCForm ( UserCCMixin , BaseCCForm ) :
pass
class AccountCCForm ( BaseCCMixin , UserCCMixin , forms . Form ) :
pass
2016-08-15 20:12:27 +00:00
2011-11-01 00:26:05 +00:00
class GoodreadsShelfLoadingForm ( forms . Form ) :
2011-11-15 20:51:38 +00:00
goodreads_shelf_name_number = forms . CharField ( widget = forms . Select ( choices = (
2011-11-01 00:26:05 +00:00
( ' all ' , ' all ' ) ,
) ) )
2011-11-16 18:20:10 +00:00
class LibraryThingForm ( forms . Form ) :
2011-11-16 19:58:39 +00:00
lt_username = forms . CharField ( max_length = 30 , required = True )
2012-05-21 14:56:18 +00:00
class PledgeCancelForm ( forms . Form ) :
2012-05-23 14:22:48 +00:00
# which campaign whose active transaction to cancel?
2012-05-21 14:56:18 +00:00
campaign_id = forms . IntegerField ( required = True , widget = forms . HiddenInput ( ) )
2012-05-23 14:22:48 +00:00
2011-12-20 22:42:06 +00:00
class CampaignAdminForm ( forms . Form ) :
2012-05-21 14:56:18 +00:00
campaign_id = forms . IntegerField ( )
2016-08-15 20:12:27 +00:00
2011-12-29 01:43:52 +00:00
class EmailShareForm ( forms . Form ) :
2012-04-16 19:28:06 +00:00
recipient = forms . EmailField ( error_messages = { ' required ' : ' Please specify a recipient. ' } )
subject = forms . CharField ( max_length = 100 , error_messages = { ' required ' : ' Please specify a subject. ' } )
2016-08-15 20:12:27 +00:00
message = forms . CharField (
widget = forms . Textarea ( ) ,
error_messages = { ' required ' : ' Please include a message. ' }
)
2012-02-28 22:28:33 +00:00
# allows us to return user to original page by passing it as hidden form input
# we can't rely on POST or GET since the emailshare view handles both
# and may iterate several times as it catches user errors, losing URL info
next = forms . CharField ( widget = forms . HiddenInput ( ) )
2016-08-15 20:12:27 +00:00
2012-01-09 20:53:09 +00:00
class FeedbackForm ( forms . Form ) :
2016-08-15 20:12:27 +00:00
sender = forms . EmailField (
widget = forms . TextInput ( attrs = { ' size ' : 50 } ) ,
label = " Your email " ,
error_messages = { ' required ' : ' Please specify your email address. ' }
)
subject = forms . CharField (
max_length = 500 ,
widget = forms . TextInput ( attrs = { ' size ' : 50 } ) ,
error_messages = { ' required ' : ' Please specify a subject. ' }
)
message = forms . CharField (
widget = forms . Textarea ( ) ,
error_messages = { ' required ' : ' Please specify a message. ' }
)
2012-02-28 22:28:33 +00:00
page = forms . CharField ( widget = forms . HiddenInput ( ) )
2016-08-15 20:12:27 +00:00
notarobot = forms . IntegerField (
label = " Please prove you ' re not a robot " ,
error_messages = { ' required ' : " You must do the sum to prove you ' re not a robot. " }
)
2012-02-28 22:28:33 +00:00
answer = forms . IntegerField ( widget = forms . HiddenInput ( ) )
num1 = forms . IntegerField ( widget = forms . HiddenInput ( ) )
num2 = forms . IntegerField ( widget = forms . HiddenInput ( ) )
2016-08-15 20:12:27 +00:00
2012-02-28 22:28:33 +00:00
def clean ( self ) :
cleaned_data = self . cleaned_data
notarobot = str ( cleaned_data . get ( " notarobot " ) )
answer = str ( cleaned_data . get ( " answer " ) )
2012-04-16 19:28:06 +00:00
if notarobot != answer :
2012-02-28 22:28:33 +00:00
raise forms . ValidationError ( _ ( " Whoops, try that sum again. " ) )
2016-08-15 20:12:27 +00:00
2012-02-28 22:28:33 +00:00
return cleaned_data
2013-02-26 17:43:54 +00:00
class MsgForm ( forms . Form ) :
2016-08-15 20:12:27 +00:00
msg = forms . CharField (
widget = forms . Textarea ( ) ,
error_messages = { ' required ' : ' Please specify a message. ' }
)
2013-02-26 17:43:54 +00:00
def full_clean ( self ) :
2016-08-15 20:12:27 +00:00
super ( MsgForm , self ) . full_clean ( )
2013-02-26 17:43:54 +00:00
if self . data . has_key ( " supporter " ) :
try :
self . cleaned_data [ ' supporter ' ] = User . objects . get ( id = self . data [ " supporter " ] )
except User . DoesNotExist :
raise ValidationError ( " Supporter does not exist " )
else :
raise ValidationError ( " Supporter is not specified " )
if self . data . has_key ( " work " ) :
try :
self . cleaned_data [ ' work ' ] = Work . objects . get ( id = self . data [ " work " ] )
except Work . DoesNotExist :
raise ValidationError ( " Work does not exist " )
else :
raise ValidationError ( " Work is not specified " )
2016-08-15 20:12:27 +00:00
2013-04-04 15:09:18 +00:00
class PressForm ( forms . ModelForm ) :
class Meta :
model = Press
2016-07-21 19:52:07 +00:00
exclude = ( )
2013-04-04 15:09:18 +00:00
2016-08-15 20:12:27 +00:00
widgets = {
2013-04-04 15:40:03 +00:00
' date ' : SelectDateWidget ( years = range ( 2010 , 2025 ) ) ,
2013-04-04 15:09:18 +00:00
}
2016-08-15 20:12:27 +00:00
2013-05-31 19:19:58 +00:00
class KindleEmailForm ( forms . Form ) :
kindle_email = forms . EmailField ( )
2016-08-15 20:12:27 +00:00
2014-10-27 23:11:44 +00:00
class LibModeForm ( forms . ModelForm ) :
2013-07-23 16:23:04 +00:00
class Meta :
2013-07-26 22:30:45 +00:00
model = Libpref
2014-10-27 15:55:46 +00:00
fields = ( )
2014-12-18 18:37:28 +00:00
class RegiftForm ( forms . Form ) :
give_to = forms . EmailField ( label = " email address of recipient " )
2016-08-15 20:12:27 +00:00
give_message = forms . CharField (
max_length = 512 ,
label = " your gift message " ,
initial = " Here ' s an ebook from unglue.it, I hope you like it! - me " ,
)
2015-01-14 20:07:54 +00:00
class SubjectSelectForm ( forms . Form ) :
add_kw = AutoCompleteSelectField (
SubjectLookup ,
widget = AutoCompleteSelectWidget ( SubjectLookup , allow_new = False ) ,
label = ' Keyword ' ,
2015-03-12 15:58:49 +00:00
)
class MapSubjectForm ( forms . Form ) :
subject = AutoCompleteSelectField (
SubjectLookup ,
widget = AutoCompleteSelectWidget ( SubjectLookup , allow_new = False ) ,
label = ' Source Subject ' ,
)
onto_subject = AutoCompleteSelectField (
SubjectLookup ,
widget = AutoCompleteSelectWidget ( SubjectLookup , allow_new = False ) ,
label = ' Target Subject ' ,
)
2016-08-15 20:12:27 +00:00