Example #1
0
 def test_get_valid_profile2(self):
     request = Request()
     request.username = "******"
     request.save()
     twitterProfile = GetTwitterProfile(request)
     self.assertEqual(twitterProfile.name,
                      "Diario Olé (@DiarioOle) | Twitter")
Example #2
0
    def get(self, url):
      
      ip = self.request.remote_addr #to prevent abuses, only a request every minute is served
      
      request = Request.gql("where ip='%s'" % ip).get() #Look for request from the same IP address
      if not request is None:
        delta = request.is_allowed()
        
        if delta > 0: #too little time has passed from the previous request
          #self.error(408)   #Timeout Error
          self.response.set_status(408, "Your IP address has issued a request less than 1 min ago. Please wait %d seconds" % delta)
          return
      else:
        request = Request(ip=ip, page_crawled=url)
        request.save()
      
      self.response.headers['Content-Type'] = 'application/json'
      handler = CrawlerHandler()


      site_image = memcache.get(url)

      
      if site_image is None:
        home_page = handler.start_crawling(url, MAX_PAGE_DEPTH, MAX_PAGES_TO_CRAWL, 0.01)  #causes a little delay, but not too big (one 100th of a sec) 

        if home_page is None:
          self.error(400) #Bad Request
          return
        else:
          site_image = handler.page_graph(home_page)
          memcache.set(url, site_image)
      
      self.__responde(site_image) 
Example #3
0
 def test_get_valid_profile1(self):
     request = Request()
     request.username = "******"
     request.save()
     twitterProfile = GetTwitterProfile(request)
     self.assertEqual(twitterProfile.name,
                      "Mauricio Morero (@maurimorero) | Twitter")
Example #4
0
 def test_get_description(self):
     request = Request()
     request.username = "******"
     request.save()
     twitterProfile = GetTwitterProfile(request)
     self.assertEqual(twitterProfile.description,
                      "Jugador de Boca Juniors y Piola Vago frontman")
Example #5
0
 def test_request_account_suspended(self):
     request = Request()
     request.username = "******"
     request.save()
     twitterProfile = GetTwitterProfile(request)
     request = Request.objects.latest()
     self.assertEqual(request.status, "Account suspended")
Example #6
0
 def test_request_completed(self):
     request = Request()
     request.username = "******"
     request.save()
     twitterProfile = GetTwitterProfile(request)
     request = Request.objects.latest()
     self.assertEqual(request.status, "Completed")
Example #7
0
    def test_requests(self):
        request = Request(path = "/foo")
        request.save()
        django_response = HttpResponse(self.text)

        # No rules, should pass
        self.assertEqual(len(check_request_rules(request, django_response)), 0)

        # Should have one error now that the status is not 200
        django_response.status_code = 442
        self.assertEqual(len(check_request_rules(request, django_response)), 1)
        
        # set it back for the rest of the tests
        django_response.status_code = 200
        self.assertEqual(len(check_request_rules(request, django_response)), 0)

        rule1 = RequestRule(request = request, target = "content", operator = "contains", value = "Teletubbies")
        rule1.save()
        self.assertEqual(len(check_request_rules(request, django_response)), 0)

        # add a rule that fails
        rule2 = RequestRule(request = request, target = "content", operator = "!contains", value = "Teletubbies")
        rule2.save()
        self.assertEqual(len(check_request_rules(request, django_response)), 1)


        # Done testing requests, now test functionality on the models that have been created
        # str the request to test the __unicode__ method
        str(request) 

        # Test the display_operator
        self.assertEqual(rule2.display_operator, "does not contain")
        rule2.operator='foo'
        # Should not fail on unrecognized operator
        self.assertEqual(rule2.display_operator, "foo")
