def get(self): # Get the current logged-in user user = users.get_current_user() # List all approved providers for use by others providers = dal.approved_providers() # Expose as JSON output_obj = [] for provider in providers: provider_obj = { 'id': provider.key().id(), 'name': provider.name, 'website': provider.website, 'logo': provider.logo_url(128) } provider_obj['website'] = provider.website output_obj.append(provider_obj) # Output as JSON self.response.out.write(json.dumps(output_obj))
def post(self): # Check if the gave us a uid if self.request.get('q'): session_store = sessions.get_store(request=self.request) session = session_store.get_session() # Check if their token matches our if 'search_token' in session: if session['search_token'] == self.request.get('search_token'): del session['search_token'] # Save the deleted token session_store.save_sessions(self.response) # Trim and check the search term. We want to avoid any errors and keep # it consistant for all the providers search_term = str(self.request.get('q')).strip().replace(' ', '').replace('\t', '').replace('\n', '').replace('\r', '') # If the term is not a valid length we redirect the user back. if len(search_term) < 3: self.redirect('/') # Get all the searchable DAL's in # the order that we will search them. providers = dal.approved_providers() # Run the Search Runner to request from all providers response = runner.search(self.request, search_term, providers) # Redirect to the Search's token so the user # can view the result. This also keeps them away from # executing this page multiple times as that would # be bad! self.redirect('/view/' + str(response.token)) # Save for Stats. This is done Async dal.update_or_add_search_counter(self.request, response).get_result() else: # Nope so redirect them back to the hompage self.redirect('/') else: # Nope so redirect them back to the hompage self.redirect('/') else: # Nope so redirect them back to the hompage self.redirect('/')
def get(self): # Get the current logged-in user user = users.get_current_user() # Get all the Approved Providers providers = dal.approved_providers() locales = { 'title': 'Registered Providers', 'description': 'Browse all our Registered Providers. These providers have been approved by us and they had to pass numerous requirements to be considered valid.', 'user': user, 'providers': providers, 'provider_count': providers.count(), 'session': dal.return_and_global_session_update(self), 'is_current_user_admin': users.is_current_user_admin() } template = jinja_environment.get_template('provider/list.html') self.response.out.write(template.render(locales))
def post(self): # Check if they gave a token if self.request.get('token'): # They did so now let's check the client client_obj = authenticate_client(str(self.request.get('token'))) # Check client if client_obj and client_obj is not False: # Found the client. Now check if they are still in their dails quota ! # We only allow as many results as assigned to the client. # We want the option to change this value for certain users that have # big volume sites. if the count is 0 that means we allow unlimited calls. # We allow 0 clients as our site uses this api too for the javascript calls # Get the current date and year current_date = int(time.strftime("%d")) current_month = int(time.strftime("%m")) current_year = int(time.strftime("%Y")) # get the calls search_apis_calls = dal.search_api_calls(client_obj, current_date, current_month, current_year) # Local Var with Limit daily_limit_local = search_apis_calls.count() # If the count of calls bigger than 0 if search_apis_calls is not False and ( client_obj.daily_limit == 0 or daily_limit_local < client_obj.daily_limit ): # Check if the Q parameter was given for a search if self.request.get('q') and len(self.request.get('q')) >= 3: # Well let's do some searches ! # Trim and check the search term. We want to avoid any errors and keep # it consistant for all the providers search_term = str(self.request.get('q')).strip().replace(' ', '').replace('\t', '').replace('\n', '').replace('\r', '') # Insert info about the call inserted_call = {} # Get all the searchable DAL's in # the order that we will search them. providers = dal.approved_providers() # Run the Search Runner to request from all providers search_raw_response = runner.search(self.request, search_term, providers) # Results # We just show quick info. Such as name, pic and some basic info. # If the user wants to send a E-Mail they can do so with the specified contact url. # This is to protected the E-Mail of address of the owner. (search_obj, search_responses) = dal.search_by_token(str(search_raw_response.token)) # Make the result text result_text = 'notfound' # Check if the response was a success if search_obj.provider_success_responses > 0: result_text = 'found' # Create the results success_results = [] failure_results = [] # Loop and add the diffrent results for response in search_responses: # Create the Provider Obj provider_obj = {} provider_obj['id'] = response.provider.key().id() provider_obj['name'] = response.provider.name provider_obj['website'] = response.provider.website provider_obj['logo'] = response.provider.logo_url(128) if response.status == runner.ProviderResponse.STATUS_FOUND: # Parse to get details data = res = json.loads(response.parsed_response) # Assign params res['owner_name'] = data['owner']['name'] res['contact_url'] = 'http://www.identichip.org/apis/v1/contact?token=' + str(self.request.get('token')) + "&key=" + str(search_obj.token) + "&provider=" + str(response.provider.key()) # Remove owner details del res['owner'] # Assign provider params res['provider'] = provider_obj # Add to list success_results.append(res) elif response.status not in [runner.ProviderResponse.STATUS_FOUND, runner.ProviderResponse.STATUS_NOTFOUND]: # Add the failed provider failure_results.append(provider_obj) # Well we just added a count daily_limit_local += 1 # Redirect to the Search's token so the user # can view the result. This also keeps them away from # executing this page multiple times as that would # be bad! self.response.out.write(json.dumps({ 'result': result_text, 'token': str(search_obj.token), 'url': 'http://www.identichip.org/view/' + str(search_obj.token), 'success': success_results, 'failed': failure_results, 'daily_limit': client_obj.daily_limit, 'remaining_limit': int(client_obj.daily_limit) - daily_limit_local })) # Save client call client_call = schemas.APICallCount() client_call.date = current_date client_call.month = current_month client_call.year = current_year client_call.uid = search_term client_call.client = client_obj client_call.search = search_obj db.put_async(client_call).get_result() # Save for Stats. This is done Async. This is the global search stat dal.update_or_add_search_counter(self.request, search_raw_response).get_result() else: # No UID to search ??? self.response.out.write(json.dumps({ 'error': 'No q parameter was given ! This parameter tells us what UID to search for. Which is quite imporant ... Please see the developer documentation for this at http://www.identichip.org/developer' })) else: # Inform them self.response.out.write(json.dumps({ 'error': 'This Token has exceeded it\'s daily call limit of ' + str(client_obj.daily_limit) + ". If your client requires more please get in contract with us as we can arrange custom plans." })) else: # Inform them self.response.out.write(json.dumps({ 'error': 'No such client found. Invalid Token !' })) else: # Inform them self.response.out.write(json.dumps({ 'error': 'No Client token was given. Please login and create a client to start searching from our providers' }))