Beispiel #1
0
def register(request, key):
  """
      Registration page. Visitor arrives wih activation key
  """
  profile = cpm.UserProfile.objects.filter(
      activation_key=key)

  if not profile.exists() or profile[0].user.is_active:
    hero_title = 'Hmm... that registration key is invalid.'
    return render_err_msg(request, hero_title)

  user = profile[0].user

  if request.POST:
    reg_form = RegForm(request.POST)
    if reg_form.is_valid():
      user.is_active = True
      user.first_name = reg_form.cleaned_data['first_name']
      user.last_name = reg_form.cleaned_data['last_name']
      user.set_password(reg_form.cleaned_data['password'])

      pic_url = put_profile_pic(
          reg_form.cleaned_data['pic_url'], user.profile)
      if pic_url:
        user.profile.pic_url = pic_url

      user.profile.class_year = reg_form.cleaned_data['class_year']

      alt_emails = request.POST.getlist('alt_email')
      for alt_email in alt_emails:
        if alt_email:
          user.profile.add_email(alt_email)

      user.save()
      user.profile.save()

      user = auth.authenticate(username=user.username,
                               password=reg_form.cleaned_data['password'])
      if user is not None:
        if user.is_active:
          auth.login(request, user)
          # Redirect to a success page.
          return redirect('/')

  else:
    reg_form = RegForm()

  template_values = {
      'page_title': 'register',
      'form': reg_form,
      'user': user,
  }

  return render_to_response('register.html',
                            template_values, request)
Beispiel #2
0
def edit_profile(request):
  """
      Edit profile page
  """

  user = request.user
  profile = user.profile
  # update for post request

  if request.POST and request.is_ajax():
    success = False
    errors = {}
    request_type = request.POST.get('form_type', None)
    if request_type == 'password':
      password = request.POST.get(
          'current_password', None)
      new_password = request.POST.get(
          'new_password', None)
      new_password_confirm = request.POST.get(
          'new_password_confirm', None)

      if user.check_password(password):
        if new_password is not None \
                and new_password == new_password_confirm:
          user.set_password(new_password)
          success = 'Password changed'
          user.save()
        else:
          errors['password'] = ['Passwords don\'t match.']
      else:
        errors['password'] = ['Incorrect password.']

    elif request_type == 'shirt_name':
      try:
        # delete all current shirt names
        cpm.ShirtName.objects.filter(
            user_profile=profile,
            editable=True).delete()

        # add in all new shirt names
        shirt_names = request.POST.getlist(
            'shirt_name')
        for name in shirt_names:
          name = name.strip()
          if not name:
            cpm.ShirtName(
                user_profile=profile,
                name=name).save()
        success = 'Shirt names added!'
      except:
        errors['shirt_name'] = [
            'Oops -- something went wrong.']

    elif request_type == 'email':
      emails = request.POST.getlist('email')
      errors['email'] = []

      for email in emails:
        # makes sure email
        if not validate_email(email):
          if not email.strip():
            errors['email'].append(
                'Empty email entered.')
          else:
            errors['email'].append(
                '%s is not a valid email.' %
                email)

        # make sure email doesn't exists
        elif cpm.UserProfile.objects.filter(
                email__email=email,
                email__confirmed=True).exists() \
                or cpm.UserProfile.objects.filter(
                user__email=email,
                send_mail=True).exists():
          errors['email'].append("""%s is already
                     re\gistered with an account.""" % email)

      if not errors['email']:
        for email in emails:
          profile.add_email(email)
        success = 'Confirmation emails sent out!'

    elif request_type == 'pic':
      pic_url = request.POST.get('pic_url')
      # download and upload to our S3
      pic_url = put_profile_pic(
          pic_url, user.profile)
      if pic_url:  # no errors/less than 1mb #patlsotw
        user.profile.pic_url = pic_url
        user.profile.save()
        success = 'Profile picture changed!'
      else:
        errors['pic'] = [
            'Oops -- something went wrong.']

    return_obj = {
        'success': success,
        'errors': errors
    }

    return JSONResponse(return_obj)

  # not post request
  passwordForm = [
      {
          'name': 'current_password',
          'placeholder': 'Current password'
      },
      {
          'name': 'new_password',
          'placeholder': 'New password'
      },
      {
          'name': 'new_password_confirm',
          'placeholder': 'Confirm new password'
      }
  ]

  shirtNameForm = []
  for name in cpm.ShirtName.objects.filter(
          user_profile=profile).values_list('name', flat=True):
    field = {
        'name': 'shirt_name',
        'placeholder': 'Shirt name',
        'value': name
    }
    shirtNameForm.append(field)

  emailForm = [{'value': user.email}]
  for email in cpm.Email.objects.filter(
          user_profile=profile,
          confirmed=True).values_list('email', flat=True):
    field = {
        'value': email
    }
    emailForm.append(field)

  template_values = {
      'page_title': 'Edit Profile',
      'nav_account': 'active',
      'user': request.user,
      'password': passwordForm,
      'shirtname': shirtNameForm,
      'email': emailForm
  }

  return render_to_response('edit_profile.html',
                            template_values, request)