Beispiel #1
0
def get_nearest(**kwargs):
    search_country   = kwargs.get('country',   None)
    search_latitude  = float(kwargs.get('latitude',  None))
    search_longitude = float(kwargs.get('longitude', None))
    limit_count      = kwargs.get('count', 10)
    list_of_people   = []
    if search_country:
        list_of_people.extend(Person.view('%s/by_country' % Person._meta.app_label, 
                                          key = search_country, 
                                          include_docs = True).all())
    if search_latitude:
        latitude = window = 0
        while window <= 90 and len(list_of_people) <= limit_count:
            window = 5 * latitude
            latitude += 1
            list_of_people.extend(Person.view('%s/by_latitude' % Person._meta.app_label,
                                              startkey = search_latitude - window, 
                                              endkey   = search_latitude + window,
                                              include_docs = True).all())
    if search_longitude:
        longitude = window = 0
        while window > 180 and len(list_of_people) <= limit_count:
            window = 5 * longitude
            longitude += 1
            list_of_people.extend(Person.view('%s/by_longitude' % Person._meta.app_label, 
                                              startkey = search_longitude - window, 
                                              endkey   = search_longitude + window,
                                              include_docs = True).all())
    return_list = []
    for people in list_of_people:
        person_json = people.to_json()
        if person_json not in return_list:
            return_list.append(person_json)
    return return_list
Beispiel #2
0
 def do_profile(self, request):
     if request.method == "GET":
         user_data = Person.view("world/by_user_id", key=request.user.id, include_docs=True).first()
         if user_data:
             form = PersonForm(data=user_data.to_json())
             ctx_dict = {"form": form}
             if form.data.get("photo", None):
                 photo = urlparse.urljoin(settings.MEDIA_URL, "site_media/%s" % form.data["photo"])
                 ctx_dict.update({"photo": photo})
         else:
             ctx_dict = {"form": PersonForm(initial={"email": request.user.email})}
         return render(request, "person/edit_profile.html", ctx_dict)
     user_data = request.POST.copy()
     # Stuff the user id and full name
     # The full name is stored now to prevent HTTP requests later
     user_data["user_id"] = request.user.id
     user_data["email"] = request.user.email
     user = User.view(
         "%s/users_by_username" % User._meta.app_label, key=request.user.username, include_docs=True
     ).first()
     user_data["full_name"] = user.get_full_name()
     vf = PersonForm(user_data, request.FILES)
     if vf.is_valid():
         # Let's handle the image first.
         if vf.cleaned_data.get("photo"):
             photo_name = request.FILES["photo"].name
             base_upload_path = os.path.basename(os.path.abspath(settings.PROFILE_PIC_UPLOAD_PATH))
             upload_path = os.path.join(settings.PROFILE_PIC_UPLOAD_PATH, request.user.id)
             if not os.path.exists(upload_path):
                 os.mkdir(upload_path)
             img_file = open(os.path.join(upload_path, photo_name), "w")
             img_file.write(request.FILES["photo"].read())
             relative_dir = os.path.join(base_upload_path, os.path.basename(upload_path))
             vf.cleaned_data.update({"photo": os.path.join(relative_dir, os.path.basename(img_file.name))})
             img_file.close()
         data = Person.view("world/by_user_id", key=request.user.id, include_docs=True).first()
         if not data:
             data = Person()
         for (field, val) in vf.cleaned_data.items():
             setattr(data, field, val)
         data.save()
         return render(
             request,
             "person/edit_profile.html",
             {
                 "form": PersonForm(vf.cleaned_data),
                 "photo": vf.cleaned_data.get("photo"),
                 "message": ugettext("Successfully saved the data."),
             },
         )
     return render(
         request,
         "person/edit_profile.html",
         {"form": vf, "message": ugettext("Please correct the errors on the page")},
     )
Beispiel #3
0
 def do_index(self, request):
     if request.user.is_anonymous():
         return render(
             request,
             "person/index.html",
             {
                 "countries": Person.view("world/by_country_summary", group=True).iterator(),
                 "batches": Person.view("world/by_batch_summary", group=True).iterator(),
                 "total_people": Person.view("world/by_user_id").count(),
                 "by_batch": Person.view("world/by_batch_summary", group=True).iterator(),
                 "recent_regs": Person.view(
                     "world/by_registration_date", limit=100, include_docs=True, descending=True
                 ).iterator(),
             },
         )
     # Show people nearest to the user.
     user_data = Person.view("world/by_user_id", key=request.user.id, include_docs=True).first()
     if not user_data:
         return HttpResponseRedirect("/profile/")
     return render(
         request,
         "person/nearest.html",
         {
             "latitude": user_data.latitude,
             "longitude": user_data.longitude,
             "total_people": Person.view("world/by_user_id").count(),
         },
     )
Beispiel #4
0
 def _do_clean_data(self, view_name, view_key, cols_list):
     data = Person.view(view_name, key=view_key, include_docs=True).all()
     cleaned_data = []
     for row_dict in data:
         short_dict = SortedDict(zip(cols_list, map(lambda x: None, cols_list)))
         row_json = row_dict.to_json()
         for key in row_json:
             if key in cols_list:
                 short_dict.update({key: row_json[key]})
         if short_dict not in cleaned_data:
             cleaned_data.append(short_dict)
     return cleaned_data
Beispiel #5
0
 def do_user(self, request, user):
     ctx_dict = {}
     user_data = Person.view("world/by_user_id", key=user, include_docs=True).first()
     if user_data:
         ctx_dict = {}
         data = PersonSortedDict
         for ii, jj in user_data.to_json().items():
             if ii not in EXCLUDE_LIST:
                 data.update({ii: jj})
         for (key, val) in data.items():
             if not val or isinstance(val, Field):
                 data.pop(key, None)
         ctx_dict.update({"user_data": data})
     return render(request, "person/user_profile.html", ctx_dict)
Beispiel #6
0
 def do_getNearest(self, request, user):
     user_data = Person.view("world/by_user_id", key=user).first()
     nearest_people = []
     if user_data:
         nearest_people = get_nearest(latitude=user_data.latitude, longitude=user_data.longitude)
     return HttpResponse(json.dumps(nearest_people), mimetype="application/json")