def submit(request): def error(msg): request.session.flash('Error: %s' % msg) return HTTPFound(request.route_url('home', _query={'realname': request.params.get('realname', ''), 'username': request.params.get('username', ''), 'text': request.params.get('text', '')})) username = request.params['username'] try: user, groups = login(username, request.params['password']) except: return error('Either your Fedora username or password was incorrect, so we couldn\'t verify your account. Double-check your username, and try typing your password again.') settings = request.registry.settings start_date = datetime.strptime(settings['start_date'], '%Y-%m-%d') creation_date = datetime.strptime(user.creation.split('.')[0].split()[0], '%Y-%m-%d') if creation_date >= start_date: return error('Your Fedora account was created after this contest started. Sorry! Only Fedora accounts created before July 25, 2012 are eligible for this contest.') if 'cla_done' not in groups: return error('You haven\'t signed the Fedora Project Contributor Agreement. <a href="https://admin.fedoraproject.org/accounts/fpca/">Go sign it</a> and come back here to submit your application again.') groups = [group for group in groups if not group.startswith('cla_')] if not groups: return error('You must be a member of at least one ' 'non-CLA/FPCA Fedora Group') if request.params['hardware'] not in settings['hardware'].split(): return error('Invalid hardware specified') if not request.params['country']: return error('You must be a legal resident of one of the listed ' + 'countries to submit an entry.') if request.params['country'] == 'United States': excluded_states = settings['exclude_states'].split() for abbrev, state in us_states(): if request.params['state'] == state: if abbrev in excluded_states: return error('Sorry, ' + state + ' residents are not ' 'eligible for this contest.') break else: return error('You forgot to select what US state you\'re in. We need to know this to determine your eligibility.') if request.params.get('of_age') != 'on': return error('If you\'re not of the age of majority in your region, we can\'t accept your application. Please check off the age checkbox at the bottom of the form if you are at least the stated age.') if user.email.split('@')[1] == settings['prohibited_users']: return error('Red Hat employees are not eligible for this contest, sorry! Didn\'t you read the rules? :) You could order a Gooseberry-Pi off of memo-list...') if DBSession.query(Application).filter_by(username=username).first(): return error('You can only submit one application, sorry! If you submitted any information that is incorrect or if you\'d like to revise your entry, you can email <strong><a mailto="*****@*****.**">[email protected]</a></strong>.') application = Application(username=username, realname=request.params['realname'], hardware=request.params['hardware'], shield=request.params.get('shield', ''), country=request.params['country'], state=request.params.get('state', ''), text=request.params['text']) DBSession.add(application) DBSession.commit() request.session.flash('Your application has been submitted! We\'ll be announcing the winners the week of August 16. You\'ll hear back from us via email during that week - watch for an email from <strong>[email protected]</strong>!') return HTTPFound(request.application_url)
from pyramid.httpexceptions import HTTPFound, HTTPMovedPermanently from pyramid.view import view_config from pyramid_mailer import get_mailer from pyramid_mailer.message import Message from operator import itemgetter from collections import defaultdict from datetime import datetime from sqlalchemy import func from fedora.client import FasProxyClient from webhelpers.constants import us_states, us_territories from .models import DBSession, Application log = logging.getLogger(__name__) us_states_and_territories = sorted(us_states() + us_territories(), key=itemgetter(1)) def login(username, password): fas = FasProxyClient() user = fas.get_user_info({'username': username, 'password': password})[1] roles = [g.name for g in user['approved_memberships']] return user, roles def authorized_admin(request): user = authenticated_userid(request) settings = request.registry.settings if not user: raise Forbidden
def submit(request): def error(msg): request.session.flash('Error: %s' % msg) return HTTPFound(request.route_url('home', _query={'realname': request.params.get('realname', ''), 'username': request.params.get('username', ''), 'text': request.params.get('text', '')})) if not asbool(request.registry.settings['accept_applications']): return error('Applications are no longer accepted') username = request.params['username'] try: user, groups = login(username, request.params['password']) except: return error('Either your Fedora username or password was incorrect, so we couldn\'t verify your account. Double-check your username, and try typing your password again.') settings = request.registry.settings start_date = datetime.strptime(settings['start_date'], '%Y-%m-%d') creation_date = datetime.strptime(user.creation.split('.')[0].split()[0], '%Y-%m-%d') if creation_date >= start_date: return error('Your Fedora account was created after this contest started. Sorry! Only Fedora accounts created before July 25, 2012 are eligible for this contest.') if 'cla_done' not in groups: return error('You haven\'t signed the Fedora Project Contributor Agreement. <a href="https://admin.fedoraproject.org/accounts/fpca/">Go sign it</a> and come back here to submit your application again.') groups = [group for group in groups if not group.startswith('cla_')] if not groups: return error('You must be a member of at least one ' 'non-CLA/FPCA Fedora Group') if request.params['hardware'] not in settings['hardware'].split(): return error('Invalid hardware specified') if not request.params['country']: return error('You must be a legal resident of one of the listed ' + 'countries to submit an entry.') if request.params['country'] == 'United States': excluded_states = settings['exclude_states'].split() for abbrev, state in us_states() + us_territories(): if request.params['state'] == state: if abbrev in excluded_states: return error('Sorry, ' + state + ' residents are not ' 'eligible for this contest.') break else: return error('You forgot to select what US state you\'re in. We need to know this to determine your eligibility.') if request.params.get('of_age') != 'on': return error('If you\'re not of the age of majority in your region, we can\'t accept your application. Please check off the age checkbox at the bottom of the form if you are at least the stated age.') if user.email.split('@')[1] == settings['prohibited_users']: return error('Red Hat employees are not eligible for this contest, sorry! Didn\'t you read the rules? :) You could order a Gooseberry-Pi off of memo-list...') application = DBSession.query(Application).filter_by(username=username).first() updated = False if application: request.session.flash('Your application has been updated!') updated = True else: application = Application(username=username) DBSession.add(application) request.session.flash('Your application has been submitted! We\'ll be ' 'announcing the winners the week of August 16. You\'ll hear ' 'back from us via email during that week - watch for an ' 'email from <strong>%s</strong>!' % settings['admin_email']) application.realname = request.params['realname'] application.hardware = request.params['hardware'] application.shield = request.params.get('shield', '') application.country = request.params['country'] application.state = request.params.get('state', '') application.text = request.params['text'] DBSession.commit() if not updated: mailer = get_mailer(request) subject = "Thanks for entering!" body = """ You have successfully entered into the Fedora Summer of Open Hardware 2012 Contest. Drawings will be held on %s. Your current hardware selection is: %s If you wish to change this, you can go back to the site and resubmit an application, which will update your original submission. Good luck! """ % (settings['stop_date'], application.hardware) message = Message(subject=subject, sender=settings['email_from'], recipients=[user.email], body=body) mailer.send_immediately(message, fail_silently=False) return HTTPFound(request.application_url)