def put(self, request, format=None): priority = request.data.get('priority') job = Job.objects.filter(user=request.user).filter(priority=priority).first() serialized = JobSerializer(job, data=request.data, partial=True) if serialized.is_valid(): serialized.save() return Response(serialized.data, status=status.HTTP_200_OK) return Response(serialized.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, format=None): user = request.user profile = Profile.objects.get(user=user) profile_ser = ProfileSerializer(profile) img = Photo.objects.get(user=user) img_ser = PhotoSerializer(img) summary = Summary.objects.get(user=user) summary_ser = SummarySerializer(summary) socials = Socials.objects.filter(user=user) socials_ser = GetSocialSerializer(socials) education = School.objects.filter(user=user) edu_ser = SchoolSerializer(education, many=True) jobs = Job.objects.filter(user=user) jobs_ser = JobSerializer(jobs, many=True) skills = Skill.objects.filter(user=user) skills_ser = SkillSerializer(skills, many=True) d = { "profile": profile_ser.data, "img": img_ser.data, "socials": socials_ser.data, "summary": summary_ser.data, "exp": jobs_ser.data, "edu": edu_ser.data, "skills": skills_ser.data, } return Response(d, status.HTTP_200_OK)
def start_download_torrent(pool): dl = Download.objects.get(pool=pool, is_active=True) h = lt.add_magnet_uri(ses, dl.url, params) while (not h.has_metadata()): time.sleep(1) print("No metadata") info = h.get_torrent_info() total_pieces = info.num_pieces() for x in range(info.num_files()): h.file_priority(x, 0) pool_users = PoolUsers.objects.filter(pool=pool) divisions = bytes_divide(0, total_pieces, len(pool_users), 0) for i in range(len(pool_users)): user_alloted = divisions[i] job = Jobs.objects.create(pool=pool, alloted_user=pool_users[i].user, range_start=user_alloted[0], range_end=user_alloted[1], status="ALLOTED", left=0, url=dl.url) job.save() response = JobSerializer(job).data websocket_connections[pool.id][pool_users[i].user.id].send( text_data=generate_response('new_torrent_job', response))
def fx_job_finished(conn, json_data): job_id = json_data['id'] job = Jobs.objects.get(id=job_id) job.status = 'COMPLETED' job.left = 0 job.save() print("alloted_user:"******"RESERVED") new_job = Jobs.objects.filter(download=job.download, status="RESERVED") user_token = json_data['user_token'] user = DLUser.objects.get(id=jwt.decode(user_token, 'secret')['id']) for user_id in websocket_connections[job.download.pool.id].keys(): websocket_connections[job.download.pool.id][user_id].send( text_data=generate_response('job_finished', json_data)) if len(new_job) > 0: new_job = new_job[0] new_job.status = "ALLOTED" new_job.alloted_user = user new_job.save() response = JobSerializer(new_job).data #websocket_connections[job.pool.id][job.alloted_user.id].send(text_data=generate_response('new_job', response)) conn.send(text_data=generate_response('new_job', response)) else: jobs_todo = Jobs.objects.filter(pool=job.download.pool, status="RESERVED") if len(jobs_todo) > 0: job_todo = jobs_todo[0] job_todo.status = "ALLOTED" job_todo.alloted_user = user job_todo.save() response = JobSerializer(job_todo).data conn.send(text_data=generate_response('new_job', response)) jobs_left = Jobs.objects.filter(pool=job.download.pool, status="ALLOTED") if len(jobs_left) == 0: response = PoolSerializer(job.download.pool).data for userid in websocket_connections[job.download.pool.id].keys(): websocket_connections[job.download.pool.id][userid].send( text_data=generate_response('all_jobs_finished', response))
def get(self, request, format=None): if request.user.is_project_owner: project = getProjectByOwner(request.user.id) if not project: return errorResponse("No project available.") jobs = Job.objects.filter(project_id=project.id) serializer = JobSerializer(jobs, many=True) return Response(serializer.data) else: user_jobs = JobUsers.objects.filter(user_id=request.user.id) serializer = JobUsersSerializer(user_jobs, many=True) return Response(serializer.data)
def fx_resume(conn, json_data): user_token = json_data['user_token'] user_id = jwt.decode(user_token, 'secret')['id'] print("user id:", user_id) for pool_id in websocket_connections.keys(): if user_id in websocket_connections[pool_id].keys(): websocket_connections[pool_id][user_id] = conn user = DLUser.objects.get( id=jwt.decode(user_token, 'secret')['id']) print("user resumed: ", user) pool = Pool.objects.get(id=pool_id) print(pool) finished_jobs = Jobs.objects.filter(pool=pool, status="COMPLETED") for job in finished_jobs: response = JobSerializer(job).data conn.send( text_data=generate_response('job_finished', response)) alloted_jobs = Jobs.objects.filter(alloted_user=user, pool=pool, status="ALLOTED") if len(alloted_jobs) > 0: alloted_job = alloted_jobs[0] response = JobSerializer(alloted_job).data print("alloted job: ", response) conn.send(text_data=generate_response('new_job', response)) else: jobs_left = Jobs.objects.filter(pool=pool, status="RESERVED") if len(jobs_left) > 0: job_left = jobs_left[0] job_left.alloted_user = user job_left.status = "ALLOTED" job_left.save() response = JobSerializer(job_left).data print("new job assigned: ", response) conn.send(text_data=generate_response('new_job', response)) print("user jobs:", alloted_jobs)
def fx_start_download(conn, json_data): pool_token = json_data['pool_token'] pool = Pool.objects.get(id=jwt.decode(pool_token, 'secret')['id']) dl = Download.objects.get(pool=pool, is_active=True) if "magnet" in dl.url: return start_download_torrent(pool) file_size = requests.get(dl.url, stream=True).headers['Content-length'] size_payload = {'file_size': file_size} for user_id in websocket_connections[pool.id]: websocket_connections[pool.id][user_id].send( text_data=generate_response('file_size', size_payload)) pool_users = PoolUsers.objects.filter(pool=pool) divisions = bytes_divide(0, file_size, len(pool_users)) for i in range(len(pool_users)): user_alloted = divisions[i] first_job = True for result in bytes_divide(user_alloted[0], user_alloted[1], STATIC_JOB_COUNT): if first_job: job = Jobs.objects.create(pool=pool, alloted_user=pool_users[i].user, range_start=result[0], range_end=result[1], status="ALLOTED", left=(result[1] - result[0]), url=dl.url) job.save() response = JobSerializer(job).data websocket_connections[pool.id][pool_users[i].user.id].send( text_data=generate_response('new_job', response)) first_job = False else: job = Jobs.objects.create(pool=pool, alloted_user=pool_users[i].user, range_start=result[0], range_end=result[1], status="RESERVED", left=(result[1] - result[0]), url=dl.url) job.save()
def post(self, request, format=None): serializer = JobSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, format=None): snippets = Job.objects.all() null = None snippets = snippets.filter(homeless_ID=null) serializer = JobSerializer(snippets, many=True) return Response(serializer.data)
def delete(self, request, format=None): pk = request.data.get('id') job = Job.objects.filter(user=request.user).filter(pk=pk).first() serialized = JobSerializer(job.delete()) return Response(serialized.data, status=status.HTTP_200_OK)
def post(self, request, format=None): job = Job.objects.create(user=request.user) serialized = JobSerializer(job) return Response(serialized.data, status=status.HTTP_201_CREATED)
def get(self, request, format=None): jobs = Job.objects.filter(user=request.user) serialized = JobSerializer(jobs, many=True) if serialized: return Response(serialized.data, status=status.HTTP_200_OK) return Response(serialized.errors, status=status.HTTP_404_NOT_FOUND)