def upload_file(url, user): try: # S3 Save conn = boto.connect_s3(AWS_ACCESS_KEY, AWS_SECRET_KEY) bucket = conn.get_bucket(BUCKET_NAME) k = Key(bucket) k.key = RE_GET_FILE.match(url).group(5) file_object = urllib2.urlopen(url) fp = StringIO.StringIO(file_object.read()) k.set_contents_from_file(fp) k.make_public() # Save S3 url to user url_to_save = 'https://s3-us-west-2.amazonaws.com/'+ BUCKET_NAME + '/' + k.key project = Project.create(url=url_to_save, project_user_id=user.user_id, status='pre') if not user.team_id: user.project_id = project.proj_id user.save() else: project.team_id = user.team_id project.save() users = User.update(project_id=project.proj_id).where(User.team_id == user.team_id) users.execute() return True except: return False
def project_submission_handler(receiver, user, type_of_sub): if type_of_sub == 'file': return bot_messages.project_file_failure_message(user) else: url = receiver.get_text() if '.com' not in url: return bot_messages.project_file_failure_message(user) project = Project.create(url=url, project_user_id=user.user_id, status='pre') if not user.team_id: user.project_id = project.proj_id user.save() else: project.project_team_id = user.team_id project.save() users = User.update(project_id=project.proj_id).where(User.team_id == user.team_id) users.execute() user.project_id = project.proj_id user.save() return bot_messages.project_file_success_message(user) return bot_messages.project_file_failure_message(user)