def create(request):

    if request.method=='POST':
        name = request.POST['username']
        email = request.POST['email']
        sex = request.POST['sex']
        mobile_number = request.POST['mobile_number']
        exam_city = request.POST['exam_city']
        current_city = request.POST['current_city']
        exam_date = request.POST['exam_date']

        if exam_date == '' or name == '' or email == '' or mobile_number == '' :
            return render_to_response('share/create.html')
        else :
            new_obj = Users(name = name, email = email, sex = sex, mobile_number = mobile_number,exam_city_id = exam_city,exam_date = exam_date, current_city = current_city)
            new_obj.save()
            if "requested_to" in request.session:

                obj = Request(requester = new_obj.id,requested_to = request.session["requested_to"])
                obj.save()
                del request.session["requested_to"]
                return HttpResponseRedirect('/thanks/')
            return HttpResponseRedirect('/thankyou/')
    if "exam_city" in request.session:
        return render_to_response('share/create.html',{'exists':1,'exam_date':request.session["exam_date"]})

    return render_to_response('share/create.html',{'exists':0})
Example #9
0
 def test_request_account_not_found(self):
     request = Request()
     request.username = "******"
     request.save()
     twitterProfile = GetTwitterProfile(request)
     request = Request.objects.latest()
     self.assertEqual(request.status, "User not found")
Example #10
0
    def test_login_as(self):
        user = User.objects.create_user(username = TEST_USER, password = TEST_PASS, email = TEST_USER)

        request = Request(path = "/foo", login_as_user = user)
        request.save()

        response = create_django_response(request)
        self.assertTrue(response.context["user"].is_authenticated())
    def process_request(self, request):

        method = request.method
        path = request.path
        time = datetime.now().strftime('%d/%b/%Y %H:%M:%S')
        if not request.is_ajax() and path != '/requests/':
            request = Request(method=method, path=path, time=time)
            request.save()
Example #12
0
 def test_get_imageURI(self):
     request = Request()
     request.username = "******"
     request.save()
     twitterProfile = GetTwitterProfile(request)
     self.assertEqual(
         twitterProfile.imageURI,
         "https://pbs.twimg.com/profile_images/959194538182103041/q6heJEdD_400x400.jpg"
     )
Example #13
0
 def process_request(self, request):
     data = {}
     data['Path'] = request.path
     data['Method'] = request.method
     data['Meta'] = str(request.META)
     req = Request(request=data)
     if request.user.is_authenticated():
         req.priority = 1
     req.save()
     return None
Example #14
0
    def test_login_as(self):
        user = User.objects.create_user(username=TEST_USER,
                                        password=TEST_PASS,
                                        email=TEST_USER)

        request = Request(path="/foo", login_as_user=user)
        request.save()

        response = create_django_response(request)
        self.assertTrue(response.context["user"].is_authenticated())
Example #15
0
def home(request):
    if request.method == 'POST':
            song_req = Request()
            song_req.user = request.POST.get('user', '')
            song_req.song = request.POST.get('song', '')
            song_req.message = request.POST.get('message', '')
            song_req.save()
    else:
        req = get_latest_request()
        listeners = get_listeners()
        title = get_title()
        return render_to_response('home.html', {'latest':req, 'listeners':listeners, 'title':title})
Example #16
0
def TakeRequest(request, url):
    request_url = URL(url)
    robj = Request.objects.filter(target__contains=request_url.host)
    if not robj:
        r = Request(request_type='HttpRequest', storage_type='UrlStorage', 
                    action='mirror', target=request_url.host, flag='requested')
    else:
        r = robj[0]
        r.flag = 'requested'

    r.save()

    return HttpResponseRedirect('http://%s/%s' % (getifip(web_server_interface), request_url.with_www().replace('http://', '')))
