summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEvgeny Fadeev <evgeny.fadeev@gmail.com>2010-12-29 22:15:46 -0500
committerEvgeny Fadeev <evgeny.fadeev@gmail.com>2010-12-29 22:15:46 -0500
commit8392593b435c8e54e03bc76f87c5af9bff245a9d (patch)
treeddace6b36d8b98e775aa6e6a6cf8494b03dad650
parentd96d15da66736716f3d163aaf79ca24a101587e4 (diff)
downloadaskbot-8392593b435c8e54e03bc76f87c5af9bff245a9d.tar.gz
askbot-8392593b435c8e54e03bc76f87c5af9bff245a9d.tar.bz2
askbot-8392593b435c8e54e03bc76f87c5af9bff245a9d.zip
added a management command that creates a new user
-rw-r--r--askbot/doc/source/management-commands.rst8
-rw-r--r--askbot/forms.py31
-rw-r--r--askbot/management/commands/add_askbot_user.py83
-rw-r--r--askbot/tests/__init__.py1
-rw-r--r--askbot/tests/management_command_tests.py29
-rw-r--r--askbot/tests/page_load_tests.py1
6 files changed, 146 insertions, 7 deletions
diff --git a/askbot/doc/source/management-commands.rst b/askbot/doc/source/management-commands.rst
index deed097a..eb889bfa 100644
--- a/askbot/doc/source/management-commands.rst
+++ b/askbot/doc/source/management-commands.rst
@@ -28,6 +28,14 @@ The bulk of the management commands fall into this group and will probably be th
| `remove_admin <user_id>` | Remove admin status from a user account - the opposite of |
| | the `add_admin` command |
+---------------------------------+-------------------------------------------------------------+
+| `askbot_add_user --user-name | Create a user account. If password is not given, an |
+| --email [--password] | unusable password will be set. Possible values for the |
+| [--email-frequency]` | --email-frequency are: **i**, **d**, **w**, **n** |
+| | that stand for |
+| | instant, daily, weekly and never - respectively. The default|
+| | value is w. The command does not create associations with |
+| | any of the external login providers. |
++---------------------------------+-------------------------------------------------------------+
| `dump_forum [--dump-name | Save forum contents into a file. `--dump-name` parameter is |
| some_name`] | optional |
+---------------------------------+-------------------------------------------------------------+
diff --git a/askbot/forms.py b/askbot/forms.py
index 520cc5d9..08b5a4d9 100644
--- a/askbot/forms.py
+++ b/askbot/forms.py
@@ -634,15 +634,27 @@ class EditUserEmailFeedsForm(forms.Form):
return self
def reset(self):
+ """equivalent to set_frequency('n')
+ but also returns self due to some legacy requirement
+ todo: clean up use of this function
+ """
if self.is_bound:
- self.cleaned_data['all_questions'] = 'n'
- self.cleaned_data['asked_by_me'] = 'n'
- self.cleaned_data['answered_by_me'] = 'n'
- self.cleaned_data['individually_selected'] = 'n'
- self.cleaned_data['mentions_and_comments'] = 'n'
+ self.cleaned_data = self.NO_EMAIL_INITIAL
self.initial = self.NO_EMAIL_INITIAL
return self
+ def set_frequency(self, frequency = 'n'):
+ data = {
+ 'all_questions': frequency,
+ 'asked_by_me': frequency,
+ 'answered_by_me': frequency,
+ 'individually_selected': frequency,
+ 'mentions_and_comments': frequency
+ }
+ if self.is_bound:
+ self.cleaned_data = data
+ self.initial = data
+
def save(self,user,save_unbound=False):
"""
with save_unbound==True will bypass form validation and save initial values
@@ -684,10 +696,17 @@ class SimpleEmailSubscribeForm(forms.Form):
choices=SIMPLE_SUBSCRIBE_CHOICES
)
+ def __init__(self, *args, **kwargs):
+ self.frequency = kwargs.pop('frequency', 'w')
+ super(SimpleEmailSubscribeForm, self).__init__(*args, **kwargs)
+
def save(self, user=None):
EFF = EditUserEmailFeedsForm
- if self.cleaned_data['subscribe'] == 'y':
+ #here we have kind of an anomaly - the value 'y' is redundant
+ #with the frequency variable - needs to be fixed
+ if self.is_bound and self.cleaned_data['subscribe'] == 'y':
email_settings_form = EFF()
+ email_settings_form.set_frequency(self.frequency)
logging.debug('%s wants to subscribe' % user.username)
else:
email_settings_form = EFF(initial=EFF.NO_EMAIL_INITIAL)
diff --git a/askbot/management/commands/add_askbot_user.py b/askbot/management/commands/add_askbot_user.py
new file mode 100644
index 00000000..0b8fd02b
--- /dev/null
+++ b/askbot/management/commands/add_askbot_user.py
@@ -0,0 +1,83 @@
+"""management command that
+creates the askbot user account programmatically
+the command can add password, but it will not create
+associations with any of the federated login providers
+"""
+from optparse import make_option
+from django.core.management.base import BaseCommand, CommandError
+from askbot import models, forms
+
+class Command(BaseCommand):
+ "The command class itself"
+
+ help = """
+ """
+ option_list = BaseCommand.option_list + (
+ make_option('--user-name',
+ action = 'store',
+ type = 'str',
+ dest = 'username',
+ default = None,
+ help = 'user name **required**, same as screen '
+ 'name and django user name'
+ ),
+ make_option('--password',
+ action = 'store',
+ type = 'str',
+ dest = 'password',
+ default = None,
+ help = 'cleartext password. If not given, an unusable '
+ 'password will be set.'
+ ),
+ make_option('--email',
+ action = 'store',
+ type = 'str',
+ dest = 'email',
+ default = None,
+ help = 'email address - **required**'
+ ),
+ make_option('--email-frequency',
+ action = 'store',
+ type = 'str',
+ dest = 'frequency',
+ default = 'w',
+ help = 'email subscription frequency (n - never, i - '
+ 'instant, d - daily, w - weekly, default - w)'
+ ),
+ )
+
+ def handle(self, *args, **options):
+ """create an askbot user account, given email address,
+ user name, (optionally) password
+ and (also optionally) - the
+ default email delivery schedule
+ """
+ if options['email'] is None:
+ raise CommandError('the --email argument is required')
+ if options['username'] is None:
+ raise CommandError('the --user-name argument is required')
+
+ password = options['password']
+ email = options['email']
+ username = options['username']
+ frequency = options['frequency']
+
+ if frequency not in ('i', 'd', 'w', 'n'):
+ raise CommandError(
+ 'value of --frequency must be one of: '
+ 'i, d, w, n'
+ )
+
+ user = models.User.objects.create_user(username, email)
+ if password:
+ user.set_password(password)
+ user.save()
+ subscription = {'subscribe': 'y'}
+ email_feeds_form = forms.SimpleEmailSubscribeForm(
+ subscription,
+ frequency = frequency
+ )
+ if email_feeds_form.is_valid():
+ email_feeds_form.save(user)
+ else:
+ raise CommandError('\n'.join(email_feeds_form.errors))
diff --git a/askbot/tests/__init__.py b/askbot/tests/__init__.py
index fa181370..1b049df3 100644
--- a/askbot/tests/__init__.py
+++ b/askbot/tests/__init__.py
@@ -5,3 +5,4 @@ from askbot.tests.permission_assertion_tests import *
from askbot.tests.db_api_tests import *
from askbot.tests.skin_tests import *
from askbot.tests.badge_tests import *
+from askbot.tests.management_command_tests import *
diff --git a/askbot/tests/management_command_tests.py b/askbot/tests/management_command_tests.py
new file mode 100644
index 00000000..41a42a91
--- /dev/null
+++ b/askbot/tests/management_command_tests.py
@@ -0,0 +1,29 @@
+from django.core import management
+from django.contrib import auth
+from askbot.tests.utils import AskbotTestCase
+from askbot import models
+
+class ManagementCommandTests(AskbotTestCase):
+ def test_add_askbot_user(self):
+ username = 'test user'
+ password = 'secretno1'
+ management.call_command(
+ 'add_askbot_user',
+ email = 'test@askbot.org',
+ username = username,
+ frequency = 'd',
+ password = password
+ )
+ #check that we have the user
+ users = models.User.objects.filter(username = username)
+ self.assertEquals(users.count(), 1)
+ user = users[0]
+ #check thath subscrptions are correct
+ subs = models.EmailFeedSetting.objects.filter(
+ subscriber = user,
+ frequency = 'd'
+ )
+ self.assertEquals(subs.count(), 5)
+ #try to log in
+ user = auth.authenticate(username = username, password = password)
+ self.assertTrue(user is not None)
diff --git a/askbot/tests/page_load_tests.py b/askbot/tests/page_load_tests.py
index 2d73b9ea..1c794f7c 100644
--- a/askbot/tests/page_load_tests.py
+++ b/askbot/tests/page_load_tests.py
@@ -1,7 +1,6 @@
from django.test import TestCase, signals
from jinja2.environment import Template as Jinja2Template
from django.template import defaultfilters
-from django.core.management import call_command
from django.core.urlresolvers import reverse
import coffin.template
from askbot import models