def create_position(request): re = dict() cpn = Companyinfo() try: assert request.method == "POST" except: re['error'] = error(2, 'error, need post!') return HttpResponse(json.dumps(re), content_type = 'application/json') try: assert request.user != None assert request.user.is_staff #todo change here #cpn = Companyinfo.objects.get(user = request.user) cpn = Companyinfo.objects.get(username = request.user.username) except: re['error'] = error(100,"Permission denied!") name = request.POST.get('name','') position_type = request.POST.get('position_type','') work_city = request.POST.get('work_city','') work_address = request.POST.get('work_address','') release_time = datetime.now() et = request.POST.get('end_time','') position_description = request.POST.get('position_description','') position_request = request.POST.get('position_request','') days = request.POST.get('days_per_week','0') intime = request.POST.get('internship_time','0') samin = request.POST.get('salary_min','0') samax = request.POST.get('salary_max','1000000') poft = request.POST.get('part_or_full_time','0') status = request.POST.get("status","open") print "ok" try: assert len(name) in range(1,30) if_legal(name,False) except (AssertionError): re['error'] = error(210,'Position name is too short or too long!') return HttpResponse(json.dumps(re),content_type = 'application/json') except (UnicodeDecodeError,ValueError): re['error'] = error(211,'Illeage character found in position name!') return HttpResponse(json.dumps(re),content_type = 'application/json') except: re['error'] = error(299,'Unknown error!') return HttpResponse(json.dumps(re),content_type = 'application/json') print "check name ok" try: assert position_type in TYPE except (AssertionError): re['error'] = error(212,'Invaild position type') return HttpResponse(json.dumps(re),content_type = 'application/json') try: assert len(work_address) in range(1,100) if_legal(work_address,False) except (AssertionError): re['error'] = error(215,'The length of work address is too short or too long!') return HttpResponse(json.dumps(re),content_type = 'application/json') except (UnicodeDecodeError,ValueError): re['error'] = error(216,'Illeage character found in work address!') return HttpResponse(json.dumps(re),content_type = 'application/json') try: end_time = datetime.strptime(et,'%Y-%m-%d') assert end_time > release_time except (ValueError): re['error'] = error(217,'Invaild end time format!') return HttpResponse(json.dumps(re),content_type = 'application/json') except (AssertionError): re['error'] = error(218,'End time is too early!') return HttpResponse(json.dumps(re),content_type = 'application/json') print "done0" try: assert len(position_description) in range(0,500) # if_legal(position_description,True) except (AssertionError): re['error'] = error(219,'The length of description is too long!') return HttpResponse(json.dumps(re),content_type = 'application/json') except (UnicodeDecodeError,ValueError): re['error'] = error(220,'Illegal character found in description!') return HttpResponse(json.dumps(re),content_type = 'application/json') except: re['error'] = error(299,'Unknown error!') return HttpResponse(json.dumps(re),content_type = 'application/json') try: assert len(position_request) in range(0,500) # if_legal(position_request,True) except (AssertionError): re['error'] = error(221,'The length of request is too long!') return HttpResponse(json.dumps(re),content_type = 'application/json') except (UnicodeDecodeError,ValueError): re['error'] = error(222,'Illegal character found in request!') return HttpResponse(json.dumps(re),content_type = 'application/json') except: re['error'] = error(299,'Unknown error!') return HttpResponse(json.dumps(re),content_type = 'application/json') print "done1" try: days_per_week = int(days) assert days_per_week in range(0,7) except (ValueError,AssertionError): re['error'] = error(223,'Invaild days perweek!') return HttpResponse(json.dumps(re),content_type = 'application/json') except: re['error'] = error(299,'Unknown Error!') return HttpResponse(json.dumps(re),content_type = 'application/json') try: internship_time = int(intime) assert internship_time >= 0 except (ValueError,AssertionError): re['error'] = error(224,'Invaild internship time!') return HttpResponse(json.dumps(re),content_type = 'application/json') except: re['error'] = error(299,'Unknown Error!') return HttpResponse(json.dumps(re),content_type = 'application/json') try: salary_min = int(samin) assert salary_min in range(0,1000000) except (ValueError,AssertionError): re['error'] = error(225,'Invaild min salary!') return HttpResponse(json.dumps(re),content_type = 'application/json') except: re['error'] = error(299,'Unknown Error!') return HttpResponse(json.dumps(re),content_type = 'application/json') print 'done2' try: salary_max = int(samax) assert salary_max in range(0,1000000) except (ValueError,AssertionError): re['error'] = error(226,'Invaild max salary!') return HttpResponse(json.dumps(re),content_type = 'application/json') except: re['error'] = error(299,'Unknown Error!') return HttpResponse(json.dumps(re),content_type = 'application/json') try: assert salary_min < salary_max except: re['error'] = error(227,'Max salary should be more than min salary') return HttpResponse(json.dumps(re),content_type = 'application/json') try: part_or_full_time = int(poft) assert part_or_full_time in range(0,2) except (AssertionError): re['error'] = error(263,'Position can only be part time or full time') return HttpResponse(json.dumps(re),content_type = 'application/json') try: assert status in STATUS except (AssertionError): re['error'] = error(228,'Invaild position status') return HttpResponse(json.dumps(re),content_type = 'application/json') posi = Position(name = name,position_type = position_type,work_address = work_address, end_time = end_time,position_description = position_description, position_request = position_request,days_per_week = days_per_week, internship_time = internship_time,salary_min = salary_min,salary_max = salary_max,part_or_full_time=part_or_full_time,status = status, company = cpn) print "done3" try: posi.save() except (DatabaseError): re['error'] = error(250,'Database error: Failed to save') return HttpResponse(json.dumps(re),content_type = 'application/json') #except: # re['error'] = error(299,'Unknown Error!') # return HttpResponse(json.dumps(re),content_type = 'application/json') re['error'] = error(1,'Create position succeed!') cpn.positions.append(posi) cpn.save() return HttpResponse(json.dumps(re),content_type = 'application/json')
def update_position(request,position_id): re = dict() try: assert request.method == "POST" except: re['error'] = error(2, 'error, need post!') return HttpResponse(json.dumps(re), content_type = 'application/json') ''' try: id = int(request.POST['id']) assert id >= 0 except (KeyError): re['error'] = error(200,"Illegal request!") return HttpResponse(json.dumps(re), content_type = 'application/json') except (ValueError,AssertionError): re['error'] = error(230,"Invaild search id!") return HttpResponse(json.dumps(re), content_type = 'application/json') ''' try: posi = Position.objects.get(id = position_id) except (ObjectDoesNotExist): re['error'] = error(249,"Object does not exist") return HttpResponse(json.dumps(re), content_type = 'application/json') except (DatabaseError): re['error'] = error(251,"Database error: Failed to search!") return HttpResponse(json.dumps(re), content_type = 'application/json') except: re['error'] = error(299,'Unknown Error!') return HttpResponse(json.dumps(re),content_type = 'application/json') try: assert request.user != None assert request.user.is_staff cpn = Companyinfo.objects.filter(user = request.user) assert posi in cpn.position except: re['error'] = error(100,"Permission denied!") name = request.POST.get('name','') position_type = request.POST.get('position_type','') work_address = request.POST.get('work_address','') et = request.POST.get('end_time','') position_description = request.POST.get('position_description','') position_request = request.POST.get('position_request','') days = request.POST.get('days_per_week','0') intime = request.POST.get('internship_time','0') samin = request.POST.get('salary_min','0') samax = request.POST.get('salary_max','1000000') part_or_full_time = request.POST.get('part_or_full_time','0') status = request.POST.get("status","open") try: assert len(name) in range(1,30) if_legal(name,False) except (AssertionError): re['error'] = error(210,'Position name is too short or too long!') return HttpResponse(json.dumps(re),content_type = 'application/json') except (UnicodeDecodeError,ValueError): re['error'] = error(211,'Illeage character found in position name!') return HttpResponse(json.dumps(re),content_type = 'application/json') except: re['error'] = error(299,'Unknown error!') return HttpResponse(json.dumps(re),content_type = 'application/json') try: assert position_type in TYPE except (AssertionError): re['error'] = error(212,'Invaild position type') return HttpResponse(json.dumps(re),content_type = 'application/json') try: assert len(work_address) in range(1,100) if_legal(work_address,False) except (AssertionError): re['error'] = error(215,'The length of work address is too short or too long!') return HttpResponse(json.dumps(re),content_type = 'application/json') except (UnicodeDecodeError,ValueError): re['error'] = error(216,'Illeage character found in work address!') return HttpResponse(json.dumps(re),content_type = 'application/json') try: end_time = datetime.strptime(et,'%Y-%m-%d') assert end_time > datetime.now() except (ValueError): re['error'] = error(217,'Invaild end time format!') return HttpResponse(json.dumps(re),content_type = 'application/json') except (AssertionError): re['error'] = error(218,'End time is too early!') return HttpResponse(json.dumps(re),content_type = 'application/json') try: assert len(position_description) in range(0,500) #if_legal(position_description,False) except (AssertionError): re['error'] = error(219,'The length of description is too long!') return HttpResponse(json.dumps(re),content_type = 'application/json') except (UnicodeDecodeError,ValueError): re['error'] = error(220,'Illegal character found in work address!') return HttpResponse(json.dumps(re),content_type = 'application/json') except: re['error'] = error(299,'Unknown error!') return HttpResponse(json.dumps(re),content_type = 'application/json') try: assert len(position_request) in range(0,500) #if_legal(position_request,False) except (AssertionError): re['error'] = error(221,'The length of request is too long!') return HttpResponse(json.dumps(re),content_type = 'application/json') except (UnicodeDecodeError,ValueError): re['error'] = error(222,'Illegal character found in request!') return HttpResponse(json.dumps(re),content_type = 'application/json') except: re['error'] = error(299,'Unknown error!') return HttpResponse(json.dumps(re),content_type = 'application/json') try: days_per_week = int(days) print days_per_week assert days_per_week in range(0,7) except (ValueError,AssertionError): re['error'] = error(223,'Invaild days perweek!') return HttpResponse(json.dumps(re),content_type = 'application/json') except: re['error'] = error(299,'Unknown Error!') return HttpResponse(json.dumps(re),content_type = 'application/json') try: internship_time = int(intime) assert internship_time >= 0 except (ValueError,AssertionError): re['error'] = error(224,'Invaild internship time!') return HttpResponse(json.dumps(re),content_type = 'application/json') except: re['error'] = error(299,'Unknown Error!') return HttpResponse(json.dumps(re),content_type = 'application/json') try: salary_min = int(samin) assert salary_min in range(0,1000000) except (ValueError,AssertionError): re['error'] = error(225,'Invaild min salary!') return HttpResponse(json.dumps(re),content_type = 'application/json') except: re['error'] = error(299,'Unknown Error!') return HttpResponse(json.dumps(re),content_type = 'application/json') try: salary_max = int(samax) assert salary_max in range(0,1000000) except (ValueError,AssertionError): re['error'] = error(226,'Invaild max salary!') return HttpResponse(json.dumps(re),content_type = 'application/json') except: re['error'] = error(299,'Unknown Error!') return HttpResponse(json.dumps(re),content_type = 'application/json') try: assert salary_min < salary_max except: re['error'] = error(227,'Max salary should be more than min salary') return HttpResponse(json.dumps(re),content_type = 'application/json') try: assert int(part_or_full_time) in range(0,2) except (AssertionError): re['error'] = error(262,'Position can only be part time or full time') return HttpResponse(json.dumps(re),content_type = 'application/json') try: assert status in STATUS except (AssertionError): re['error'] = error(228,'Invaild position status') return HttpResponse(json.dumps(re),content_type = 'application/json') posi.name = name posi.position_type = position_type posi.work_address = work_address posi.end_time = end_time posi.position_description = position_description posi.position_request = position_request posi.days_per_week = days_per_week posi.internship_time = internship_time posi.salary_min = salary_min posi.salary_max = salary_max posi.part_or_full_time = int(part_or_full_time) posi.status = status try: posi.save() except (DatabaseError): re['error'] = error(250,'Database error: Failed to save') return HttpResponse(json.dumps(re),content_type = 'application/json') except: re['error'] = error(299,'Unknown Error!') return HttpResponse(json.dumps(re),content_type = 'application/json') re['error'] = error(1,'Update position succeed!') return HttpResponse(json.dumps(re),content_type = 'application/json')
def search_position(request): re = dict() #try: #assert request.mothod == "GET" #except: # re['error'] = error(3, 'error, need get!') # return HttpResponse(json.dumps(re), content_type = 'application/json') qs = Position.objects.all().order_by('index') qs = qs.filter(status='open') ##cpn = Companyinfo.objects.all() if "id" in request.GET.keys(): if len(request.GET["id"]) > 0: try: id = request.GET["id"] qs = qs.filter(id = id) except (ValueError): re['error'] = error(230,"Invaild search id!") return HttpResponse(json.dumps(re), content_type = 'application/json') except (DatabaseError): re['error'] = error(251,"Database error: Failed to search!") return HttpResponse(json.dumps(re), content_type = 'application/json') except: re['error'] = error(299,'Unknown Error!') return HttpResponse(json.dumps(re),content_type = 'application/json') if "name" in request.GET.keys(): if len(request.GET["name"]) > 0: try: name = request.GET["name"] assert len(name) < 30 if_legal(name) qs = qs.filter(name__contains = name) except (AssertionError, ValueError, UnicodeDecodeError): re['error'] = error(231,"Invaild search name!") return HttpResponse(json.dumps(re), content_type = 'application/json') except (DatabaseError): re['error'] = error(251,"Database error: Failed to search!") return HttpResponse(json.dumps(re), content_type = 'application/json') except: re['error'] = error(299,'Unknown Error!') return HttpResponse(json.dumps(re),content_type = 'application/json') #types is a string which connected by ',' it is like "technology,design" if "types" in request.GET.keys(): if len(request.GET["types"]) > 0: try: position_types = request.GET["types"] position_types = position_types.split(',') for position_type in position_types: assert position_type in TYPE qs = qs.filter(position_type__in = position_types) except (AssertionError,ValueError,UnicodeDecodeError): re['error'] = error(238,"Invaild search type!") return HttpResponse(json.dumps(re), content_type = 'application/json') except (DatabaseError): re['error'] = error(251,"Database error: Failed to search!") return HttpResponse(json.dumps(re), content_type = 'application/json') except: re['error'] = error(299,'Unknown Error!') return HttpResponse(json.dumps(re),content_type = 'application/json') ##todo: tobe tested if "fields" in request.GET.keys(): if len(request.GET["fields"]) > 0: try: fields = request.GET["fields"] fields = fields.split(',') for field in fields: assert field in FIELD cmp = Companyinfo.objects(field__in = fields) qs = qs.filter(company__in = cmp) except (AssertionError,ValueError,UnicodeDecodeError): re['error'] = error(238,"Invaild search type!") return HttpResponse(json.dumps(re), content_type = 'application/json') except (DatabaseError): re['error'] = error(251,"Database error: Failed to search!") return HttpResponse(json.dumps(re), content_type = 'application/json') except: re['error'] = error(299,'Unknown Error!') return HttpResponse(json.dumps(re),content_type = 'application/json') ##single #if "type" in request.GET.keys(): # if len(request.GET["type"]) > 0: # try: # position_type = request.GET["type"] # assert position_type in TYPE # qs = qs.filter(position_type = position_type) # except (AssertionError,ValueError,UnicodeDecodeError): # re['error'] = error(238,"Invaild search type!") # return HttpResponse(json.dumps(re), content_type = 'application/json') # except (DatabaseError): # re['error'] = error(251,"Database error: Failed to search!") # return HttpResponse(json.dumps(re), content_type = 'application/json') # except: # re['error'] = error(299,'Unknown Error!') # return HttpResponse(json.dumps(re),content_type = 'application/json') if "work_city" in request.GET.keys(): if len(request.GET["work_city"]) > 0: try: work_city = request.GET["work_city"] assert len(work_city) < 50 if_legal(work_city) qs = qs.filter(work_city = work_city) except (AssertionError,ValueError,UnicodeDecodeError): re['error'] = error(232,"Invaild search work city!") return HttpResponse(json.dumps(re), content_type = 'application/json') except (DatabaseError): re['error'] = error(251,"Database error: Failed to search!") return HttpResponse(json.dumps(re), content_type = 'application/json') except: re['error'] = error(299,'Unknown Error!') return HttpResponse(json.dumps(re),content_type = 'application/json') if "mindays" in request.GET.keys(): if len(request.GET["mindays"]) > 0: try: mindays = int(request.GET["mindays"]) qs = qs.filter(days_per_week__gte = mindays) except (ValueError): re['error'] = error(233,"Invaild search min daysperweek!") return HttpResponse(json.dumps(re), content_type = 'application/json') except (DatabaseError): re['error'] = error(251,"Database error: Failed to search!") return HttpResponse(json.dumps(re), content_type = 'application/json') except: re['error'] = error(299,'Unknown Error!') return HttpResponse(json.dumps(re),content_type = 'application/json') if "maxdays" in request.GET.keys(): if len(request.GET["maxdays"]) > 0: try: maxdays = int(request.GET["maxdays"]) qs = qs.filter(Q(days_per_week__lte = maxdays) | Q(days_per_week = 0)) except (ValueError): re['error'] = error(234,"Invaild search max daysperweek!") return HttpResponse(json.dumps(re), content_type = 'application/json') except (DatabaseError): re['error'] = error(251,"Database error: Failed to search!") return HttpResponse(json.dumps(re), content_type = 'application/json') except: re['error'] = error(299,'Unknown Error!') return HttpResponse(json.dumps(re),content_type = 'application/json') #todo: to be tested #"workdays" is a string, it is like "3,4,6,0" (0 is for flexible time) if "workdays" in request.GET.keys(): if request.GET["workdays"] != 0: try: workdays = request.GET["workdays"] workdays = workdays.split(',') wd_list = [] for workday in workdays: wd_list.append(int(workday)) qs = qs.filter(days_per_week__in = wd_list) except (ValueError): re['error'] = error(234,"Invaild search max daysperweek!") return HttpResponse(json.dumps(re), content_type = 'application/json') except (DatabaseError): re['error'] = error(251,"Database error: Failed to search!") return HttpResponse(json.dumps(re), content_type = 'application/json') except: re['error'] = error(299,'Unknown Error!') return HttpResponse(json.dumps(re),content_type = 'application/json') if "min_workday" in request.GET.keys(): if request.GET["min_workday"] != 0: try: min_workday = request.GET["min_workday"] qs = qs.filter(days_per_week__gte = min_workday) except (ValueError): re['error'] = error(234,"Invaild search max daysperweek!") return HttpResponse(json.dumps(re), content_type = 'application/json') except (DatabaseError): re['error'] = error(251,"Database error: Failed to search!") return HttpResponse(json.dumps(re), content_type = 'application/json') except: re['error'] = error(299,'Unknown Error!') return HttpResponse(json.dumps(re),content_type = 'application/json') if "max_workday" in request.GET.keys(): if request.GET["max_workday"] != 0: try: max_workday = int(request.GET["max_workday"]) qs = qs.filter(days_per_week__lte = max_workday) except (ValueError): re['error'] = error(234,"Invaild search max daysperweek!") return HttpResponse(json.dumps(re), content_type = 'application/json') except (DatabaseError): re['error'] = error(251,"Database error: Failed to search!") return HttpResponse(json.dumps(re), content_type = 'application/json') except: re['error'] = error(299,'Unknown Error!') return HttpResponse(json.dumps(re),content_type = 'application/json') if "salary_min" in request.GET.keys(): if len(request.GET["salary_min"]) > 0: try: sa_min = int(request.GET["salary_min"]) qs = qs.filter(salary_max__gte = sa_min) except (ValueError): re['error'] = error(235,"Invaild search min salary!") return HttpResponse(json.dumps(re), content_type = 'application/json') except (DatabaseError): re['error'] = error(251,"Database error: Failed to search!") return HttpResponse(json.dumps(re), content_type = 'application/json') except: re['error'] = error(299,'Unknown Error!') return HttpResponse(json.dumps(re),content_type = 'application/json') if "salary_max" in request.GET.keys(): if len(request.GET["salary_max"]) > 0: try: sa_max = int(request.GET["salary_max"]) qs = qs.filter(salary_min__lte = sa_max) except (ValueError): re['error'] = error(236,"Invaild search max salary!") return HttpResponse(json.dumps(re), content_type = 'application/json') except (DatabaseError): re['error'] = error(251,"Database error: Failed to search!") return HttpResponse(json.dumps(re), content_type = 'application/json') except: re['error'] = error(299,'Unknown Error!') return HttpResponse(json.dumps(re),content_type = 'application/json') if "part_or_full_time" in request.GET.keys(): if request.GET["part_or_full_time"] != '0' or request.GET["part_or_full_time"] != '1': re['error'] = error(262,'Position can only be part time or full time') return HttpResponse(json.dumps(re),content_type = 'application/json') else: try: poft = request.GET["part_or_full_time"] part_or_full_time = int(poft) qs = qs.filter(part_or_full_time = part_or_full_time) except (DatabaseError): re['error'] = error(251,"Database error: Failed to search!") return HttpResponse(json.dumps(re), content_type = 'application/json') except: re['error'] = error(299,'Unknown Error!') return HttpResponse(json.dumps(re),content_type = 'application/json') if "status" in request.GET.keys(): if len(request.GET["status"]) > 0: try: status = request.GET["status"] assert status in STATUS qs = qs.filter(status = status) except (AssertionError): re['error'] = error(237,"Invaild search status!") return HttpResponse(json.dumps(re), content_type = 'application/json') except (DatabaseError): re['error'] = error(251,"Database error: Failed to search!") return HttpResponse(json.dumps(re), content_type = 'application/json') except: re['error'] = error(299,'Unknown Error!') return HttpResponse(json.dumps(re),content_type = 'application/json') page = 1 if "page" in request.GET.keys(): if len(request.GET["page"]) > 0: try: page = int(request.GET["page"]) assert page > 0 except (ValueError,AssertionError): re['error'] = error(200,"Invaild request!") return HttpResponse(json.dumps(re), content_type = 'application/json') except: re['error'] = error(299,'Unknown Error!') return HttpResponse(json.dumps(re),content_type = 'application/json') #todo fen ye #orderValue = "" #qs.order_by(orderValue) shang = qs.count() / POSITIONS_PER_PAGE yushu = 1 if qs.count() % POSITIONS_PER_PAGE else 0 page_number = shang + yushu qs = qs[(page - 1) * POSITIONS_PER_PAGE: page * POSITIONS_PER_PAGE] positions = json.loads(qs.to_json()) for position in positions: try: #print position["company"]["$oid"] company = Companyinfo.objects.get(id=position["company"]["$oid"]) position["company"] = json.loads(company.to_json()) except DoesNotExist: re['error'] = error(105,'Companyinfo does not exist!') return HttpResponse(json.dumps(re),content_type = 'application/json') re['positions'] = positions re['page_number'] = page_number re['page'] =page re["error"] = error(1,"Search succeed!") return HttpResponse(json.dumps(re),content_type = 'application/json')