Example #17
0
def profile_edit(request, l):
    error_msg = None
    me = l.get_ldap_node(request.session['ldap_binddn'])

    if request.method == 'POST':
        f = ProfileForm(request.POST)
        if f.is_valid():
            me.displayName = f.cleaned_data['name']
            me.cn = f.cleaned_data['nick']
            me.save()

            passwd_new = f.cleaned_data['passwd']
            email_new = f.cleaned_data['email']

            if passwd_new:
                me.set_password(passwd_new)
                request.session['ldap_passwd'] = passwd_new

            if email_new != str(me.mail):
                req = Request()
                req.type = Request.EMAIL
                req.uid = me.uid
                req.email = email_new
                req.save()

                t = loader.get_template('accounts/email_email_request')
                c = Context({
                        'name': me.displayName,
                        'url': request.build_absolute_uri(
                                         reverse(process, kwargs={ 'token': req.token })),
                        'expire_in': settings.REQ_EXPIRE_STR,
                        })
                send_mail(u'Confirmation email FedeRez', t.render(c), settings.EMAIL_FROM,
                          [req.email], fail_silently=False)

            return HttpResponseRedirect('/')

    else:
        f = ProfileForm(label_suffix='', initial={ 'email': me.mail,
                                                   'name': me.displayName,
                                                   'nick': me.cn })

    c = { 'form': f,
          'name': me.displayName,
          'nick': me.cn,
          'email': me.mail,
          'error_msg': error_msg, }
    c.update(csrf(request))

    return render_to_response('accounts/edit.html', c,
                              context_instance=RequestContext(request))
    def registerOptimizationRequest(request, user):
        assert user is not None
        ip = RequestUtils.getIPAddress(request)
        url = request.get_full_path()

        req = Request(ip=ip, url=url, request_user=user)
        req.save()

        optreq = OptimizationRequest(ip=ip,
                                     url=url,
                                     request=req,
                                     request_user=user)
        #req = Request(ip=ip,url=url,request_user = user)
        optreq.save()

        return optreq.id
    def process_request(self, request):

        path_info = request.META['PATH_INFO']

        exlude_list = [reverse('request-counter'),
                       reverse('admin:jsi18n'),
                       settings.MEDIA_URL,
                       settings.STATIC_URL]

        if not any(url in path_info for url in exlude_list):
            new_http_request = Request()
            new_http_request.method = request.META['REQUEST_METHOD']
            new_http_request.path_info = path_info
            new_http_request.server_protocol = request.META['SERVER_PROTOCOL']
            new_http_request.viewed = False
            new_http_request.save()
Example #20
0
File: views.py Project: eknuth/pimh
def local_search(request, place_type):
    if request.GET.get('coords', ''):
        form = SearchForm(request.GET)
        if form.is_valid():
            (lat,lon) = form.cleaned_data['coords'].split(',')
            search_point = Point(float(lat),float(lon))
            r = Request(point=search_point, place_type=place_type)
            r.save()
            places = Place.objects.distance(search_point).filter(place_type=place_type).filter(point__distance_lte=(search_point, distance(mi=2))).order_by('distance')
            return render_to_response('_local_search.html', {
                    'title': place_type,
                    'places': places
                })

    else:
        form = SearchForm()
        return render_to_response('search.html', {
                'form': form,
                })
Example #21
0
File: views.py Project: eknuth/pimh
def lookup(request):
    if request.GET.get('coords', ''):
        form = SearchForm(request.GET)
        if form.is_valid():
            (lat,lon) = form.cleaned_data['coords'].split(',')
            pnt=Point(float(lat),float(lon))
            n = get_neighborhood_by_point(pnt)
            r = Request(point=pnt, place_type="neighborhood")
            r.save()
            search_response = {'name': n.name.title(), 'polygon': n.gpoly(), 
                               'slug': n.slug, 'wiki': n.wiki,
                               'centroid_x': "%.5f" % n.poly.centroid.x,
                               'centroid_y': "%.5f" % n.poly.centroid.y}
            return HttpResponse(simplejson.dumps(search_response),
                                    mimetype='application/json')

    else:
        form = SearchForm()
        return render_to_response('search.html', {
                'form': form,
                })
Example #22
0
def get_or_create_request():
    '''
    Returns a request instance that is global for this request
    If this function is called outside of a web request then the user_name is marked as system
    '''
    thread_ident = thread.get_ident()
    request, rq = STATE.get(thread_ident, (None, None))
    if not rq:
        rq = Request()
        if request:
            rq.request_path = request.path
            if request.user.is_anonymous():
                rq.user_name = u'(Anonymous)'
            else:
                rq.user_pk = request.user.pk
                rq.user_name = unicode(request.user)[:255]
        else:
            rq.user_name = u'(System)'
        rq.save()
        STATE[thread_ident] = (request, rq)
    return rq
