def add_maillist_from_web_form(mail, form): """Add a mailing list based on data submited from web form. @mail - mail address of mailing list account @form - a dict of web form input """ domain = mail.split('@', 1)[-1] # Store mlmmj parameters kvs = {} # Add empty values for 'remove_headers', 'custom_headers'. This will # trigger form process functions to add pre-defined default values. if 'remove_headers' not in form: form['remove_headers'] = '' if 'custom_headers' not in form: form['custom_headers'] = '' # Set 'owner' to 'postmaster@<domain>' if 'owner' not in form: form['owner'] = 'postmaster@' + domain # Set 'owner' to 'postmaster@<domain>' if 'moderators' not in form: form['moderators'] = 'postmaster@' + domain kvs.update(__convert_form_to_mlmmj_params(mail=mail, form=form)) # Add (missing) default settings _form = settings.MLMMJ_DEFAULT_PROFILE_SETTINGS for param in _form: # Avoid conflict parameters. if param == 'only_subscriber_can_post' and form.get( 'only_moderator_can_post') == 'yes': continue if param == 'only_moderator_can_post' and form.get( 'only_subscriber_can_post') == 'yes': continue if param not in form: kv = form_utils.get_dict_for_form_param(mail=mail, form=_form, param=param) kvs.update(kv) # Always set values _form = settings.MLMMJ_FORCED_PROFILE_SETTINGS for param in _form: kv = form_utils.get_dict_for_form_param(mail=mail, form=_form, param=param) kvs.update(kv) qr = create_ml(mail=mail, **kvs) return qr
def __convert_form_to_mlmmj_params(mail, form): """Convert variables in web form to (a dict of) mlmmj parameters.""" # Both 'moderate_subscription' and 'subscription_moderators' use same # mlmmj parameter name 'submod' if 'moderate_subscription' in form and 'subscription_moderators' in form: _mod = form.get('moderate_subscription') _moderators = form.get('subscription_moderators') if _mod == 'yes': if _moderators: # If there's some moderators, it will create 'submod' file # with emails of moderators. If file 'submod' presents, it # means moderation subscription is enabled. So we should remove # 'moderate_subscription' parameter here to avoid improper # file removal or re-creation (with empty content) form.pop('moderate_subscription') else: # If no subscription moderators, use an empty 'submod' file # and use mailing list owners as subscription moderators. form.pop('subscription_moderators') else: # remove 'subscription_moderators' and let mlmmjadmin remove # 'submod' directly. form.pop('subscription_moderators') # solve conflict of 'only_moderator_can_post' and 'only_subscriber_can_post' # 'only_moderator_can_post' should has higher priority if 'only_moderator_can_post' in form and 'only_subscriber_can_post' in form: if form.get('only_moderator_can_post') == 'yes': form['only_subscriber_can_post'] = 'no' # Store key:value of mlmmj parameters kvs = {} # Convert form variable names to mlmmj parameter names for param in form: kv = form_utils.get_dict_for_form_param(mail=mail, form=form, param=param) kvs.update(kv) return kvs