Example #1
0
def forgot():
    if request.method == 'POST':
        un = request.form.get('un',"")
        account = models.Account.pull(un)
        if account is None: account = models.Account.pull_by_email(un)
        if account is None:
            flash('Sorry, your account username / email address is not recognised. Please contact us.')
        else:
            newpass = util.generate_password()
            account.set_password(newpass)
            account.save()

            to = [account.data['email'],app.config['ADMIN_EMAIL']]
            fro = app.config['ADMIN_EMAIL']
            subject = app.config.get("SERVICE_NAME","") + "password reset"
            text = "A password reset request for account " + account.id + " has been received and processed.\n\n"
            text += "The new password for this account is " + newpass + ".\n\n"
            text += "If you are the user " + account.id + " and you requested this change, please login now and change the password again to something of your preference.\n\n"
            
            text += "If you are the user " + account.id + " and you did NOT request this change, please contact us immediately.\n\n"
            try:
                util.send_mail(to=to, fro=fro, subject=subject, text=text)
                flash('Your password has been reset. Please check your emails.')
                if app.config.get('DEBUG',False):
                    flash('Debug mode - new password was set to ' + newpass)
            except:
                flash('Email failed.')
                if app.config.get('DEBUG',False):
                    flash('Debug mode - new password was set to ' + newpass)

    return render_template('account/forgot.html')
Example #2
0
def contact(ad_id):
    advert = models.Advert.pull(ad_id)
    owner = advert.owner
    title = advert.title
    ad_id = advert.id

    form = ContactForm(request.form)

    if request.method == "POST" and form.validate():

        to = [owner, app.config["BCC_EMAIL"]]
        fro = current_user.id
        subject = form.about.data + " on " + app.config.get("SERVICE_NAME", "")
        text = form.message.data
        try:
            util.send_mail(to=to, fro=fro, subject=subject, text=text)
            flash("Email has been sent.")
            if app.config.get("DEBUG", False):
                flash(to[0] + " " + fro + " " + subject + " " + text)
            return redirect(url_for(".details", ad_id=ad_id))
        except Exception as e:
            flash("Hm, sorry - sending the email didn't work.", "error")
            if app.config.get("DEBUG", False):
                flash("Debug mode - email is " + to[0] + " " + fro + " " + subject + " " + text)

    return render_template("advert/contact.html", form=form, advert=advert, owner=owner, ad_id=ad_id, title=title)
Example #3
0
def forgot():
    if request.method == 'POST':
        un = request.form.get('un', "")
        account = models.Account.pull(un)
        if account is None: account = models.Account.pull_by_email(un)
        if account is None:
            flash(
                'Sorry, your account username / email address is not recognised. Please contact us.'
            )
        else:
            newpass = util.generate_password()
            account.set_password(newpass)
            account.save()

            to = [account.data['email'], app.config['ADMIN_EMAIL']]
            fro = app.config['ADMIN_EMAIL']
            subject = app.config.get("SERVICE_NAME", "") + "password reset"
            text = "A password reset request for account " + account.id + " has been received and processed.\n\n"
            text += "The new password for this account is " + newpass + ".\n\n"
            text += "If you are the user " + account.id + " and you requested this change, please login now and change the password again to something of your preference.\n\n"

            text += "If you are the user " + account.id + " and you did NOT request this change, please contact us immediately.\n\n"
            try:
                util.send_mail(to=to, fro=fro, subject=subject, text=text)
                flash(
                    'Your password has been reset. Please check your emails.')
                if app.config.get('DEBUG', False):
                    flash('Debug mode - new password was set to ' + newpass)
            except:
                flash('Email failed.')
                if app.config.get('DEBUG', False):
                    flash('Debug mode - new password was set to ' + newpass)

    return render_template('account/forgot.html')
Example #4
0
def forgot():
    if request.method == 'POST':
        # get hold of the user account
        un = request.form.get('un', "")
        account = models.Account.pull(un)
        if account is None:
            account = models.Account.pull_by_email(un)
        if account is None:
            util.flash_with_url('Your account email address is not recognised.', 'error')
            return render_template('account/forgot.html')

        if account.is_deleted():
            util.flash_with_url('Your account email address is not recognised.', 'error')
            return render_template('account/forgot.html')

        if account.is_banned():
            flash('This account is banned from the service', 'error')
            return render_template('account/forgot.html')

        if not account.data.get('email'):
            util.flash_with_url('Your account does not have an associated email address.', 'error')
            return render_template('account/forgot.html')

        # if we get to here, we have a user account to reset
        reset_token = uuid.uuid4().hex
        account.set_reset_token(reset_token, app.config.get("PASSWORD_RESET_TIMEOUT", 86400))
        account.save()

        sep = "/"
        if request.url_root.endswith("/"):
            sep = ""
        reset_url = request.url_root + sep + "account/reset/" + reset_token

        to = [account.data['email'], app.config['FEEDBACK_EMAIL']]
        fro = app.config['FEEDBACK_EMAIL']
        subject = app.config.get("SERVICE_NAME", "") + " - password reset"
        text = "A password reset request for account '" + account.id + "' has been received and processed.\n\n"
        text += "Please visit " + reset_url + " and enter your new password.\n\n"
        text += "If you are the user " + account.id + " and you requested this change, please visit that link now and set the password to something of your preference.\n\n"
        text += "If you are the user " + account.id + " and you did not request this change, you can ignore this email.\n\n"
        text += "Regards, The UniBoard Team"
        try:
            util.send_mail(to=to, fro=fro, subject=subject, text=text)
            flash('Instructions to reset your password have been sent to you. Please check your emails.', "success")
            if app.config.get('DEBUG', False):
                flash('Debug mode - url for reset is ' + reset_url, "error")
        except Exception as e:
            flash('Hm, sorry - sending the password reset email didn\'t work.', 'error')
            if app.config.get('DEBUG', False):
                flash('Debug mode - url for reset is' + reset_url, "error")
                # app.logger.error(magic + "\n" + repr(e))

    return render_template('account/forgot.html')