Example #23
0
def services(request):
    r = Request(request_method=request.method,
                request_path=request.path,
                request_accept=request.META['HTTP_ACCEPT'],
                request_body=request.body)
    try:
        action = Action.objects.get(request_method=request.method,
                                    request_path=request.path,
                                    request_accept=request.META['HTTP_ACCEPT'])
    except ObjectDoesNotExist:
        r.response_status_code = 404
        r.save()
        return HttpResponse(status=404)
    else:
        r.response_status_code = action.response_status_code
        r.response_content = action.response_content
        r.response_content_type = action.response_content_type
        r.save()
        return HttpResponse(status=action.response_status_code,
                            content=action.response_content,
                            content_type=action.response_content_type)
Example #24
0
    def test_requests(self):
        request = Request(path="/foo")
        request.save()
        django_response = HttpResponse(self.text)

        # No rules, should pass
        self.assertEqual(len(check_request_rules(request, django_response)), 0)

        # Should have one error now that the status is not 200
        django_response.status_code = 442
        self.assertEqual(len(check_request_rules(request, django_response)), 1)

        # set it back for the rest of the tests
        django_response.status_code = 200
        self.assertEqual(len(check_request_rules(request, django_response)), 0)

        rule1 = RequestRule(request=request,
                            target="content",
                            operator="contains",
                            value="Teletubbies")
        rule1.save()
        self.assertEqual(len(check_request_rules(request, django_response)), 0)

        # add a rule that fails
        rule2 = RequestRule(request=request,
                            target="content",
                            operator="!contains",
                            value="Teletubbies")
        rule2.save()
        self.assertEqual(len(check_request_rules(request, django_response)), 1)

        # Done testing requests, now test functionality on the models that have been created
        # str the request to test the __unicode__ method
        str(request)

        # Test the display_operator
        self.assertEqual(rule2.display_operator, "does not contain")
        rule2.operator = 'foo'
        # Should not fail on unrecognized operator
        self.assertEqual(rule2.display_operator, "foo")
Example #25
0
def process(request):
    if request.method == 'POST':
        form = UploadedFileForm(request.POST, request.FILES)
        if form.is_valid():
            motive = form.cleaned_data["motive"]
            instances_used = form.cleaned_data["instance_num"]
            seed_content = request.FILES['file'].read()


            fetcher = Fetcher("fetch_queue", instances_used)
            instances_ids = fetcher.fetch(seed_content)

            #Could not parse directly from form because of the file
            req_model = Request()
            req_model.motive = motive
            req_model.instances_used = instances_used
            req_model.user = request.user
            req_model.content = seed_content
            req_model.instances_ids = json.dumps(instances_ids)
            req_model.save()

            return HttpResponseRedirect("/process/%d" % req_model.id)
Example #26
0
def create(request):

    if request.method == 'POST':
        name = request.POST['username']
        email = request.POST['email']
        sex = request.POST['sex']
        mobile_number = request.POST['mobile_number']
        exam_city = request.POST['exam_city']
        current_city = request.POST['current_city']
        exam_date = request.POST['exam_date']

        if exam_date == '' or name == '' or email == '' or mobile_number == '':
            return render_to_response('share/create.html')
        else:
            new_obj = Users(name=name,
                            email=email,
                            sex=sex,
                            mobile_number=mobile_number,
                            exam_city_id=exam_city,
                            exam_date=exam_date,
                            current_city=current_city)
            new_obj.save()
            if "requested_to" in request.session:

                obj = Request(requester=new_obj.id,
                              requested_to=request.session["requested_to"])
                obj.save()
                del request.session["requested_to"]
                return HttpResponseRedirect('/thanks/')
            return HttpResponseRedirect('/thankyou/')
    if "exam_city" in request.session:
        return render_to_response('share/create.html', {
            'exists': 1,
            'exam_date': request.session["exam_date"]
        })

    return render_to_response('share/create.html', {'exists': 0})
