def get_all(): projects = [] query = {} for key, obj in request.args.iteritems(): query[key] = ObjectId(obj) for project in Project.objects(**query).select_related(max_depth=1): projects.append(project.to_dict()) return json.dumps(projects), 200, jsonType
def get_all_projects(): # TODO: find out how to filter and sort with mongoengine q = Project.objects() if 'country_id' in request.args: q.filter(country__country_code=request.args['country_id']) if 'sort' in request.args: q.order_by(request.args['sort']) # parse them through a schema which converts is to a dict result = project_schema.dump(q.all(), many=True) return jsonify({'projects': result.data }) # convert dict to json and return that to client
def search(): user_query = User.objects.all() project_query = Project.objects.all() if 'q' in request.args: q = request.args['q'] user_query = User.objects(username__icontains=q).all() project_query = Project.objects(title__icontains=q).all() return jsonify({ 'projects': project_schema.dump(project_query.all(), many=True).data, 'users': user_schema.dump(user_query, many=True).data })
def delete_project(current_user: User, project_id): project = Project.objects(pk=project_id) # check if user is an admin if not current_user.admin: # if not filter by the projects only from this user project = project.filter(owner=current_user.id) project = project.first() if project is None: return jsonify({'message': 'No project found!'}), 404 project.delete() return jsonify({'message': 'Project deleted!'})
def get_event_projects(event_id): event = Event.find_event(event_id) if not event: return "Event not found", 404 projects = [] query = {} for key, obj in request.args.iteritems(): query[key] = ObjectId(obj) query['name__exists'] = True query['event'] = event.id for project in Project.objects(**query).only( 'name', 'image', 'description', 'team', 'prize').select_related(max_depth=1): projects.append(project.to_dict()) return json.dumps(projects), 200, jsonType
def update_project(current_user: User, project_id): q = Project.objects(pk=project_id) # check if user is an admin if not current_user.admin: # if not filter by the projects only from this user q = q.filter(owner=current_user.id) project = q.first() if project is None: return jsonify({'message': 'No project found!'}), 404 data = request.form.to_dict( ) # form data is used instead request.json so files can be uploaded if not data: # no data given return jsonify({'message': 'Missing data to update project'}), 400 if 'cover' in request.files and request.files['cover'] != '': # TODO: cleanup old file first # project.delete_cover() # a file is uploaded file = request.files['cover'] if allowed_file(file.filename): data['filename'] = save_file(file) # load and validate result = project_schema.load(data, partial=True) if len(result.errors) > 0: return jsonify({'errors': result.errors}), 422 for attr, value in result.data.items(): setattr(project, attr, value) project.save() return jsonify({ 'message': "Project updated!", 'project': project_schema.dump(project).data })
def populate(): print('populating countries') for country in countries: Country(**country).save() # populate users print('populating users') for user in users: User(**user).save() # populate projects print('populating projects') for project in projects: owner = User.objects(public_id=project['owner']).first() country = Country.objects(country_code=project['country']).first() collaborators = [] for collab in project['collaborators']: collaborators.append(User.objects(public_id=collab).first()) del project['owner'] del project['country'] del project['collaborators'] p = Project(**project) p.owner = owner p.collaborators += collaborators p.country = country p.save() print('populating donations') for donation in donations: project = Project.objects(title=donation['project']).first() donator = User.objects(public_id=donation['donator']).first() del donation['project'] del donation['donator'] d = Donation(**donation) d.project = project d.donator = donator d.save()
def get_user_projects(user_id): user = user_schema.dump(find_user_or_404(user_id)).data projects = Project.objects(owner=user['id']) result = project_schema.dump(projects.all(), many=True) return jsonify({'projects': result.data})