Ejemplo n.º 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)
Ejemplo n.º 2
0
def profile_search(request, template_values, profile_user):
  """
      Helper function to deal with recent/best
      pages for user.
  """
  get_dict = add_usr_to_query(request.GET,
                              profile_user.username)
  template_values['commprod_timeline'] = commprod_query_manager(
      get_dict, user=request.user)
  template_values['header_classes'] = ''

  return render_to_response('profile_search.html',
                            template_values, request)
Ejemplo n.º 3
0
def profile(request, username):
  """
      User profile page,
      displays avg. overall score + list of commprods
      Profile can be gotten to by user_id, username,
      or an alt_email
  """
  if User.objects.filter(username=username).exists():
    profile_user = User.objects.filter(
        username=username)[0]
    if 'paige' in profile_user.username:
      raise Http404
  else:
    raise Http404

  page_username = get_rand_username(profile_user)

  request_type = request.GET.get('type', '')

  subnav_key, subnav_value, page_title = get_active_page(
      'profile', request_type)

  header = possesive(page_username, page_title)
  title = possesive(profile_user.username, page_title)

  template_values = {
      'page_title': title,
      'nav_profile': 'active',
      subnav_key: subnav_value,
      'header': header,
      'user': request.user,
      'profile_user': profile_user,
      'header-classes': '',
      'floor_percentile': get_floor_percentile(
          profile_user.profile),
      'trend': get_day_trend(profile_user.profile),
      'num_commprods': cpm.CommProd.objects.filter(
          user_profile=profile_user.profile).count(),
      'num_votes': cpm.Rating.objects.filter(
          user_profile=profile_user.profile).count()
  }

  if request_type != '':
    return profile_search(request,
                          template_values, profile_user)
  template_values.update(profile_query_manager(
      request.user, profile_user))
  return render_to_response('profile.html',
                            template_values, request)
Ejemplo n.º 4
0
def reset_password(request):
  errors = {'username': []}  # setup for javascript error setting
  success = False
  if request.POST:
    username = request.POST.get('username', None)
    if not username or not username.strip():
      errors['username'].append(
          'Empty username entered.')

      # make sure user exists
    elif not cpm.UserProfile.objects.filter(
            user__username=username,
            send_mail=True, user__is_active=True).exists():
      errors['username'].append(
          '%s is not registered with an account.' %
          username)

      # create password reset object
    else:
      user_profile = cpm.UserProfile.objects.filter(
          user__username=username)[0]
      reset = cpm.PasswordReset(
          user_profile=user_profile, is_active=True)
      reset.save()
      reset.send_confirm_email()

    if not len(errors['username']):
      success = 'Password reset email sent out!'

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

    return JSONResponse(return_obj)
  else:
    template_values = {
        'page_title': 'Reset Password',
        'hero_err_title': '',
        'user': request.user,
    }

  return render_to_response('reset_password.html',
                            template_values, request)
Ejemplo n.º 5
0
def user_donate(request, template_values):
  """
  Method for dealing with donations from users with accounts.
  Saves the stripe customer id for later use.
  """
  if request.method == 'POST':
    form = DonateForm(request.POST)
    if form.is_valid():
      reason = form.cleaned_data['reason']
      amount = form.cleaned_data['amount']
      is_anonymous = form.cleaned_data['is_anonymous']
      user_profile = request.user.profile

      description = """Donation of $%(amount)s.00
            by %(username)s on %(date)s
            for %(reason)s""" % {
          'amount': amount,
          'username': user_profile.user.username,
          'date': datetime.now(),
          'reason': reason
      }

      # set your secret key: remember to
      # change this to your live secret key in production
      # see your keys here
      # https://manage.stripe.com/account
      stripe.api_key = env['STRIPE_SECRET_KEY']

      # get the credit card details
      # submitted by the form
      token = request.POST.get('stripeToken', '')

      if not token:
        return redirect('/donate')

      # charge the Customer
      stripe.Charge.create(
          amount=amount * 100,  # in cents
          currency='usd',
          card=token,
          description=description
      )

      # charge has gone through successfully
      donation = dm.Donation(reason=reason,
                             amount=amount,
                             is_anonymous=is_anonymous,
                             user_profile=user_profile)
      donation.save()

      template_values = {
          'amount': amount,
          'reason': reason,
      }

      return render_to_response(
          'donations/donate_success.html',
          template_values, request)

  else:
    form = DonateForm()

  template_values.update({
      'form': form,
  })

  return render_to_response(
      'donations/donate.html', template_values, request)
Ejemplo n.º 6
0
def anon_donate(request, template_values):
  """
      Method for dealing with donations
       from users without accounts.
       Does not save a stripe customer id
       and just charges the card directly.
  """
  if request.method == 'POST':
    form = AnonDonateForm(request.POST)
    if form.is_valid():
      reason = form.cleaned_data['reason']
      amount = form.cleaned_data['amount']
      name = form.cleaned_data['name']

      description = """AnonDonation by %(name)s of
                $%(amount)s.00 on %(date)s for %(reason)s""" % {
          'name': name,
          'amount': amount,
          'date': datetime.now(),
          'reason': reason,
      }

      # set your secret key: remember to
      # change this to your live secret key in production
      # see your keys here
      # https://manage.stripe.com/account
      stripe.api_key = env['STRIPE_SECRET_KEY']

      # get the credit card details submitted
      # by the form
      token = request.POST.get('stripeToken', '')

      if not token:
        return redirect('/donate')

      # charge the Customer
      stripe.Charge.create(
          amount=amount * 100,  # in cents
          currency='usd',
          card=token,
          description=description
      )

      # charge has gone through successfully
      donation = dm.AnonDonation(name=name,
                                 reason=reason, amount=amount)
      donation.save()

      template_values = {
          'amount': amount,
          'reason': reason,
      }

      return render_to_response('donations/donate_success.html',
                                template_values,
                                request)

  else:
    form = AnonDonateForm()

  template_values.update({
      'form': form,
  })

  return render_to_response('donations/donate.html',
                            template_values, request)
Ejemplo n.º 7
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)