Example #27
0
 def process_request(self, request):
     r = Request()
     r.date = datetime.now()
     r.url = request.get_full_path()
     r.ip = request.get_host()
     r.save()
Example #28
0
 def process_request(self, request):
     stored_request = Request()
     for f in ('META', 'path'):
         setattr(stored_request, f.lower(), str(getattr(request, f)))
     stored_request.path = str(request.path)
     stored_request.save()
Example #29
0
 def mutate(self, info, **kwargs):
     request = RequestModel(**kwargs)
     request.save()
     return CreateRequest(request=request)
Example #30
0
def requests(request):
    was_limited = getattr(request, 'limited', False)
    if was_limited:
        return JsonResponse(
            {
                'status': 'you hit the rate limit!'
            }
        )
    else:
        token = get_token(request)
        user_id_login = User.verify_auth_token(token)
        if user_id_login:
            if request.method == 'GET':
                all_requests = Request.objects.all()
                data = []
                for request_instance in all_requests:
                    item = {
                                'id': request_instance.id,
                                'user_id': request_instance.user_id.id,
                                'meal_type': request_instance.meal_type,
                                'location_string': request_instance.location_string,
                                'latitude': request_instance.latitude,
                                'longitude': request_instance.longitude,
                                'meal_time': str(request_instance.meal_time)
                            }
                    data.append(item)
                return JsonResponse(
                    {
                        'objects': data
                    }
                )
            elif request.method == 'POST':
                user = User.objects.get(id=user_id_login)
                if user:
                    meal_type = request.POST.get('meal_type')
                    location_string = request.POST.get('location_string')
                    latitude = request.POST.get('latitude')
                    longitude = request.POST.get('longitude')
                    meal_time = datetime.strptime(request.POST.get('meal_time'), "%Y %b %d %H:%M")
                    request_instance = Request(
                                                user_id=user,
                                                meal_type=meal_type,
                                                location_string=location_string,
                                                latitude=latitude,
                                                longitude=longitude,
                                                meal_time=meal_time
                                                )
                    request_instance.save()
                    return JsonResponse(
                        {
                            'status': 'instance create successfully',
                            'object':
                                {
                                    'id': request_instance.id,
                                    'user_id': request_instance.user_id.id,
                                    'meal_type': request_instance.meal_type,
                                    'location_string': request_instance.location_string,
                                    'latitude': request_instance.latitude,
                                    'longitude': request_instance.longitude,
                                    'meal_time': str(request_instance.meal_time)
                                }
                        }
                    )
                else:
                    return HttpResponseForbidden('user not exist!')
        else:
            return HttpResponseForbidden('invalid token')
Example #31
0
 def test_request_pending(self):
     request = Request()
     request.username = "******"
     request.save()
     request = Request.objects.latest()
     self.assertEqual(request.status, "Pending")
 def registerRequest(request, user):
     assert user is not None
     ip = RequestUtils.getIPAddress(request)
     url = request.get_full_path()
     req = Request(ip=ip, url=url, request_user=user)
     req.save()
Example #33
0
 def test_get_suspended_profile(self):
     request = Request()
     request.username = "******"
     request.save()
     twitterProfile = GetTwitterProfile(request)
     self.assertEqual(twitterProfile.id, -1)
