def signup(request, template_name='signup.html', *args, **kwargs): me = 'people' error = pop_error(request) data = { 'me': me, 'error': '', 'form': SignupForm() } if request.method == 'POST': form = SignupForm(request.POST, request.FILES) print 'testing' print request.FILES print form.errors if form.is_valid(): print 'testing' username = asciify(form.cleaned_data['username']) password = form.cleaned_data['password2'] email = form.cleaned_data['email'].strip() or '' firstName = asciify(form.cleaned_data['first']) lastName = asciify(form.cleaned_data['last']) store_picture(request.FILES['picture']) # check that username not taken userslug = slugify(username) Profile = get_profile_model(raise_on_error=False) if Profile.objects.filter(username=userslug).count(): data['error'] = u'Username "{}" is taken'.format(userslug) # error! #return HttpResponseRedirect('user:signup') ''' safe_username = slugify('%s-%s' % (username, str(tznow()))) changed_warningmsg = errormsg + ", changed it to '%s'." messages.warning(request, changed_warningmsg % (username, safe_username)) username = safe_username ''' # make user else: try: user = make_user(firstName, lastName,username, password, email=email, request=request) except NanoUserExistsError: next_profile = Profile.objects.get(user=user).get_absolute_url() return HttpResponseRedirect(next_profile) else: # fake authentication, avoid a db-lookup/thread-trouble/ # race conditions user.backend = 'django.contrib.auth.backends.ModelBackend' _LOG.debug('Attempting login of: %s' % user) login(request, user) nexthop = getattr(settings, 'NANO_USER_SIGNUP_NEXT', reverse('user:nano_user_signup_done')) ''' try: nexthop_profile = Profile.objects.get(user=user).get_absolute_url() return HttpResponseRedirect(nexthop_profile) except Profile.DoesNotExist: pass ''' return HttpResponseRedirect(nexthop) _LOG.debug('Should never end up here') return render(request, template_name, data)
def make_user(username, password, email=None, request=None): User = get_user_model() try: User.objects.get(username=username) except User.DoesNotExist: # make user user = User(username=username[:30]) user.set_password(password) user.is_staff = False user.is_superuser = False user.is_active = True if email: user.email = email user.save() # Create profile Profile = get_profile_model(raise_on_error=False) if Profile: profile = Profile(user=user, display_name=username) profile.save() # Don't signal creation of test users test_users = getattr(settings, 'NANO_USER_TEST_USERS', ()) for test_user in test_users: if user.username.startswith(test_user): break else: new_user_created.send(sender=User, user=user) if request is not None: infomsg = 'You\'re now registered, as "%s"' % username messages.info(request, infomsg) _LOG.debug('Created user: %s/%s' % (user, user.check_password(password))) return user else: raise NanoUserExistsError("The username '%s' is already in use by somebody else" % username)
def timetravellers(): Profile = get_profile_model() badge = Badge.objects.get(name='Timetraveller') timetravellers = [ profile.user for profile in Profile.objects.all() if profile.seen_ipv6 ] batchbadge(badge, timetravellers)
def autobiographers(): User = get_user_model() Profile = get_profile_model() badge = Badge.objects.get(name='Autobiographer') autobiographer_profiles = Profile.objects.autobiographers() autobiographers = User.objects.filter( id__in=[p.user_id for p in autobiographer_profiles]) batchbadge(badge, autobiographers)
def unlurk(): Profile = get_profile_model() brs = Badge.objects.get_all_recipients().filter(profile__is_lurker=True) unlurked = Profile.objects.filter(user__in=brs) exlurker_count = unlurked.count() unlurked.update(is_lurker=False) # Right way: method on Profile.objects that sends this signal for ul in unlurked: user_unlurked.send(sender=ul, user=ul.user) return exlurker_count
def signup(request, template_name='signup.html', *args, **kwargs): me = 'people' error = pop_error(request) data = {'me': me, 'error': error, 'form': SignupForm()} if request.method == 'POST': form = SignupForm(data=request.POST) if form.is_valid(): username = asciify(form.cleaned_data['username']) password = form.cleaned_data['password2'] email = form.cleaned_data['email'].strip() or '' errormsg = 'Username "%s" is taken' # check that username not taken userslug = slugify(username) Profile = get_profile_model(raise_on_error=False) if Profile.objects.filter(slug=userslug).count(): # error! safe_username = slugify('%s-%s' % (username, str(tznow()))) changed_warningmsg = errormsg + ", changed it to '%s'." messages.warning( request, changed_warningmsg % (username, safe_username)) username = safe_username # make user try: user = make_user(username, password, email=email, request=request) except NanoUserExistsError: next_profile = Profile.objects.get( user=user).get_absolute_url() return HttpResponseRedirect(next_profile) else: # fake authentication, avoid a db-lookup/thread-trouble/ # race conditions user.backend = 'django.contrib.auth.backends.ModelBackend' _LOG.debug('Attempting login of: %s' % user) login(request, user) nexthop = getattr(settings, 'NANO_USER_SIGNUP_NEXT', reverse('nano_user_signup_done')) try: nexthop_profile = Profile.objects.get( user=user).get_absolute_url() return HttpResponseRedirect(nexthop_profile) except Profile.DoesNotExist: pass return HttpResponseRedirect(nexthop) _LOG.debug('Should never end up here') return render(request, template_name, data)
def make_user(username, password, email=None, request=None): User = get_user_model() try: User.objects.get(username=username) except User.DoesNotExist: # make user user = User(username=username[:30]) user.set_password(password) user.is_staff = False user.is_superuser = False user.is_active = True if email: user.email = email user.save() # Create profile Profile = get_profile_model(raise_on_error=False) if Profile: profile = Profile(user=user, display_name=username) profile.save() # Don't signal creation of test users test_users = getattr(settings, 'NANO_USER_TEST_USERS', ()) for test_user in test_users: if user.username.startswith(test_user): break else: new_user_created.send(sender=User, user=user) if request is not None: infomsg = 'You\'re now registered, as "%s"' % username messages.info(request, infomsg) _LOG.debug('Created user: %s/%s' % (user, user.check_password(password))) return user else: raise NanoUserExistsError( "The username '%s' is already in use by somebody else" % username)
from django.contrib.auth.models import User from django.core.mail import send_mail from django.core.urlresolvers import reverse from django.http import HttpResponseRedirect from django.shortcuts import get_object_or_404, render from django.template.defaultfilters import slugify from nano.tools import pop_error, get_profile_model, asciify from nano.user.forms import SignupForm, PasswordChangeForm, PasswordResetForm from nano.user import new_user_created import logging _LOG = logging.getLogger(__name__) Profile = get_profile_model(raise_on_error=False) class NanoUserError(Exception): pass class NanoUserExistsError(NanoUserError): pass # def pop_error(request): # error = request.session.get('error', None) # if 'error' in request.session: # del request.session['error'] # return error def random_password(): sample_space = string.letters + string.digits + r'!#$%&()*+,-.:;=?_'
from django.conf import settings from django.contrib import auth, messages from django.contrib.auth.decorators import login_required from django.contrib.auth.models import User from django.core.mail import send_mail from django.core.urlresolvers import reverse from django.http import HttpResponseRedirect from django.shortcuts import get_object_or_404 from django.template.defaultfilters import slugify from nano.tools import pop_error, render_page, get_profile_model, asciify from nano.user.forms import * from nano.user import new_user_created Profile = get_profile_model() class NanoUserError(Exception): pass class NanoUserExistsError(NanoUserError): pass # def pop_error(request): # error = request.session.get('error', None) # if 'error' in request.session: # del request.session['error'] # return error def random_password(): sample_space = string.letters + string.digits + r'!#$%&()*+,-.:;=?_'