def create_user_stream(): try: # parse arguments parser = reqparse.RequestParser() parser.add_argument('name') parser.add_argument('description', default="Just a stream") parser.add_argument('isPublic', default=True) form = parser.parse_args() # grab current user user = User.objects.get(id=g.user['id']) # create new stream stream = Stream() stream.name = form['name'] stream.description = form['description'] stream.isPublic = form['isPublic'] stream.user = user stream.save() tiny_id = tinyurl.encode(str(stream.id)) url = url_for('spa_web.single_stream', stream_id=tiny_id) return {'status': 'success', 'url': url}, 200 except Exception as err: print "Error while creating stream: " + repr(err) return {'status': 'failed', 'error': repr(err)}, 500
def save_new_recording(): parser = reqparse.RequestParser() parser.add_argument('audio-blob', type=FileStorage, location='files') parser.add_argument('description') form = parser.parse_args() file = form['audio-blob'] current_app.logger.debug("file: '%s' type: '%s'" % (file.filename, file.content_type)) if file and file.content_type == 'audio/ogg': user = User.objects.get(id=g.user['id']) recording = Recording() recording.description = form['description'] recording.isPublic = True recording.user = user recording.save() recording_id = str(recording.id) recording_path = os.path.join(current_app.config['RECORDINGS_PATH'], recording_id + '.ogg') current_app.logger.debug('saving recording to: ' + recording_path) file.save(recording_path) tiny_id = tinyurl.encode(str(recording_id)) url = url_for('spa_web.single_recording', recording_id=tiny_id) return {'status': 'success', 'url': url} # else file upload failed, show an error page return {'status': 'failed'}
def to_web_dto(entity): tiny_id = tinyurl.encode(str(entity.id)) try: listen = Listen.objects.get(Q(user=g.user['id']) & Q(recording=entity.id)) progress, position, duration = listen.progress, listen.position, listen.duration except Listen.DoesNotExist: progress, position, duration = 0, 0, 0 return { 'timestamp': entity.postedAt.isoformat(), 'isMine': entity.user.id == g.user['id'], 'username': entity.user.username, 'profileImage': entity.user.profileImage, 'isPublic': entity.isPublic, 'description': entity.description or "(no description)", 'id': entity.id, 'url': '/recordings/' + str(entity.id) + '.ogg', 'tinyId': tiny_id, 'publicUrl': url_for('spa_web.single_recording', recording_id=tiny_id), 'progress': progress, 'position': position, 'duration': duration, }