예제 #1
0
def oauth_callback(provider, user_type=1):
    if not current_user.is_anonymous():
        return redirect(url_for('index'))
    oauth = OAuthSignIn.get_provider(provider)
    email, first_name, last_name = oauth.callback()
    if email is None:
        flash(
            'Facebook authentication failed. Try again or register via email.')
        return redirect(url_for('home.register'))
    user = User.query.filter_by(email=email).first()
    if not user:
        user = User(first_name=first_name,
                    last_name=last_name,
                    email=email,
                    password='',
                    user_type=1)
        db.session.add(user)
        if user_type == 1:
            student = Student(user_id=User.query.filter_by(
                email=email).first().id)
            db.session.add(student)
        if user_type == 2:
            donor = Donor(user_id=User.query.filter_by(email=email).first().id)
            db.session.add(donor)
        db.session.commit()
    login_user(user, remember=False)
    return redirect(url_for('home.index'))
예제 #2
0
def donation(request):
    context_dict = {}
    if request.POST:
        try:

            if Donation.objects.filter():
                donation = Donation.objects.filter()[0]
            else:
                donation = Donation()
                donation.name = "Corona virus fundraising"
                donation.save()

            donor = Donor()
            donor.full_name = request.POST['firstname'] + " " + request.POST[
                'lastname']
            donor.amount = request.POST['amount']
            donor.email = request.POST['email']
            donor.phone_number = request.POST['phonenumber']
            donor.save()

            donorDonation = DonorDonation()
            donorDonation.donation = donation
            donorDonation.donor = donor

            return redirect('/')
        except:
            render(request, 'app/donate.html', context_dict)
    else:
        return render(request, 'app/donate.html', context_dict)
예제 #3
0
def createdonor():
    """
    @api {post} /donor Add a new donor
    @apiVersion 1.0.0
    @apiName createdonor
    @apiGroup Donor

    @apiParam {String}      first_name      The first name of the Donor.
    @apiParam {String}      last_name       the last name of the Donor.
    @apiParam {String}      email           email of Donor.
    @apiParam {String}      phone_no        phone number of Donor.
    @apiParam {String}      password        password of Donor.
    @apiParam {String}      organisation    organisation of Donor.
    @apiParam {String}      city            city name(part of address)
    @apiParam {String}      street          street number(part of address)
    @apiParam {String}      landmark        landmark description(part of address)
    @apiParam {String}      country         country name(part of address)

    @apiSuccess {String}    message         donor added to database

    @apiError               message         Donor with that email already exists!
    @apiError               message[2]         address street not provided
    @apiError               message[3]         not json
    """
    donor = request.json
    if not donor:
        return jsonify({"message": "not json"}), 400
    if not donor.get("street"):
        return jsonify({"message": "address street not provided"}), 400
    print(donor)
    check_donor = Donor.query.filter_by(email=donor.get('email')).first()
    if check_donor:
        return jsonify({'message': 'Donor with that email already exists!'})
    password_hash = bcrypt.generate_password_hash(
        donor.get('password')).decode('utf-8')
    username = donor.get('email').split('@')[0]
    check_username = username_in_database_donor(username)
    if check_username:
        while check_username:
            username = username + '1'
            check_username = username_in_database_donor(username)
    u = Donor(first_name=donor.get('first_name'),
              last_name=donor.get('last_name'),
              email=donor.get('email'),
              phone_no=donor.get('phone_no'),
              username=username,
              password_hash=password_hash,
              organisation=donor.get('organisation'))
    address = Address(donor=u,
                      city=donor.get('city'),
                      street=donor.get('street'),
                      country=donor.get('country'),
                      landmark=donor.get('landmark'))
    db.session.add(address)
    db.session.add(u)
    db.session.commit()
    return jsonify({'message': 'Donor added to database'}), 200
예제 #4
0
def test_donor_with_complete_fields():
    test_donor = Donor(donor_name='Tesla', email='*****@*****.**', want_receipt=True,
                       telephone_number='604-123-5678', mobile_number='604-123-5678',
                       address_line='Mars street', city='Tokyo',
                       province='Japan', postal_code='123123', verified=True)
    test_donor.save()

    response = Donor.objects.get(donor_name='Tesla')
    assert response.donor_name == 'Tesla'
    assert response.email == '*****@*****.**'
    assert response.want_receipt == True
    assert response.telephone_number == '604-123-5678'
    assert response.mobile_number == '604-123-5678'
    assert response.address_line == 'Mars street'
    assert response.city == 'Tokyo'
    assert response.province == 'Japan'
    assert response.postal_code == '123123'
    assert response.verified == True
예제 #5
0
def insert_donor():
    form = InsertForm()
    # If form is valid, post it and return the updated table
    # on account page
    if form.validate_on_submit():
        # add donor to database
        donor = Donor(name=form.name.data,
                      contact_email=form.contact_email.data,
                      donation_amount=form.donation_amount.data,
                      donate_event=form.donate_event.data,
                      admin=current_user)
        db.session.add(donor)
        db.session.commit()
        flash(' A new person data was added', 'success')
        return redirect(url_for('users.account'))
    return render_template('insert_donor.html',
                           form=form,
                           title=' Add Donor',
                           legend='Add Donor')
