Exemplo n.º 1
0
def edit_account(request, account_id):
    account = get_object_or_404(Account, pk=account_id)

    if not account.can_edit(request):
        return HttpResponseForbidden(
            '<h1>Access Denied</h1>'
            '<p>You do not have permission to edit details '
            'of this account.</p>')

    if common.is_admin(request):
        person = account.person
        username = account.username
        form = AdminAccountForm(
            data=request.POST or None,
            instance=account, person=person, initial={'username': username})
    else:
        person = request.user
        assert account.person == person
        form = UserAccountForm(
            data=request.POST or None, instance=account)

    if request.method == 'POST':
        if form.is_valid():
            account = form.save()
            person = account.person
            return HttpResponseRedirect(person.get_absolute_url())

    return render_to_response(
        'karaage/machines/account_form.html',
        {'form': form, 'person': person, 'account': account},
        context_instance=RequestContext(request))
Exemplo n.º 2
0
def edit_account(request, account_id):
    account = get_object_or_404(Account, pk=account_id)

    if not account.can_edit(request):
        return HttpResponseForbidden(
            '<h1>Access Denied</h1>'
            '<p>You do not have permission to edit details '
            'of this account.</p>')

    if common.is_admin(request):
        person = account.person
        username = account.username
        form = AdminAccountForm(data=request.POST or None,
                                instance=account,
                                person=person,
                                initial={'username': username})
    else:
        person = request.user
        assert account.person == person
        form = UserAccountForm(data=request.POST or None, instance=account)

    if request.method == 'POST':
        if form.is_valid():
            account = form.save()
            person = account.person
            return HttpResponseRedirect(person.get_absolute_url())

    return render_to_response('karaage/machines/account_form.html', {
        'form': form,
        'person': person,
        'account': account
    },
                              context_instance=RequestContext(request))
Exemplo n.º 3
0
 def test_valid_data(self):
     form_data = self._valid_form_data()
     form_data['username'] = '******'
     form = AdminAccountForm(person=self.account.person,
                             data=form_data,
                             instance=self.account)
     self.assertEqual(form.is_valid(), True, form.errors.items())
     form.save()
     self.assertEqual(self.account.username, 'test-account')
Exemplo n.º 4
0
 def test_valid_data(self):
     form_data = self._valid_form_data()
     form_data['username'] = '******'
     form = AdminAccountForm(person=self.account.person,
                             data=form_data,
                             instance=self.account)
     self.assertEqual(form.is_valid(), True, form.errors.items())
     form.save()
     self.assertEqual(self.account.username, 'test-account')
Exemplo n.º 5
0
 def test_upper_username(self):
     form_data = self._valid_form_data()
     form_data['username'] = '******'
     form = AdminAccountForm(person=self.account.person,
                             data=form_data,
                             instance=self.account)
     self.assertEqual(form.is_valid(), False)
     self.assertEqual(
         form.errors.items(),
         dict.items({'username':
                     [six.u('Username must be all lowercase')]}))
Exemplo n.º 6
0
 def test_upper_username(self):
     form_data = self._valid_form_data()
     form_data['username'] = '******'
     form = AdminAccountForm(person=self.account.person,
                             data=form_data,
                             instance=self.account)
     self.assertEqual(form.is_valid(), False)
     self.assertEqual(
         form.errors.items(),
         [('username',
           [u'Username must be all lowercase'])])
Exemplo n.º 7
0
 def test_long_username(self):
     form_data = self._valid_form_data()
     form_data['username'] = '******' * 100
     form = AdminAccountForm(person=self.account.person,
                             data=form_data,
                             instance=self.account)
     self.assertEqual(form.is_valid(), False)
     self.assertEqual(
         form.errors.items(),
         [('username',
           [u'Ensure this value has at '
            u'most 255 characters (it has 400).'])])
Exemplo n.º 8
0
 def test_invalid_usernamen(self):
     form_data = self._valid_form_data()
     form_data['username'] = '******'
     form = AdminAccountForm(person=self.account.person,
                             data=form_data,
                             instance=self.account)
     self.assertEqual(form.is_valid(), False)
     self.assertEqual(
         form.errors.items(),
         [('username',
           [(u'Usernames can only contain '
             u'letters, numbers and underscores')])])
Exemplo n.º 9
0
 def test_invalid_usernamen(self):
     form_data = self._valid_form_data()
     form_data['username'] = '******'
     form = AdminAccountForm(person=self.account.person,
                             data=form_data,
                             instance=self.account)
     self.assertEqual(form.is_valid(), False)
     self.assertEqual(
         form.errors.items(),
         dict.items({
             'username': [(six.u('Usernames can only contain '
                                 'letters, numbers and underscores'))]
         }))
Exemplo n.º 10
0
 def test_long_username(self):
     form_data = self._valid_form_data()
     form_data['username'] = '******' * 100
     form = AdminAccountForm(person=self.account.person,
                             data=form_data,
                             instance=self.account)
     self.assertEqual(form.is_valid(), False)
     self.assertEqual(
         form.errors.items(),
         dict.items({
             'username': [
                 six.u('Ensure this value has at '
                       'most 255 characters (it has 400).')
             ]
         }))
Exemplo n.º 11
0
def add_account(request, username=None):
    person = get_object_or_404(Person, username=username)
    account = None

    form = AdminAccountForm(
        data=request.POST or None,
        instance=account, person=person, initial={'username': username})

    if request.method == 'POST':
        if form.is_valid():
            account = form.save()
            person = account.person
            return HttpResponseRedirect(person.get_absolute_url())

    return render_to_response(
        'karaage/machines/account_form.html',
        {'form': form, 'person': person, 'account': account},
        context_instance=RequestContext(request))
Exemplo n.º 12
0
def add_account(request, username=None):
    person = get_object_or_404(Person, username=username)
    account = None

    form = AdminAccountForm(
        data=request.POST or None,
        instance=account, person=person, initial={'username': username})

    if request.method == 'POST':
        if form.is_valid():
            account = form.save()
            person = account.person
            return HttpResponseRedirect(person.get_absolute_url())

    return render(
        template_name='karaage/machines/account_form.html',
        context={'form': form, 'person': person, 'account': account},
        request=request)