def check_progress_status(): """Compares when a project was last updated.""" users=dict() up_to_date = datetime.now() - timedelta(seconds=1) complete = db.session.query(Proj_Stat.project_id).filter(Proj_Stat.status_id == 6).all() last_update = db.session.query(Project.user_id, Project.name, Project.project_id).filter(Project.updated_at < up_to_date).all() for user_id, name, project_id in last_update: if user_id not in users.keys(): users[user_id] = [] if project_id not in complete: users[user_id].append(QuickReply(title=name, payload="project_{}".format(project_id))) for user_id, quick_replies in users.items(): if quick_replies: page.send(user_id, "It appears you haven't updated the following projects. Do you want to update any of them", quick_replies=quick_replies)
def handle_project_notes(sender_id, message_text): """Handles any notes the user inputs to their new project.""" from lib.utilities import update_project_notes if message_text: project_id = crafter[sender_id].get('project_id') project = update_project_notes(project_id=project_id, message_text=message_text) page.send( sender_id, Template.Buttons( "Your note has been added to your project. If you would like to get back to main menu type 'craftybot' again or click to view your projects page.", [{ 'type': 'web_url', 'title': 'Open Projects Home', 'value': server_host + '/user/{}/projects'.format(sender_id) }])) crafter[sender_id] = dict() else: page.send(sender_id, 'Did you want to leave a note?')
def handle_project_name(sender_id, message_text): """Handles message text for naming user's new project.""" # user can't call project project from lib.utilities import check_stock_count if message_text: project = add_project(sender_id=sender_id, name=message_text.strip()) crafter[sender_id]['project_id'] = project.project_id if check_stock_count(stock_type='pattern', sender_id=sender_id) != 0: page.send( sender_id, Template.Buttons( "Please upload your pattern photo to start a new project or click to open stock gallery.", [{ 'type': 'web_url', 'title': 'Open Stock Gallery', 'value': server_host + '/user/{}/pattern'.format(sender_id) }])) else: page.send( sender_id, "Please upload your pattern photo to start a new project") else: page.send(sender_id, "Sorry, didn't catch that.\nPlease add a project name")
def handle_due_date(sender_id, message_text): """Takes in the number of weeks a user has to do their new project.""" from lib.utilities import update_project_due_date if message_text: try: weeks = int(message_text.strip()) except: page.send( sender_id, "I'm sorry I didn't catch that.\nHow many weeks do you want to do this project?", ) else: project_id = crafter[sender_id].get('project_id') project = update_project_due_date(project_id=project_id, weeks=weeks) note_no = [ QuickReply(title="Note", payload="NOTE"), QuickReply(title="No Notes", payload="NO_NOTES") ] page.send( sender_id, "Got it. Anything else you want me to know about this project", quick_replies=note_no) crafter[sender_id]['due_date'] = weeks else: page.send( sender_id, 'Please add the number of weeks until you would like to work on this project' )
def handle_status_image(sender_id, image_url): """Handles message attachment for user's update photo.""" from lib.utilities import add_status_update image = add_image(sender_id=sender_id, image_url=image_url) project_id = crafter[sender_id].get('project_id') update_status = add_status_update(project_id, image) project = Project.query.filter(Project.project_id == project_id).first() status = Status.query.filter( Status.status_id == update_status.status_id).first() due_date = datetime.strftime(project.due_at, "%A, %B %d, %Y") crafter[sender_id] = dict() page.send( sender_id, Template.Buttons( "YAY! You're {status}. Just a reminder, this project is due by {due_date}. To update another project say 'craftybot'" .format(status=status.name, due_date=due_date), [{ 'type': 'web_url', 'title': 'Open Projects Home', 'value': server_host + '/user/{}/projects'.format(sender_id) }]))
def handle_stock_image(sender_id, image_url, stock_type): """Handles message attachment for user's pattern photo.""" image = add_image(sender_id=sender_id, image_url=image_url) stock = add_stock(image=image, stock_type=stock_type) project_id = crafter[sender_id].get('project_id') crafter[sender_id][stock_type + '_id'] = getattr(stock, stock_type + '_id') if project_id: project = add_stock_to_project(stock=stock, project_id=project_id) add_next_stock_response(sender_id=sender_id, stock_type=stock_type) else: page.send( sender_id, Template.Buttons( "This image has been add to your {} stock photos.To update a project say 'craftybot' or click to open your projects page" .format(stock_type), [{ 'type': 'web_url', 'title': 'Open Projects Home', 'value': server_host + '/user/{}/projects'.format(sender_id) }])) crafter[sender_id] = dict()
def add_next_stock_response(sender_id, stock_type): """If the user select an item from stock galleries return correct response.""" if stock_type == 'pattern': if check_stock_count(stock_type='fabric', sender_id=sender_id) != 0: page.send(sender_id, Template.Buttons("If you have the material upload you next photo or pick something from your exisiting stock.", [{'type': 'web_url', 'title': 'Open Fabric Gallery', 'value': server_host + '/user/{}/fabric'.format(sender_id)}])) else: page.send(sender_id, "If you have the material upload you next photo") else: page.send(sender_id, "Success, How many weeks do you want to do this project?")
def handle_start_route(sender_id): """Handles message text for first communication with bot.""" user = add_user(sender_id=sender_id) total_inprogress(sender_id=sender_id) if total_inprogress(sender_id=sender_id) >= 6: craftybot = [ QuickReply(title="Update Status", payload="UPDATE_STATUS"), QuickReply(title="Add Stock", payload="STOCK") ] page.send( sender_id, "Sorry but you have reach maximum number of projects. You need to finish something before you can add another new project.", quick_replies=craftybot) else: craftybot = [ QuickReply(title="New Project", payload="NEW_PROJECT"), QuickReply(title="Add Stock", payload="STOCK") ] if total_inprogress(sender_id=sender_id) != 0: craftybot.append( QuickReply(title="Update Status", payload="UPDATE_STATUS"), ) page.send(sender_id, "How may I help you today?", quick_replies=craftybot)