def create_scheduleitem(event_id): user_id = auth.check_token( request.headers.get('session') ) if not user_id: return "Unauthorized request: Bad session token", 401 organizer = Organizer.find_id( user_id ) if not organizer: return "Unauthorized request: User doesn't have permission", 401 event = Event.find_event( event_id ) if not event: return "Event not found", 404 body = request.get_json() schedule = ScheduleItem( title=body.get('title'), location=body.get('location'), time = dateParser.parse( body.get('time') ) ) event.schedule.append(schedule) event.save() event.reload() return event.to_json()
def update_scheduleitem(event_id, index): index = int(index) user_id = auth.check_token( request.headers.get('session') ) if not user_id: return "Unauthorized request: Bad session token", 401 user = Organizer.find_id( user_id ) if not user: return "Unauthorized request: User doesn't have permission", 401 event = Event.find_event( event_id ) if not event: return "Event not found", 404 if not (0 <= index < len(event.schedule)): return "Schedule Item not found", 404 schedule = event.schedule[index] for key, value in request.get_json().items(): if not key.startswith('_'): # Some security setattr(schedule, key, value) event.schedule[index] = schedule; return event.to_json()
def create_post(event_id): user_id = auth.check_token(request.headers.get('session')) if not user_id: return "Unauthorized request: Bad session token", 401 organizer = Organizer.find_id(user_id) if not organizer: return "Unauthorized request: User doesn't have permission", 401 event = Event.find_event(event_id) if not event: return "Event not found", 404 body = request.get_json() post = Post() post.event = event post.author = organizer post.time = datetime.utcnow() post.image = body.get('image') post.title = body.get('title') post.body = body.get('body') post.notif = body.get('notif') post.save() if not post.id: return "Error creating post", 500 # If post.notif, send text message to all attendees return post.to_json()
def get_attendees(event_id, attendee_type): user_id = auth.check_token(request.headers.get('session')) if not user_id: return "Unauthorized request: Bad session token", 401 user = Organizer.find_id(user_id) if not user: return "Unauthorized request: User doesn't have permission", 401 if not event_id: return "Event ID required", 400 event = Event.find_event(event_id) if not event: return "Event not found", 404 attendee_type = attendee_type.lower() if attendee_type not in ['attendees', 'students', 'mentors', 'organizers']: return "Invalid Attendee Type", 404 attendees = None if attendee_type == 'attendees': attendees = { 'students': [], 'mentors': [], 'organizers': [], 'checkins': [] } for usr in User.objects(events=event.id): if usr.type in ['student', 'mentor', 'organizer']: attendees[usr.type + 's'].append(usr.to_dict()) else: if not attendees['other']: attendees['other'] = [] attendees['other'].append(usr.to_dict()) for usr in User.objects(checkins=event.id): attendees['checkins'].append(usr.to_dict()) else: attendees = [] attendee_cls = None if attendee_type == 'students': attendee_cls = Student elif attendee_type == 'mentors': attendee_cls = Mentor elif attendee_type == 'organizers': attendee_cls = Organizer else: attendee_cls = User for usr in attendee_cls.objects(events=event.id): attendees.append(usr.to_dict()) return json.dumps(attendees), 200, jsonType
def remove_event(event_id): user_id = auth.check_token(request.headers.get('session')) if not user_id: return "Unauthorized request: Bad session token", 401 user = Organizer.find_id(user_id) if not user: return "Unauthorized request: User doesn't have permission", 401 event = Event.find_event(event_id) if not event: return "Event not found", 404 event.delete() return 'Event deleted'
def unregister(event_id): user_id = auth.check_token(request.headers.get('session')) if not user_id: return "Unauthorized request: Bad session token", 401 user = User.find_id(user_id) if not user: return "Unauthorized request: User doesn't have permission", 401 event = Event.find_event(event_id) if not event: return "Event not found", 404 if event in user.events: user.events.remove(event) user.save() return json.dumps({"status": "removed"}), 200, jsonType
def create_project(): user_id = auth.check_token(request.headers.get('session')) if not user_id: return "Unauthorized request: Bad session token", 401 user = User.find_id(user_id) if not user: return "User not found", 404 project = Project() event_id = request.json.get('event') if not event_id: return "Event is required", 400 event = Event.find_event(event_id) if not event: return "Event not found", 404 if not (event in user.events or event.id in user.events): return "User not attending event", 400 teammate_email = request.json.get('teammate') # A team is required if not teammate_email: return "Teammate email is required", 400 teammate = User.objects(email=teammate_email).first() if not teammate: return "Teammate not found", 404 if not (event in teammate.events or event.id in teammate.events): return "Teammate not registered for event", 400 project.name = request.json.get('name') project.description = request.json.get('description') project.image = request.json.get('image') project.event = event project.team = [] project.team.append(user) project.team.append(teammate) project.save() if not project.id: return 'Error creating project', 500 return project.select_related(max_depth=1).to_json()
def register_mentor(event_id): sg = sendgrid.SendGridClient('gopilot', app.config["SENDGRID_PASS"]) event = Event.find_event(event_id) if not event: return "Event not found", 404 user = Mentor() for key, value in request.get_json().items(): if key == "password": setattr(user, key, bcrypt.hashpw(value.encode('utf-8'), bcrypt.gensalt())) elif not key.startswith('_') and not key == "id": # Some security setattr(user, key, value) user.events.append(event) user.save() message = sendgrid.Mail() message.add_to(user.name + "<" + user.email + ">") message.set_from("Pilot <*****@*****.**>") message.set_subject("Thanks for signing up to mentor at " + event.name + "!") email_html = render_template( 'mentor_registration.html', event_name=event.name, first_name=user.name.split(' ')[0], subject="Thanks for signing up to mentor at for " + event.name + "!") message.set_html(email_html) email_text = render_template( 'mentor_registration.txt', event_name=event.name, first_name=user.name.split(' ')[0], subject="Thanks for signing up to mentor at " + event.name + "!") message.set_text(email_text) if not app.config['TESTING']: status, msg = sg.send(message) print(status, msg) else: print("Sending message to " + user.email, message) return user.to_json()
def get_event_projects(event_id): event = Event.find_event(event_id) if not event: return "Event not found", 404 projects = [] query = {} for key, obj in request.args.iteritems(): query[key] = ObjectId(obj) query['name__exists'] = True query['event'] = event.id for project in Project.objects(**query).only( 'name', 'image', 'description', 'team', 'prize').select_related(max_depth=1): projects.append(project.to_dict()) return json.dumps(projects), 200, jsonType
def update_event(event_id): user_id = auth.check_token(request.headers.get('session')) if not user_id: return "Unauthorized request: Bad session token", 401 user = Organizer.find_id(user_id) if not user: return "Unauthorized request: User doesn't have permission", 401 event = Event.find_event(event_id) if not event: return "Event not found", 404 for key, value in request.get_json().items(): if not key.startswith('_'): # Some security setattr(event, key, value) event.save() return event.to_json()
def delete_scheduleitem(event_id, index): index = int(index) user_id = auth.check_token( request.headers.get('session') ) if not user_id: return "Unauthorized request: Bad session token", 401 user = Organizer.find_id( user_id ) if not user: return "Unauthorized request: User doesn't have permission", 401 event = Event.find_event( event_id ) if not event: return "Event not found", 404 if not (0 <= index < len(event.schedule)): return "Schedule item not found", 404 event.schedule.pop(index) return 'Schedule item deleted'
def checkout(event_id): user_id = auth.check_token(request.headers.get('session')) if not user_id: return "Unauthorized request: Bad session token", 401 user = Organizer.find_id(user_id) if not user: return "Unauthorized request: User doesn't have permission", 401 event = Event.find_event(event_id) if not event: return "Event not found", 404 attendee = User.find_id(request.json['attendee_id']) if not attendee: return "Attendee not found", 404 if event in attendee.checkins: attendee.checkins.remove(event) attendee.save() return json.dumps({"status": "removed"}), 200, jsonType
def get_post(event_id, post_id): user_id = auth.check_token(request.headers.get('session')) if not user_id: return "Unauthorized request: Bad session token", 401 user = User.find_id(user_id) if not user: return "User not found", 404 event = Event.find_event(event_id) if not event: return "Event not found", 404 attended_ids = [evt.id for evt in user.events] if not (event.id in attended_ids or user.type == "organizer"): return "Unauthorized request: User doesn't have permission" post = Post.find_id(post_id) if not post: return "Post not found", 404 return post.to_json()
def all_scheduleitems(event_id): user_id = auth.check_token( request.headers.get('session') ) if not user_id: return "Unauthorized request: Bad session token", 401 user = User.find_id( user_id ) if not user: return "User not found", 404 event = Event.find_event( event_id ) if not event: return "Event not found", 404 attended_ids = [ evt.id for evt in user.events ] if not (event.id in attended_ids or user.type == "organizer"): return "Unauthorized request: User doesn't have permission" schedule = [] for s in event.schedule: schedule.append(s.to_dict()) return json.dumps( schedule ), 200, jsonType
def all_posts(event_id): user_id = auth.check_token(request.headers.get('session')) if not user_id: return "Unauthorized request: Bad session token", 401 user = User.find_id(user_id) if not user: return "User not found", 404 event = Event.find_event(event_id) if not event: return "Event not found", 404 attended_ids = [evt.id for evt in user.events] if not (event.id in attended_ids or user.type == "organizer"): return "Unauthorized request: User doesn't have permission" posts = [] for p in Post.objects(event=event): posts.append(p.to_dict()) return json.dumps(posts), 200, jsonType
def find_tweets(event_id): event = Event.find_event(event_id) if not event: return "Event not found", 404 twitter = Twitter(auth=OAuth2(bearer_token=app.config['TWITTER_TOKEN'])) tweets = twitter.search.tweets(q='#' + event.name, result_type="recent")['statuses'] data = [] if tweets: for tweet in tweets: data.append({ 'time': tweet['created_at'], 'text': tweet['text'], 'user': tweet['user']['screen_name'], 'url': 'https://twitter.com/' + tweet['user']['screen_name'] + "/status/" + str(tweet['id']) }) # else: ## Fallback - use Topsy API which returns older tweets # r = requests.get('http://otter.topsy.com/search.json?apikey='+app.config['TOPSY_KEY']+'&perpage=20&q=%23'+event.name) # tweets = json.loads(r.text)['response']['list'] # for tweet in tweets: # if not tweet['content'].startswith('RT'): # data.append({ # 'time': datetime.utcfromtimestamp(tweet['firstpost_date']).strftime("%Y-%m-%d %H:%M:%S"), # 'text': tweet['content'], # 'user': tweet['trackback_author_nick'], # 'url': tweet['url'] # }) return json.dumps(data), 200, jsonType
def register(event_id): user = None discount = False sg = sendgrid.SendGridClient('gopilot', app.config["SENDGRID_PASS"]) event = Event.find_event(event_id) if not event: return "Event not found", 404 price = event.price if hasattr(request, 'json') and request.json and 'user' in request.json: if User.objects(email=request.json['user']['email']).first(): return json.dumps({ "status": "failed", "reason": "email", "message": "Your email already has a Pilot account." }), 400, jsonType print("has user") user = Student() user.name = request.json['user']['name'] user.email = request.json['user']['email'] user.complete = False user.completion_token = random_uuid().hex if 'discount' in request.json and request.json['discount'] != False: print("has discount") # user.save() discount = checkDiscount(request.json['discount']) if discount: price -= discount print("Charging user %s" % price) if 'stripe_token' in request.json: print("has stripe") stripe.api_key = app.config['STRIPE_KEY'] try: customer = stripe.Customer.create( source=request.json['stripe_token'], description=user.name, email=user.email) except stripe.CardError, e: app.logger.error("Customer Card Error: " + str(e)) err = e.json_body['error'] return json.dumps({ "status": "failed", "reason": err['param'] if ('param' in err) else 'customer', "message": err['message'] }), 400, jsonType user.stripe_id = customer.id try: stripe.Charge.create( amount=(price * 100), ## Cents currency="usd", customer=customer.id, description="Registration for " + event.name) except stripe.CardError, e: print("Charge Card Error", e) err = e.json_body['error'] return json.dumps({ "status": "failed", "reason": err['param'] if ('param' in err) else 'charge', "message": err['message'] }), 400, jsonType
def find_event(event_id): event = Event.find_event(event_id) if not event: return "Event not found", 404 return event.to_json()