def add(): logged_in_user = get_user(current_user) data = {'logged_in_user': logged_in_user} form = AddNewDatasets() if form.validate_on_submit(): hashcode = generate_random_str(16) new_datasets = Datasets() new_datasets.hashcode = hashcode new_datasets.name = form.data['name'] new_datasets.description = form.data['description'] new_datasets.status = 1 new_datasets.created_at = datetime.now() new_datasets.created_by = logged_in_user['id'] # check attachment if form.attachment.data: name, ext = os.path.splitext(form.attachment.data.filename) upload_file_path = 'app/contentfiles/' hashed_filename = ''.join([hashcode, ext]) local_dirs = ''.join( [upload_file_path, datetime.now().strftime('%Y-%m-%d'), '/']) local_path = ''.join([local_dirs, hashed_filename]) # create dirs if not exists if not os.path.exists(os.path.dirname(local_dirs)): os.makedirs(os.path.dirname(local_dirs)) # move to local dirs form.attachment.data.save(local_path) # update new_datasets new_datasets.original_file_name = name new_datasets.original_file_ext = ext new_datasets.original_file_size = os.stat(local_path).st_size new_datasets.hashed_filename = hashed_filename new_datasets.hashed_filepath = local_dirs # insert SqlDB.session.add(new_datasets) SqlDB.session.commit() flash('Success add new dataset') return redirect(url_for('.index')) else: for field, errors in form.errors.items(): for error in errors: flash(u"Error in the %s field - %s" % (getattr(form, field).label.text, error)) return render_template('cms/datasets/add.html', data=data, form=form) return render_template('cms/datasets/add.html', data=data, form=form)
def index(): logged_in_user = get_user(current_user) data = {'logged_in_user': logged_in_user} # load active datasets datasets = Datasets.query.filter((Datasets.status == 1) & ( Datasets.created_by == logged_in_user['id'])).order_by( Datasets.id.desc()).all() data['datasets'] = datasets return render_template('cms/datasets/index.html', data=data)
def index(): logged_in_user = get_user(current_user) data = { 'logged_in_user': logged_in_user } booked_datasets = BookDatasets.query.filter((BookDatasets.user_id==logged_in_user['id']) & (BookDatasets.is_deleted==0)).all() data['booked_datasets'] = booked_datasets return render_template('cms/tasks/index.html', data=data)
def index(): logged_in_user = get_user(current_user) data = { 'logged_in_user': logged_in_user, 'active_users': 0, 'active_datasets': 0, 'active_task_booking': 0, } # stats data data['active_users'] = Users.query.filter(Users.status == 1).count() data['active_datasets'] = Datasets.query.filter( Datasets.status == 1).count() data['active_task_booking'] = BookDatasets.query.filter( BookDatasets.is_deleted == 0).count() return render_template('cms/dashboard/index.html', data=data)
def revoke(task_id): logged_in_user = get_user(current_user) booking = BookDatasets.query.filter((BookDatasets.id==task_id) & (BookDatasets.user_id==logged_in_user['id'])).first() if booking == None: return "Access Denied!" else: booking.is_deleted = 1 booking.updated_at = datetime.now() booking.updated_by = logged_in_user['id'] try: SqlDB.session.commit() except Exception as e: SqlDB.session.rollback() return redirect(url_for('.index'))
def booking(): logged_in_user = get_user(current_user) data = { 'logged_in_user': logged_in_user } form = AddNewBookingTask() if form.validate_on_submit(): # valid data for item in form.data['datasets']: print(item.id, item.name, item.description) # check if exists and status = 1, if not exist create new check = BookDatasets.query.filter((BookDatasets.user_id==logged_in_user['id']) & (BookDatasets.datasets_id==item.id) & (BookDatasets.is_deleted==0)).first() if check == None: new_db_book = BookDatasets() new_db_book.datasets_id = item.id new_db_book.user_id = logged_in_user['id'] new_db_book.is_deleted = 0 new_db_book.created_at = datetime.now() new_db_book.created_by = logged_in_user['id'] try: SqlDB.session.add(new_db_book) SqlDB.session.commit() except Exception as e: print(e) flash('Success book datasets') return redirect(url_for('.index')) else: for field, errors in form.errors.items(): for error in errors: flash(u"Error in the %s field - %s" % ( getattr(form, field).label.text, error )) return render_template('cms/tasks/book.html', data=data, form=form) return render_template('cms/tasks/book.html', data=data, form=form)
def delete(dataset_id): logged_in_user = get_user(current_user) dataset = Datasets.query.filter( (Datasets.id == dataset_id) & (Datasets.status == 1) & (Datasets.created_by == logged_in_user['id'])).first() if dataset == None: return "Access Denied" else: dataset.status = 0 dataset.updated_at = datetime.now() dataset.updated_by = logged_in_user['id'] try: SqlDB.session.commit() except Exception as e: SqlDB.session.rollback() return redirect(url_for('.index'))
def download(task_id): logged_in_user = get_user(current_user) data = { 'logged_in_user': logged_in_user } # validate datasets booked_dataset = BookDatasets.query.filter((BookDatasets.id==task_id) & (BookDatasets.user_id==logged_in_user['id']) & (BookDatasets.is_deleted==0)).first() if booked_dataset == None: return "Access Denied!" else: # send from directory try: hashed_filepath = booked_dataset.dataset.hashed_filepath split_path = booked_dataset.dataset.hashed_filepath.split('/') if 'app' in split_path[0]: hashed_filepath = '/'.join(split_path[1:]) + '/' return send_file(''.join([hashed_filepath, booked_dataset.dataset.hashed_filename]), as_attachment=True, attachment_filename=booked_dataset.dataset.hashed_filename) except Exception as e: return "Something went wrong or dataset not exist anymore"