예제 #6
0
def register():
    # if already signed in take to homepage
    if current_user.is_authenticated():
        return redirect(url_for('home.index'))

    form = RegisterForm(request.form)
    if form.validate_on_submit():
        if form.user_type.data == 1:
            user = User(first_name=form.first_name.data,
                        last_name=form.last_name.data,
                        email=form.email.data,
                        password=generate_password_hash(form.password.data),
                        user_type=1)
        elif form.user_type.data == 2:
            user = User(first_name=form.first_name.data,
                        last_name=form.last_name.data,
                        email=form.email.data,
                        password=generate_password_hash(form.password.data),
                        user_type=2)

        try:
            db.session.add(user)
            db.session.commit()
        except IntegrityError:
            flash('An account with this email already exists.')
        else:
            if form.user_type.data == 1:
                student = Student(user_id=User.query.filter_by(
                    email=form.email.data).first().id)
                db.session.add(student)
            elif form.user_type.data == 2:
                donor = Donor(user_id=User.query.filter_by(
                    email=form.email.data).first().id)
                db.session.add(donor)
            db.session.commit()
            login_user(user, remember=False)  # TODO: add remember me
            flash('Thanks for registering')
            return redirect(url_for('home.index'))

    return render_template("home/register.html", form=form)
예제 #7
0
def createdonor(module):
    donor_pure = request.json
    donor = donor_pure.get('form')
    if not donor:
        return jsonify({"message": "not json"}), 400
    if not donor.get("street"):
        return jsonify({"message": "address street not provided"}), 400
    print(donor)
    check_donor = Donor.query.filter_by(email=donor.get('email')).first()
    if check_donor:
        return jsonify({'message': 'Donor with that email already exists!'})
    # modules = Modules.query.all()
    # # if module not in modules:           # TODO: check if this works
    #     return jsonify({'message': 'This module does not exist'})
    password_hash = bcrypt.generate_password_hash(
        donor.get('password')).decode('utf-8')
    username = donor.get('email').split('@')[0]
    check_username = username_in_database_donor(username)
    if check_username:
        while check_username:
            username = username + '1'
            check_username = username_in_database_donor(username)
    u = Donor(first_name=donor.get('first_name'),
              last_name=donor.get('last_name'),
              email=donor.get('email'),
              phone_no=donor.get('phone_no'),
              username=username,
              password_hash=password_hash,
              module=donor_pure.get('module'),
              organisation=donor.get('organisation'))
    address = Address(donor=u,
                      city=donor.get('city'),
                      street=donor.get('street'),
                      country=donor.get('country'),
                      landmark=donor.get('landmark'))
    db.session.add(address)
    db.session.add(u)
    db.session.commit()
    return jsonify({'message': 'Donor added to database'}), 200
예제 #8
0
def signup(request):
    context_dict = {}

    # If the user is logged in redirect to the app dashboard
    if request.user.is_authenticated:
        return redirect("app:app")

    # Check if the request is Post, new user is being registered
    if request.method == 'POST':
        qd = dict(request.POST)

        # Check if it is Donor or Hospital
        if 'username' in qd:
            for k, v in qd.items():
                qd[k] = v[0]

            new_donor = Donor()

            # Create a new donor instance
            r = new_donor.new_donor(data=qd)
            # Handle any errors which may occur
            if r['error'] is None:
                return JsonResponse({
                    'success':
                    True,
                    'message':
                    "Account was successfully created. You can now log in!"
                })
            else:
                return JsonResponse({'success': False, 'message': r['error']})

        elif 'hospital_name' in qd:
            for k, v in qd.items():
                qd[k] = v[0]

            # Create a new hospital instance
            new_hosp = Hospital()
            # Handle any errors which may occur
            r = new_hosp.new_hospital(data=qd)
            if r['error'] is None:
                return JsonResponse({
                    'success':
                    True,
                    'message':
                    "Account was successfully created. You can now log in!"
                })
            else:
                return JsonResponse({'success': False, 'message': r['error']})

    # Use this to have a link choosing the Hospital form directly
    if request.method == 'GET':
        # Check if GET parameter has been used in the url to show hospital sign up form directly
        hospital = request.GET.get('hospital', '')
        if hospital == "true":
            context_dict['hospital'] = "true"
            response = render(request, 'app/signup.html', context=context_dict)

    else:
        return render(request, 'app/signup.html')

    response = render(request, 'app/signup.html', context=context_dict)

    # Return a rendered response to send to the client.
    return response
예제 #9
0
def test_invalid_postal_code():
    test_donor = Donor(donor_name='Kobayashi',
                       postal_code='12345678123afasdf', want_receipt=False)
    test_donor.save()
    response = Donor.objects.get(donor_name='Kobayashi')
    assert len(response.postal_code) == 7
예제 #10
0
def test_donor_null_for_want_receipt():
    with pytest.raises(IntegrityError):
        test_donor = Donor(donor_name='Tohru', telephone_number='123-333-3333')
        test_donor.save()
예제 #11
0
def test_donor_bad_phonenumber():
    # DOES NOT THROW ANYTHING.
    test_donor = Donor(donor_name='Zavala',
                       telephone_number='111122223333123', want_receipt=False)
    test_donor.save()