def ajax(request): if request.method == "POST": volunteer = Volunteer() volunteer.first_name = request.POST['first_name'] volunteer.save() volunteer_list = list(Volunteer.objects.all()) ajax_list = [] for item in volunteer_list: ajax_list.append({ "first_name": item.first_name, "last_name": item.last_name, "email": item.email, }) return HttpResponse(dumps(ajax_list), content_type="application/json")
def parseFile(json_url, begin, end, args): resp = requests.get(url=json_url) data = resp.json() cols = max([int(i['gs$cell']['col']) for i in data['feed']['entry']]) rows = max([int(i['gs$cell']['row']) for i in data['feed']['entry']]) df = [['' for i in range(cols)] for j in range(rows)] for i in data['feed']['entry']: col = int(i['gs$cell']['col']) row = int(i['gs$cell']['row']) #print(i['gs$cell']) df[row - 1][col - 1] = i['gs$cell']['inputValue'] lb = [df[1][i] if df[0][i] == '' else df[0][i] for i in range(cols)] lb = [i[:15] for i in lb] rr = [{lb[i]: v for i, v in enumerate(j)} for j in df[2:]] ids = [] begin = int(begin) end = int(end) for row in rr[begin:end]: #add item = parseRow(row) #if args.get('legitimatie') is not None and item['received_contract']: item['is_active'] = True ob = Volunteer.objects(phone=item['phone']).first() if not ob: comment = Volunteer(**item) comment.save() ids.append(comment.clean_data()['_id']) #return jsonify(comment.clean_data()['_id']) elif 'latitude' in item: data = ob.clean_data() if 'latitude' not in data or data['latitude'] == '': ob.update(latitude=item['latitude'], longitude=item['longitude'], address=item['address']) return jsonify(ids)
def register_volunteer(request_json, created_by): """Creates and persists a new volunteer into database. Parameters ---------- request_json : dict A dictionary representing the volunteer profile details. created_by : str A string representing either name of user who is going to create a new volunteer, or the token Returns ------- 200: If the volunteer was successful created and saved. 400: If the volunteer wasn't created or saved, and there was raised some exception. """ log.debug("Relay offer for req:%s from ", request_json) try: if not vu.is_email(created_by): user = Operator.verify_auth_token(created_by) created_by = user.get().clean_data()['email'] if created_by == os.getenv("COVID_BACKEND_USER"): vu.exists_by_telegram_chat_id(request_json["chat_id"]) new_volunteer_data = process_volunteer_data_from_telegram(request_json) else: vu.exists_by_email(request_json["email"]) new_volunteer_data = request_json new_volunteer_data["password"] = PassHash.hash(new_volunteer_data["password"]) new_volunteer_data['created_by'] = created_by new_volunteer = Volunteer(**new_volunteer_data) new_volunteer.save() return jsonify({"response": "success", 'user': new_volunteer.clean_data()}) except Exception as error: return jsonify({"error": str(error)}), 400
def signup(request, pk): do_coordinator = False if is_coordinator(request.user) : template_name='signup/signup-coordinator.html' form = SignupFormCoordinator do_coordinator = True else: template_name='signup/signup.html' form = SignupFormUser job = Job.objects.get(pk=pk) if job == None : raise Http404("Job does not exist") # TODO: Check perimssions if request.method=='POST': form = form(request.POST) if form.is_valid() : try: with transaction.atomic() : signup_user = request.user if do_coordinator : # Check if the form has another user's email in it. other_user = form.cleaned_data.get('email', None) if other_user is not None and other_user != u'': for user in User.objects.filter(email__exact=other_user) : signup_user = user break # Create a Volunteer with form data # We need the natural key from the job... this way # if the job changes in a non-meaningful way this volunteer # continues to be valid. v = Volunteer( user = signup_user, comment = form.cleaned_data['comment'], source = job.source.pk, title = job.title, start = job.start, end = job.end, ) # NB: This feature requires discussion: Doing this will make it # impossible for people to sign up their friends for shifts. # # Before we commit this to the database let's check to see if the # user is already signed up for a shift at this time... #shifts = Volunteer.objects.filter(user__exact=request.user) #for s in shifts : # if s.start == v.start \ # or ( v.start < s.start and s.end < v.end ) \ # or ( v.start > s.start and v.start < s.end ) : # raise ValueError("Overlap!") # Now add the row, so the count query works... v.save() # Now check if there are too many volunteers. This has to # be done atomically. If we're overbooked, rollback. volcount = Volunteer.objects.filter(source__exact=job.source.pk, title__exact=job.title, start__exact=job.start).count() if volcount > job.needs : raise IntegrityError("f**k! nabbed!") except IntegrityError: return HttpResponse('Oh no! This signup was nabbed!', status=450) except ValueError: return HttpResponse('Wait a second!', status=451) return redirect('jobs', job.source.pk) else: return render(request, template_name, {'form':form, 'ret':job.source.pk, 'job':job}) else: form = form() return render(request, template_name, {'form':form, 'ret':job.source.pk, 'job':job})