def plan(request, location): context_dict ={} # A HTTP POST? if request.method == 'POST': form = MyForm(request.POST) planner = Planner.objects.get(user=request.user) tripForm = CreateTrip(request.POST) # Have we been provided with a valid form? if form.is_valid() and tripForm.is_valid(): # Save the new category to the database. trip = tripForm.save(commit=False) trip.planner=planner #Planner will be sent automatically eventually trip.save() data = form.cleaned_data['places'] for place in data: visit = Visit(trip=trip, place=place) visit.save() # The user will be shown the summary page. return redirect(reverse('tripSummary', args=[trip.id] )) else: # The supplied form contained errors - just print them to the terminal. print form.errors context_dict['errors'] = form.errors else: places = get_places(location) form = CreateTrip() # If the request was not a POST, display the form to enter details. context_dict['places'] = places context_dict['tripForm'] = form return render(request, 'plan.html', context_dict)
def get(self, request, ip, *args, **kw): print "Requested IP: {}".format(ip) details_request = IPDetails(ip, *args, **kw) # Track API usage alien_vault_id = request.COOKIES.get(COOKIE_NAME, None) if alien_vault_id is None: user = User() else: try: user = User.objects.get(alien_vault_id=alien_vault_id) except User.DoesNotExist: user = User() user.save() visit = Visit( user=user, address=request.META.get('REMOTE_ADDR'), # TODO: Handle X-Forwarded-For endpoint='api/threat/ip/{}'.format(ip) ) visit.save() result = DetailsSerializer(details_request) response = Response(result.data, status=status.HTTP_200_OK) response.set_cookie( COOKIE_NAME, user.alien_vault_id, max_age=COOKIE_MAX_AGE ) return response
def production_seen(request, play_id, play, production_id, type): production = check_parameters(play_id, play, production_id) if type == 'add': alert = Visit(user=request.user, production=production) try: alert.save() except IntegrityError, e: if e.args[0] != 1062: # Duplicate raise messages.success(request, u"Your visit has been recorded.")
def create(self, request, **kwargs): serializer = RectSerializer(data=request.data, many=True) if serializer.is_valid(): serializer.save() # We receive a list of Rects with the same picture, so we just # take the picture from the first Rect. picture_obj = serializer.validated_data[0]['picture'] image = picture_obj.picture # Create the Recognizer and pass all available faces recognizer = VisitorRecognizer(VisitorFaceSample.objects.all()) recognizer.train_model() # Build an URL where the clients can download the image picture_url = request.build_absolute_uri(image.url) response_dict = { "visitors": [], "people": 0, "picture_url": picture_url } # Create a new visit visit = Visit(picture=picture_obj) visit.save() # Try to recognize people in the incoming picture visitors, faces = recognizer.recognize_visitor(image) # Add the recognized visitors to the Visit and to the JSON response for visitor_id, confidence in visitors: visitor = Visitor.objects.get(pk=visitor_id) visit.visitors.add(visitor) visitor_dict = {"name": visitor.name, "confidence": confidence} response_dict["visitors"].append(visitor_dict) # Save and send the number of people in the visit response_dict["people"] = visit.people = faces visit.save() # Pack the data (url and visitor data) in json json_response = json.dumps(response_dict) # Send the data to the xmpp server where the devices are listening xmppconnector.connect_and_send(json_response) # Return an empty response since we don't need to inform anything return Response()
def get(self, request, *args, **kwargs): ip = kwargs['ip'] # User's source address x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') if x_forwarded_for: user_addr = x_forwarded_for.split(',')[0] else: user_addr = request.META.get('REMOTE_ADDR') # Generate IPDetails object for serialization details_request = IPDetails(ip, *args, **kwargs) # Serialize object result = DetailsSerializer(details_request) response = Response(result.data, status=status.HTTP_200_OK) # Set UTC time current_datetime = datetime.utcnow() epoch = int((current_datetime - datetime(1970, 1, 1)).total_seconds()) expires = addYear(current_datetime, 1) # Handle Cookies if 'alienvaultid' in request.COOKIES: avid = request.COOKIES['alienvaultid'] response.set_cookie('last_visit', epoch, expires=expires) else: # Didn't find cookie, set one. avid = ''.join(["%s" % random.randint(0, 9) for num in range(0, 11)]) response.set_cookie('alienvaultid', avid, expires=expires) response.set_cookie('last_visit', epoch, expires=expires) user = User(alienvaultid=avid) user.save() visit = Visit(user=user, address=user_addr, timestamp=epoch, endpoint=kwargs['endpoint'] + ip) visit.save() return response
def get(self, request, *args, **kwargs): ip = kwargs['ip'] # User's source address x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') if x_forwarded_for: user_addr = x_forwarded_for.split(',')[0] else: user_addr = request.META.get('REMOTE_ADDR') # Generate IPDetails object for serialization details_request = IPDetails(ip, *args, **kwargs) # Serialize object result = DetailsSerializer(details_request) response = Response(result.data, status=status.HTTP_200_OK) # Set UTC time current_datetime = datetime.utcnow() epoch = int((current_datetime - datetime(1970, 1, 1)).total_seconds()) expires = addYear(current_datetime, 1) # Handle Cookies if 'alienvaultid' in request.COOKIES: avid = request.COOKIES['alienvaultid'] response.set_cookie('last_visit', epoch, expires=expires) else: # Didn't find cookie, set one. avid = ''.join( ["%s" % random.randint(0, 9) for num in range(0, 11)]) response.set_cookie('alienvaultid', avid, expires=expires) response.set_cookie('last_visit', epoch, expires=expires) user = User(alienvaultid=avid) user.save() visit = Visit(user=user, address=user_addr, timestamp=epoch, endpoint=kwargs['endpoint'] + ip) visit.save() return response
def create(self, request, **kwargs): serializer = RectSerializer(data=request.data, many=True) if serializer.is_valid(): serializer.save() # We receive a list of Rects with the same picture, so we just # take the picture from the first Rect. picture_obj = serializer.validated_data[0]['picture'] image = picture_obj.picture # Create the Recognizer and pass all available faces recognizer = VisitorRecognizer(VisitorFaceSample.objects.all()) recognizer.train_model() # Build an URL where the clients can download the image picture_url = request.build_absolute_uri(image.url) response_dict = {"visitors": [], "people": 0, "picture_url": picture_url} # Create a new visit visit = Visit(picture=picture_obj) visit.save() # Try to recognize people in the incoming picture visitors, faces = recognizer.recognize_visitor(image) # Add the recognized visitors to the Visit and to the JSON response for visitor_id, confidence in visitors: visitor = Visitor.objects.get(pk=visitor_id) visit.visitors.add(visitor) visitor_dict = {"name": visitor.name, "confidence": confidence} response_dict["visitors"].append(visitor_dict) # Save and send the number of people in the visit response_dict["people"] = visit.people = faces visit.save() # Pack the data (url and visitor data) in json json_response = json.dumps(response_dict) # Send the data to the xmpp server where the devices are listening xmppconnector.connect_and_send(json_response) # Return an empty response since we don't need to inform anything return Response()
def follow(request, token): logger.debug('Follow: %s' % token) try: link = Link.objects.get(token=token) except Link.DoesNotExist: raise Http404() ip = request.META.get('REMOTE_ADDR') referer = request.META.get('HTTP_REFERER') visit = Visit(link=link, ip=ip, referer=referer) visit.save() link.visits = F('visits') + 1 link.save() return HttpResponsePermanentRedirect(link.url)