Example #34
0
def get(request):
    if request.method in ['POST', 'GET']:
        url = request.GET.get('url', '')
        callback = request.GET.get('callback', '')
        tor = request.GET.get('tor', '')
        compress = request.GET.get('compress', '')

    # check valid url starts here
    if url != '' and url != 'http://alloworigin.com' and url != 'http://www.alloworigin.com':
        validate = URLValidator()
    else:
        return HttpResponse('invalid url')

    try:
        validate(url)
    except ValidationError:
        return HttpResponse('invalid url')
    # check valid url ends here

    # request
    try:
        if tor == '1':
            pytor_instance = pytor()
            try:
                # try to send tor request
                r = pytor_instance.get(url, timeout=3)
                origin = get_server_ip(tor='1', pytor_instance=pytor_instance)
            except Exception:
                # if tor not avail, use local instead
                r = requests.get(url, timeout=3)
                origin = get_server_ip()
        else:
            r = requests.get(url, timeout=3)
            origin = get_server_ip()

    except requests.exceptions.Timeout:
        return HttpResponse('timed out')

    this_request = Request(ip=get_client_ip(request), dest=url)
    this_request.save()

    if callback not in ['?', '']:
        if compress == '1':
            from base64 import b64encode as b64
            response = JsonResponse(
                {"contents": b64(r.text.encode('utf-8').encode('zlib_codec')),
                 "status_code": r.status_code,
                 "origin": origin, "destination": url}
                )
        else:
            response = JsonResponse({"contents": r.text,
                                     "status_code": r.status_code,
                                     "origin": origin,
                                     "destination": url})
        return HttpResponse(callback+"("+response.content+")",
                            content_type="application/json")
    else:
        if compress == '1':
            from base64 import b64encode as b64
            return JsonResponse(
                {"contents": b64(r.text.encode('utf-8').encode('zlib_codec')),
                 "status_code": r.status_code,
                 "origin": origin, "destination": url}
                )
        else:
            return JsonResponse(
                {"contents": r.text, "status_code": r.status_code,
                 "origin": origin, "destination": url}
                )
Example #35
0
def get(request):
    if request.method in ['POST', 'GET']:
        url = request.GET.get('url', '')
        callback = request.GET.get('callback', '')
        tor = request.GET.get('tor', '')
        compress = request.GET.get('compress', '')
        origin = 'http://www.alloworigin.com'
    else:
        return HttpResponse('invalid request')

    # check valid url starts here
    if url != '' and url != origin.replace('www.', '') and url != origin:
        validate = URLValidator()
    else:
        return HttpResponse('invalid url')

    try:
        validate(url)
    except ValidationError:
        return HttpResponse('invalid url')
    # check valid url ends here

    # check if within 10 seconds from now, the same url has been requested
    if is_abuse(get_client_ip(request), url):
        return HttpResponse('rate limited. 1 request per 5 seconds.')

    # request
    try:
        if tor == '1':
            pytor_instance = pytor()
            try:
                # try to send tor request
                r = pytor_instance.get(url, timeout=3)
                origin = get_server_ip(tor='1', pytor_instance=pytor_instance)
            except Exception:
                # if tor not avail, use local instead
                r = requests.get(url, timeout=3)
                origin = get_server_ip()
        else:
            r = requests.get(url, timeout=3)
            origin = get_server_ip()

    except requests.exceptions.Timeout:
        return HttpResponse('timed out')

    this_request = Request(ip=get_client_ip(request), dest=url)
    this_request.save()

    if callback not in ['?', '']:
        if compress == '1':
            from base64 import b64encode as b64
            response = JsonResponse(
                {"contents": b64(r.text.encode('utf-8').encode('zlib_codec')),
                 "status_code": r.status_code,
                 "origin": origin, "destination": url}
                )
        else:
            response = JsonResponse({"contents": r.text,
                                     "status_code": r.status_code,
                                     "origin": origin,
                                     "destination": url})
        return HttpResponse(callback+"("+response.content+")",
                            content_type="application/json")
    else:
        if compress == '1':
            from base64 import b64encode as b64
            return JsonResponse(
                {"contents": b64(r.text.encode('utf-8').encode('zlib_codec')),
                 "status_code": r.status_code,
                 "origin": origin, "destination": url}
                )
        else:
            return JsonResponse(
                {"contents": r.text, "status_code": r.status_code,
                 "origin": origin, "destination": url}
                )
Example #36
0
 def test_get_invalid_profile(self):
     request = Request()
     request.username = "******"
     request.save()
     twitterProfile = GetTwitterProfile(request)
     self.assertEqual(twitterProfile.id, -1)