Example #5
0
def forgot():
    if request.method == "POST":
        # get hold of the user account
        un = request.form.get("un", "")
        account = models.Account.pull(un)
        if account is None:
            account = models.Account.pull_by_email(un)
        if account is None:
            util.flash_with_url("Hm, sorry, your account username / email address is not recognised.", "error")
            return render_template("account/forgot.html")

        if not account.data.get("email"):
            util.flash_with_url("Hm, sorry, your account does not have an associated email address.", "error")
            return render_template("account/forgot.html")

        # if we get to here, we have a user account to reset
        reset_token = uuid.uuid4().hex
        account.set_reset_token(reset_token, app.config.get("PASSWORD_RESET_TIMEOUT", 86400))
        account.save()

        sep = "/"
        if request.url_root.endswith("/"):
            sep = ""
        reset_url = request.url_root + sep + "account/reset/" + reset_token

        to = [account.data["email"], app.config["ADMIN_EMAIL"]]
        fro = app.config["ADMIN_EMAIL"]
        subject = app.config.get("SERVICE_NAME", "") + " - password reset"
        text = "A password reset request for account '" + account.id + "' has been received and processed.\n\n"
        text += "Please visit " + reset_url + " and enter your new password.\n\n"
        text += (
            "If you are the user '"
            + account.id
            + "' and you requested this change, please visit that link now and set the password to something of your preference.\n\n"
        )
        text += (
            "If you are the user '"
            + account.id
            + "' and you did not request this change, you can ignore this email.\n\n"
        )
        text += "Regards, The OpenDOAR Team"
        try:
            util.send_mail(to=to, fro=fro, subject=subject, text=text)
            flash("Instructions to reset your password have been sent to you. Please check your emails.")
            if app.config.get("DEBUG", False):
                flash("Debug mode - url for reset is " + reset_url)
        except Exception as e:
            flash("Hm, sorry - sending the password reset email didn't work.", "error")
            if app.config.get("DEBUG", False):
                flash("Debug mode - url for reset is" + reset_url)
            # app.logger.error(magic + "\n" + repr(e))

    return render_template("account/forgot.html")
Example #6
0
def mailer():
    if request.method == 'GET':
        pass
    elif request.method == 'POST':
        try:
            if request.values.get('message',False) and not request.values.get('not',False):
                util.send_mail(
                    [app.config['ADMIN_NAME'] + ' <' + app.config['ADMIN_EMAIL'] + '>'],
                    request.values.get('email',app.config['ADMIN_NAME'] + ' <' + app.config['ADMIN_EMAIL'] + '>'),
                    'website enquiry',
                    request.values['message']
                )
                return ''
            else:
                abort(403)
        except:
            abort(500)
Example #7
0
def student():

    # for forms requiring auth, add an auth check here

    if request.method == "GET":
        # TODO: if people are logged in it may be necessary to render a form with previously submitted data
        selections = {
            "schools": dropdowns("school"),
            "subjects": dropdowns("subject"),
            "advancedsubjects": dropdowns("advancedsubject"),
            "levels": dropdowns("level"),
            "grades": dropdowns("grade"),
            "institutions": dropdowns("institution"),
            "advancedlevels": dropdowns("advancedlevel"),
        }
        if current_user.is_anonymous() or not current_user.do_admin:
            if "TEST" in selections["schools"]:
                selections["schools"] = [i for i in selections["schools"] if i != "TEST"]
            if "TEST" in selections["institutions"]:
                selections["institutions"] = [i for i in selections["institutions"] if i != "TEST"]
        response = make_response(render_template("leaps/survey/survey.html", selections=selections, data={}))
        response.headers["Cache-Control"] = "public, no-cache, no-store, max-age=0"
        response.headers["Pragma"] = "no-cache"
        return response

    if request.method == "POST":
        student = models.Student()
        student.save_from_form(request)

        try:
            to = [app.config["LEAPS_EMAIL"]]
            if app.config.get("ADMIN_EMAIL", False):
                to.append(app.config["ADMIN_EMAIL"])
            fro = app.config["LEAPS_EMAIL"]
            subject = "New student survey submitted"
            text = "A student has just submitted a survey. View it in the admin interfacet at "
            text += '<a href="http://leapssurvey.org/admin/student/' + student.id
            text += '">http://leapssurvey.org/admin/student/' + student.id + "</a>."
            util.send_mail(to=to, fro=fro, subject=subject, text=text)
        except:
            flash("Email failed.")

        return redirect(url_for(".complete"))
