def fetch(media_type, genre): if media_type not in app.config.SUPPORTED_MEDIATYPES: raise UnsupportedException('Media type %s is not supported' % media_type) if genre not in app.config.SUPPORTED_GENRE: raise UnsupportedException('Genre %s is not supported' % genre) api_key = request.args.get('api_key') if api_key is None: raise UnsupportedException('API Key is missing') resource = Resources.fetch(api_key, media_type, genre) if resource is None: raise ResourceExhaustException('Resources exhausted for media_type: %s and genre: %s' % (media_type, genre)) return jsonify(media_type=media_type, content=resource)
def add(): if request.method == 'GET': return render_template('add.html', \ media_types=app.config.SUPPORTED_MEDIATYPES, \ genres=app.config.SUPPORTED_GENRE) else: attachment = request.files['attachment'] media_type = request.form.get('media_type') genre = request.form.get('genre') if not media_type or not genre or not attachment: flash('Please fill all mandatory fields') return redirect(url_for('pages.add')) if media_type not in app.config.SUPPORTED_MEDIATYPES: raise UnsupportedException('Media type %s is not supported' % media_type) if genre not in app.config.SUPPORTED_GENRE: raise UnsupportedException('Genre %s is not supported' % genre) destination = None if attachment and allowed_file(attachment.filename): filename = secure_filename(attachment.filename) destination = os.path.join(app.config.UPLOAD_FOLDER, filename) attachment.save(destination) if destination is None: raise DevilException('Something went wrong, please upload your \ content again') # Add the content of the file into database with open(destination, 'rb') as inputfile: items = json.loads(inputfile.read()) ids, failed_items = Resources.update(media_type, genre, items) os.remove(destination) return jsonify(inserted_ids=ids, failed_items=failed_items)