def clean(self): for k in ['name', 'app_sequence', 'num_demo_participants']: if k not in self: msg = f'Session config is missing "{k}"' raise SessionConfigError(msg) validate_alphanumeric( self['name'], identifier_description='settings.SESSION_CONFIGS name' ) app_sequence = self['app_sequence'] if len(app_sequence) != len(set(app_sequence)): msg = ( 'settings.SESSION_CONFIGS: ' 'app_sequence of "{}" ' 'must not contain duplicate elements. ' 'If you want multiple rounds, ' 'you should set Constants.num_rounds.' ) raise SessionConfigError(msg.format(self['name'])) if len(app_sequence) == 0: raise SessionConfigError( 'settings.SESSION_CONFIGS: app_sequence cannot be empty.' ) self.setdefault('display_name', self['name']) self.setdefault('doc', '') self['participation_fee'] = RealWorldCurrency(self['participation_fee'])
class RealWorldCurrencyField(BaseCurrencyField): MONEY_CLASS = RealWorldCurrency auto_submit_default = RealWorldCurrency(0) def formfield(self, **kwargs): import otree.forms defaults = { 'form_class': otree.forms.RealWorldCurrencyField, 'choices_form_class': otree.forms.CurrencyChoiceField, } defaults.update(kwargs) return super().formfield(**defaults)
def form_valid(self, form): session = self.session session.label = form.label.data session.comment = form.comment.data participation_fee = form.participation_fee.data rwc_per_point = form.real_world_currency_per_point.data config = session.config.copy() if participation_fee is not None: config['participation_fee'] = RealWorldCurrency(participation_fee) if rwc_per_point is not None: config['real_world_currency_per_point'] = rwc_per_point # need to do this to get SQLAlchemy to detect a change session.config = config enqueue_admin_message('success', 'Properties have been updated') return self.redirect('SessionEditProperties', code=session.code)
def form_valid(self, form): session = self.session session.label = form.cleaned_data['label'] session.comment = form.cleaned_data['comment'] participation_fee = form.cleaned_data['participation_fee'] rwc_per_point = form.cleaned_data['real_world_currency_per_point'] if participation_fee is not None: # need to convert back to RealWorldCurrency, because easymoney # MoneyFormField returns a decimal, not Money (not sure why) session.config['participation_fee'] = RealWorldCurrency(participation_fee) if rwc_per_point is not None: session.config['real_world_currency_per_point'] = rwc_per_point self.session.save() messages.success(self.request, 'Properties have been updated') return redirect('SessionEditProperties', session.code)
class RealWorldCurrencyField(BaseCurrencyField): MONEY_CLASS = RealWorldCurrency auto_submit_default = RealWorldCurrency(0)
class CurrencyType(BaseCurrencyType): MONEY_CLASS = Currency class RealWorldCurrencyType(BaseCurrencyType): MONEY_CLASS = RealWorldCurrency AUTO_SUBMIT_DEFAULTS = { st.Boolean: False, st.Integer: 0, st.Float: 0, st.String: '', st.Text: '', RealWorldCurrencyType: Currency(0), CurrencyType: RealWorldCurrency(0), } def wrap_column(coltype, *, initial=None, null=True, **form_props) -> OTreeColumn: if 'default' in form_props: raise Exception( "Don't use default= in model fields. Use initial= instead.") col = OTreeColumn(coltype, default=initial, nullable=null) col.form_props = form_props col.auto_submit_default = AUTO_SUBMIT_DEFAULTS[