Example #8
0
def mailer():
    if request.method == 'POST':
        try:
            if request.values.get('message',False) and not request.values.get('not',False):
                util.send_mail(
                    [app.config['ADMIN_NAME'] + ' <' + app.config['ADMIN_EMAIL'] + '>'],
                    request.values.get('email',app.config['ADMIN_NAME'] + ' <' + app.config['ADMIN_EMAIL'] + '>'),
                    'website enquiry',
                    request.values['message']
                )
                flash('Thank you very much for you enquiry. We will get back to you as soon as possible.', 'success')
            else:
                flash('Sorry. Your message could not be delivered. Please try again.', 'error')
        except:
            if app.config.get('DEBUG',False):
                flash('Sorry, Your message failed. Probably because debug.', 'error')
            else:
                flash('Sorry. Your message failed. Please try again', 'error')

    return render_template('contact/index.html') 
Example #9
0
def expire_email(testing=False):
    for item in Advert.get_by_expiration():
        if not item.is_deleted and not item.is_deactivated:
            if item.is_expired:
                print item.owner + " delete"
                if not testing:
                    item.mark_deactivated(True)
                    item.save()
            else:
                print item.owner
                if not testing:
                    activation_link = app.config['LOCALHOST_URL'] + "/advert/" + item.id + "/reactivate"
                    to = [item.owner, app.config['BCC_EMAIL']]
                    fro = app.config['FEEDBACK_EMAIL']
                    subject = app.config.get("SERVICE_NAME", "") + ': ' + item.title + " - expires soon"
                    text = "Hello, " + item.owner + "!\n\n"
                    text += "Your advert " + item.title + " expires soon."
                    text += "Please visit " + activation_link + " if you want to keep it up for another week.\n\n"
                    text += "Regards, The UniBoard Team"

                    util.send_mail(to=to, fro=fro, subject=subject, text=text)
Example #10
0
def register():
    form = RegisterForm(request.form, csrf_enabled=False)

    if request.method == 'POST' and form.validate():
        existing_account = models.Account.pull(form.email.data)
        account = None
        if existing_account is not None:
            if existing_account.is_banned():
                flash('You have been banned from using this service.', "error")
                return render_template('account/register.html', form=form)
            elif existing_account.is_deleted():
                flash('Your old account has been restored. Welcome back!', "success")
                existing_account.set_deleted(False, save=False)
                account = existing_account
                account.clear_password()
            else:
                flash('This account already exists.')
                return redirect(url_for('.forgot'))

        if account is None:
            account = models.Account()

        account.id = form.email.data
        account.set_email(form.email.data)
        account.set_name(form.name.data)

        if form.degree.data:
            account.set_degree(form.degree.data)

        if form.postcode.data:
            account.set_postcode(form.postcode.data)

            results = Geocoder.geocode(form.postcode.data + ', United Kingdom')
            lat, lng = results[0].coordinates
            account.set_location(lat, lng)

        if form.phone.data:
            account.set_phone(form.phone.data)

        if form.graduation.data:
            account.set_graduation(form.graduation.data)

        # automatically set the user role to be "user"
        account.add_role("user")

        activation_token = uuid.uuid4().hex
        account.set_activation_token(activation_token, app.config.get("PASSWORD_ACTIVATE_TIMEOUT", 86400))
        account.save()
        account.refresh()  # refresh the index

        #sending the email with the activation link

        sep = "/"
        if request.url_root.endswith("/"):
            sep = ""
        activation_url = request.url_root + sep + "account/activate/" + activation_token

        to = [account.data['email'], app.config['FEEDBACK_EMAIL']]
        fro = app.config['FEEDBACK_EMAIL']
        subject = app.config.get("SERVICE_NAME", "") + " - new password"
        text = "Welcome to UniBoard, '" + account.email + "'!\n\n"
        text += "Please visit " + activation_url + " to set a password for your account.\n\n"
        text += "Regards, The UniBoard Team"
        try:
            util.send_mail(to=to, fro=fro, subject=subject, text=text)
            flash('Instructions to set up your password have been sent to you. Please check your emails.', "success")
            if app.config.get('DEBUG', False):
                flash('Debug mode - url for activation is ' + activation_url, "error")
        except Exception as e:
            magic = str(uuid.uuid1())
            #util.flash_with_url(
                #'Hm, sorry - sending the password reset email didn\'t work.' + CONTACT_INSTR + ' It would help us if you also quote this magic number: ' + magic + ' . Thank you!',
                #'error')
            if app.config.get('DEBUG', False):
                flash('Debug mode - url for reset is ' + activation_url, "error")
            app.logger.error(magic + "\n" + repr(e))

        return redirect('/account/register')  #TODO should be redirecting somewhere else
    if request.method == 'POST' and not form.validate():
        flash('Please correct the errors', 'error')
    return render_template('account/register.html', form=form)