def tugas_create(): form = TugasForm(request.form) if form.validate_on_submit(): tugas = Tugas() form.populate_obj(tugas) user = g.user matkul = MataKuliah.get(MataKuliah.dosen == user) tugas.mata_kuliah = matkul tugas.save() file_list = request.files.getlist('file_pendukung') for f in file_list: if isinstance(f, FileStorage) and len(f.filename) != 0: import os path = generate_path() save_to = os.path.join(path, secure_filename(user.email)) if not os.path.exists(save_to): os.makedirs(save_to) tujuan = os.path.join(save_to, secure_filename(f.filename)) f.save(tujuan) p = Phile.create( filename=f.filename, filepath=tujuan, filetype=f.mimetype ) TugasFile.create(tugas=tugas, phile=p) flash('Sukses membuat tugas') return redirect(url_for('dosen:tugas:list')) return render_template('dosen/tugas/create.html', form=form)
def tugas_kumpulkan(tugas_id): tugas = Tugas.get(Tugas.id == tugas_id) user = current_user() kumpul_tugas = KumpulTugas() kumpul_tugas.mahasiswa = user kumpul_tugas.tugas = tugas phile = request.files['phile'] if isinstance(phile, FileStorage): path = generate_path(str(user.id), str(tugas.id)) path = os.path.join(path, secure_filename(phile.filename)) phile.save(path) f = Phile() f.filename = phile.filename f.filetype = phile.mimetype f.filepath = path f.save() kumpul_tugas.phile = f kumpul_tugas.save() return redirect(url_for('mhs:tugas:detail', tugas_id=tugas.id))
def upload(): phile = request.files.get('upload', None) if phile: path = generate_path() filepath = os.path.join(path, secure_filename(phile.filename)) phile.save(filepath) f = Phile() f.filename = phile.filename f.filetype = phile.mimetype f.filepath = filepath f.save() return jsonify({ 'uploaded': 1, 'filename': f.filename, 'url': url_for('sendfile', file_id=f.id) }) return jsonify({ 'uploaded': 0, 'error': { 'message': 'File cannot be uploaded' } })
def sendfile(file_id, download=False): phile = Phile.get(Phile.id == file_id) filepath = phile.filepath download = bool(request.args.get('download', False)) return send_file(filepath, as_attachment=download)