def recommend(request): global tag_relations_mem token = request.META.get('HTTP_AUTHORIZATION', None) if not (token and authentication.is_authenticated(token)): return JsonResponse({ "response": False, "error": "Unauthorized" }) if not tag_relations_mem: initialize_memory() recommend_id = request.GET.get('id', None) if not recommend_id: recommend_id = authentication.get_user_id(token) if Project.objects(id=recommend_id): ret = find_freelancers(Project.objects.get(id=recommend_id)) elif User.objects(id=recommend_id): ret = find_projects(User.objects.get(id=recommend_id), authentication.get_user_id(token)) else: return JsonResponse({ "response": False, "error": "No matching id" }) return JsonResponse({ "response": True, "recommendation": ret[:6] })
def get_own_projects(request): if request.method == 'GET': token = request.META.get('HTTP_AUTHORIZATION', None) if token and authentication.is_authenticated(token): user_id = authentication.get_user_id(token) freelancer_projects = Project.objects. \ filter(freelancer=user_id) owner_projects = Project.objects. \ filter(owner=user_id) freelancer_projects_json = [] owner_projects_json = [] for project in freelancer_projects: freelancer_projects_json.append(project_json(project,user_id)) for project in owner_projects: owner_projects_json.append(project_json(project,user_id)) res = {'freelancer':freelancer_projects_json, 'client': owner_projects_json} try: return JsonResponse({"response": True, "projects": res}) except Exception as e: return JsonResponse({'response': False, 'error': str(e)}) else: return JsonResponse({"response": False, "error": "Unauthorized"}) return JsonResponse({ "response": False, "error": "wrong request method" })
def accept_bid(request): if request.method == 'POST': token = request.META.get('HTTP_AUTHORIZATION', None) if token and authentication.is_authenticated(token): body = json.loads(request.body.decode('utf-8')) try: user_id = authentication.get_user_id(token) bid = Bid.objects.get(id=body['bid_id']) if user_id != str(bid.project.owner.id): return JsonResponse({'response': False, 'error': "Only owner of the project can accept a bid"}) other_bids = Bid.objects.filter(project=bid.project) other_bids.update(status=2, updated_at=datetime.now) bid.update(status=1, updated_at=datetime.now) bid.project.freelancer = bid.freelancer bid.project.status = 1 bid.project.save() return JsonResponse({'response': True}) except Exception as e: return JsonResponse({'response': False, 'error': str(e)}) else: return JsonResponse({"response": False, "error": "Unauthorized"}) return JsonResponse({ "response": False, "error": "wrong request method" })
def finish_project(request): if request.method == 'PUT': token = request.META.get('HTTP_AUTHORIZATION', None) if token and authentication.is_authenticated(token): user_id = authentication.get_user_id(token) body = json.loads(request.body.decode('utf-8')) try: project = Project.objects.get(id=body['project_id']) if user_id == str(project.owner.id): payment_status = make_payment(project) if payment_status == 0: return JsonResponse({"response": False, "error": "Insufficient funds"}) elif payment_status == 1: milestones = Milestone.objects.filter(project=project) milestones.update(status=3, updated_at=datetime.now()) project.status = 2 project.save() return JsonResponse({"response": True, "project": project_json(project, user_id)}) else: return JsonResponse({"response": False, "error": "Not allowed to edit this project"}) except Exception as e: return JsonResponse({'response': False, 'error': str(e)}) else: return JsonResponse({"response": False, "error": "Unauthorized"}) return JsonResponse({ "response": False, "error": "wrong request method" })
def search_projects(request): if request.method == 'GET': query = request.GET.get('query', '') token = request.META.get('HTTP_AUTHORIZATION', None) user_id = (authentication.get_user_id(token) if token else None) projects = Project.objects.filter(Q(status__gte=0) & (Q(title__icontains=query) | Q(description__icontains=query))) res = [] for project in projects: res.append(project_json(project, user_id)) try: return JsonResponse({"response": True, "projects": res}) except Exception as e: return JsonResponse({'response': False, 'error': str(e)}) return JsonResponse({ "response": False, "error": "wrong request method" })
def attachment_handler(request): token = request.META.get('HTTP_AUTHORIZATION', None) if not (token and authentication.is_authenticated(token)): return JsonResponse({"response": False, "error": "Unauthorized"}) attach_to = request.GET.get('id', authentication.get_user_id(token)) if Portfolio.objects(id=attach_to): attached = Portfolio.objects.get(id=attach_to) elif Project.objects(id=attach_to): attached = Project.objects.get(id=attach_to) elif Milestone.objects(id=attach_to): attached = Milestone.objects.get(id=attach_to) else: attached = None if request.method == 'POST': try: for file in request.FILES: utils.handle_uploaded_file('attachments/' + attach_to, request.FILES[file], str(request.FILES[file])) attached.attachments.append(str(request.FILES[file])) attached.save() except Exception as e: return JsonResponse({'response': False, 'error': str(e)}) return JsonResponse({'response': True}) elif request.method == 'DELETE': try: delete_this = request.body.decode('utf-8') if delete_this: attached.attachments.remove(str(delete_this)) os.remove('media/attachments/' + attach_to + '/' + delete_this) attached.save() except Exception as e: return JsonResponse({'response': False, 'error': str(e)}) return JsonResponse({'response': True}) else: return JsonResponse({ "response": False, "error": "Wrong request method" })
def annotation_handler(request): token = request.META.get('HTTP_AUTHORIZATION', None) if request.method == 'GET': #try: query = request.GET.get('query') annotations = Annotation.objects.filter(IRI=query) response = [] for annotation in annotations: response.append(annotation.annotation_object) return JsonResponse({"response": True, "annotations": response}) #except Exception as e: # return JsonResponse({'response': False, 'error': str(e)}) elif request.method == 'POST': if token and authentication.is_authenticated(token): try: user_id = authentication.get_user_id(token) request_data = json.loads(request.body.decode('utf-8')) targets = [] if 'targets' in request_data: targets = request_data['targets'] body = request_data['body'] if 'body' in request_data else None annotation = Annotation() if 'context' in request_data: annotation.context = request_data['context'] annotation.IRI = request_data['url'] annotation.motivation = request_data['motivation'] annotation.creator = 'https://karpuz.ml/' + user_id annotation.save() if body: new_body = Body() new_body.annotation = annotation new_body.IRI = request_data['url'] new_body.type = body['type'] new_body.text = body['text'] if body[ 'type'] == 'text' else None new_body.save() if len(targets) > 0: for target in targets: new_target = Target() new_target.annotation = annotation if 'context' in target: new_target.context = target['context'] new_target.type = target['type'] new_target.IRI = target['IRI'] new_target.x = target['x'] if 'x' in target else None new_target.y = target['y'] if 'y' in target else None new_target.start = target[ 'start'] if 'start' in target else None new_target.end = target[ 'end'] if 'end' in target else None new_target.save() except Exception as e: return JsonResponse({'response': False, 'error': str(e)}) return JsonResponse({ "response": True, "annotation": annotation.annotation_object }) else: return JsonResponse({"response": False, "error": "Unauthorized"})
def project_handler(request): token = request.META.get('HTTP_AUTHORIZATION', None) if request.method == 'GET': ids = request.GET.get('ids') user_id = (authentication.get_user_id(token) if token else None) if ids: ids = ids.split(',') projects = Project.objects.filter(id__in=ids) # TODO: order as given id list res = [] for project in projects: res.append(project_json(project, user_id)) try: return JsonResponse({"response": True, "projects": res}) except Exception as e: return JsonResponse({'response': False, 'error': str(e)}) else: projects = Project.objects.filter(status__in=[0,1]).order_by("-created_at") # excludes discarded projects res = [] for project in projects: res.append(project_json(project, user_id)) try: return JsonResponse({"response": True, "projects": res}) except Exception as e: return JsonResponse({'response': False, 'error': str(e)}) elif request.method == 'POST': if token and authentication.is_authenticated(token): user_id = authentication.get_user_id(token) body = json.loads(request.body.decode('utf-8')) new_project = Project() try: new_project.owner = models.User.objects.get(id=authentication.get_user_id(token)) new_project.freelancer = None new_project.description = body['description'] new_project.title = body['title'] new_project.budget = body['budget'] new_project.status = 0 # default new_project.location = body['location'] if 'location' in body else None new_project.tags = [] if 'tags' in body: for tag in body['tags']: tag = str(tag) create_tag(tag) new_project.tags.append(SemanticTag.objects.get(wikidata_id=tag)) new_project.save() if "milestones" in body: for milestone in body['milestones']: new_milestone = Milestone() new_milestone.name = milestone['name'] new_milestone.detail = milestone['detail'] if 'detail' in milestone else None new_milestone.deadline = fix_date(milestone['deadline']) new_milestone.project = new_project new_milestone.save() final_milestone = Milestone(name='Final', detail='This is the final delivery of the project.', deadline=fix_date(body['project_deadline']), project=new_project, is_final=True) final_milestone.save() return JsonResponse({"response": True, "project": project_json(new_project, user_id)}) except Exception as e: return JsonResponse({'response': False, 'error': str(e)}) else: return JsonResponse({"response": False, "error": "Unauthorized"}) elif request.method == 'PUT': if token and authentication.is_authenticated(token): user_id = authentication.get_user_id(token) body = json.loads(request.body.decode('utf-8')) project = Project.objects.get(id=body['project_id']) modify_project(body, project) try: project.save() return JsonResponse({"response": True, "project": project_json(project, user_id)}) except Exception as e: return JsonResponse({'response': False, 'error': str(e)}) else: return JsonResponse({"response": False, "error": "Unauthorized"}) elif request.method == 'DELETE': project_id = request.GET.get('id', '') token = request.META.get('HTTP_AUTHORIZATION', None) if token and authentication.is_authenticated(token): user_id = authentication.get_user_id(token) try: project = Project.objects.get(id=project_id) milestones = Milestone.objects.filter(project=project) if str(project.owner.id) == user_id: project.delete() milestones.delete() else: return JsonResponse({'response': False, 'error': "Not allowed to delete this project"}) return JsonResponse({"response": True}) except Exception as e: return JsonResponse({'response': False, 'error': str(e)}) else: return JsonResponse({"response": False, "error": "Unauthorized"}) return JsonResponse({ "response": False, "error": "wrong request method" })