def create(self, request): ''' Creates a relation record ''' from_person_id, from_person_id_valid = intTryParse(request.data.get('from_person_id')) to_person_id, to_person_id_valid = intTryParse(request.data.get('to_person_id')) relation_type, relation_type_valid = intTryParse(request.data.get('relation_type')) if not (from_person_id_valid and to_person_id_valid and relation_type_valid): return HttpResponse(status=400, content="Invalid to_person_id, from_person_id or relation_type") if relation_type not in (PARTNERED, RAISED, RAISED_BY): return HttpResponse(status=400, content="Invalid relation_type") if from_person_id == to_person_id: return HttpResponse(status=400, content="from_person_id cannot be to_person_id") # Ensure people exist person_queryset = Person.objects.filter(family_id = request.user.family_id) from_person = get_object_or_404(person_queryset, pk=from_person_id) to_person = get_object_or_404(person_queryset, pk=to_person_id) with reversion.create_revision(): relation = create_relation(request.user, from_person, to_person, relation_type) if not relation: raise Http404 # Store some meta-information. reversion.set_user(request.user) reversion.set_comment('Update ' + request.META.get('HTTP_X_REAL_IP')) serializer = RelationSerializer(relation, context={'request': request}) return Response(serializer.data)
def update(self, request, pk=None): queryset = Person.objects.filter(family_id = request.user.family_id) person = get_object_or_404(queryset, pk=pk) #Make sure we can't change locked profiles if person.locked and person.user_id != request.user.id: raise PermissionDenied('Access denied to locked profile') x, x_valid = intTryParse(request.data.get("x")) y, y_valid = intTryParse(request.data.get("y")) w, w_valid = intTryParse(request.data.get("w")) h, h_valid = intTryParse(request.data.get("h")) r, r_valid = intTryParse(request.data.get("r")) if not (x_valid and y_valid and w_valid and h_valid and r_valid): raise ParseError('Invalid crop parameters') try: uploaded = request.FILES['picture'] if uploaded.size > MAX_FILE_SIZE: raise ValueError('File is too big') filename = create_hash(person.name) +'.jpg' photo_file = ''.join([settings.MEDIA_ROOT, 'profile_photos/', filename]) #Write the file to the destination destination = open(photo_file, 'wb+') for chunk in uploaded.chunks(): destination.write(chunk) destination.close() person.set_profile_image_crop_rotate_resize(photo_file, x, y, w, h, r) person.save() except Exception as e: raise ParseError(str(e)) create_message('profile_photo_process', person.id) serializer = PersonListSerializer(person) return Response(serializer.data)
def create(self, request): ''' Image upload ''' queryset = Gallery.objects.filter(family_id=request.user.family_id) gallery_id, gallery_id_valid = intTryParse( request.data.get("gallery_id")) if not gallery_id_valid: raise ParseError('Invalid gallery_id') # Check gallery is part of family gallery = get_object_or_404(queryset, pk=gallery_id) try: uploaded = request.FILES['picture'] name, ext = os.path.splitext(uploaded.name) if uploaded.size > MAX_FILE_SIZE: raise ParseError('File too big') filename = create_hash(uploaded.name) + '.jpg' image = Image(gallery_id=gallery.id, family_id=gallery.family_id, title=name, uploaded_by=request.user) path = upload_to(image, filename) #Write the file to the destination destination = open(os.path.join(settings.MEDIA_ROOT, path), 'wb+') for chunk in uploaded.chunks(): destination.write(chunk) destination.close() image.original_image = path PIL.Image.open( os.path.join(settings.MEDIA_ROOT, str(image.original_image))).verify() image.save() image.upload_files_to_s3() image.delete_local_image_files() create_message('image_face_detect', image.id) serializer = ImageSerializer(image) return Response(serializer.data) except Exception as e: if image: image.delete_local_image_files() image.delete() raise ParseError(str(e))
def create(self, request): ''' Creates a tag ''' person_id, person_id_valid = intTryParse(request.data.get("person_id")) if not person_id_valid: return HttpResponse(status=400, content="Invalid person_id") person_queryset = Person.objects.filter( family_id=self.request.user.family_id) person = get_object_or_404(person_queryset, pk=person_id) image_id, image_id_valid = intTryParse(request.data.get("image_id")) if not image_id_valid: return HttpResponse(status=400, content="Invalid image_id") image_queryset = Image.objects.filter( family_id=self.request.user.family_id) image = get_object_or_404(image_queryset, pk=image_id) x1, x1_valid = floatTryParse(request.data.get("x1")) x2, x2_valid = floatTryParse(request.data.get("x2")) y1, y1_valid = floatTryParse(request.data.get("y1")) y2, y2_valid = floatTryParse(request.data.get("y2")) if not (x1_valid and x2_valid and y1_valid and y2_valid): return HttpResponse(status=400, content="Invalid x1, x2, y1, or y2") tag = Tag.objects.create(image_id=image.id, x1=x1, y1=y1, x2=x2, y2=y2, person_id=person.id) create_message('resize_tag', tag.id) # Send notification email tag.send_tag_notification_email() serializer = TagSerializer(tag, many=False) return Response(serializer.data)
def create(self, request): ''' Handles when a sign up is submitted ''' #Check ip has not been locked if not AxesProxyHandler.is_allowed(request): raise Http404 name = request.data.get("name") email = request.data.get("email") gender = request.data.get("gender") language = request.data.get("language") ip_address = request.META.get('HTTP_X_REAL_IP') or request.META.get( 'REMOTE_ADDR') if not (name and email and gender and language): raise ParseError('Invalid name, email, gender, language') if gender not in (MALE, FEMALE, OTHER, NON_BINARY, PREFER_NOT_TO_SAY): raise ParseError('Invalid gender') name = name.strip() email = email.strip().lower() # assign non required stuff birth_year, birth_year_valid = intTryParse( request.data.get("birth_year")) if not birth_year_valid: birth_year = 0 address = request.data.get("address") if not address: address = "" try: validate_email(email) except ValidationError: # return invalid email template raise ParseError('Invalid Email') #Check email is not being used if is_email_in_use(email): raise ParseError('Email in Use') new_signup = SignUp.objects.create(name=name, email_address=email, gender=gender, language=language, address=address, birth_year=birth_year, ip_address=ip_address) serializer = SignUpSerializer(new_signup) return Response(serializer.data)
def partial_update(self, request, pk=None): ''' Editing image ''' queryset = Image.objects.filter(family_id=request.user.family_id) image = get_object_or_404(queryset, pk=pk) title = self.request.data.get('title') if not title or len(title.strip()) == 0: raise ParseError('Invalid title') anticlockwise_angle, rotation_valid = intTryParse( request.data.get("anticlockwise_angle")) if rotation_valid and anticlockwise_angle != 0: image.rotate(anticlockwise_angle) # Rotate any image tags for tag in list(Tag.objects.filter(image_id=image.id)): tag.rotate(anticlockwise_angle) tag.save() description = self.request.data.get('description') if not title or len(title.strip()) == 0: description = "" image.title = title image.description = description latitude, latitude_valid = floatTryParse(request.data.get("latitude")) longitude, longitude_valid = floatTryParse( request.data.get("longitude")) if latitude_valid and longitude_valid and latitude != 0 and longitude != 0: image.latitude = latitude image.longitude = longitude image.save() # if we have rotated image, run facial recognition again if rotation_valid and anticlockwise_angle != 0: create_message('image_face_detect', image.id) serializer = ImageSerializer(image) return Response(serializer.data)
def partial_update(self, request, pk=None): ''' Converts suggested tag into a tag ''' queryset = SuggestedTag.objects.filter( image__family_id=self.request.user.family_id) suggested_tag = get_object_or_404(queryset, pk=pk) person_id, person_id_valid = intTryParse(request.data.get("person_id")) if not person_id_valid: return HttpResponse(status=400, content="Invalid person_id") new_tag = suggested_tag.convertToTag(person_id) # Send notification email new_tag.send_tag_notification_email() create_message('tag_converted_process', new_tag.id) serializer = TagSerializer(new_tag) return Response(serializer.data)
def sign_up_post(request): ''' Handles when a sign up is submitted ''' name = request.POST.get("name").strip() email = request.POST.get("email").strip().lower() gender = request.POST.get("gender") language = request.POST.get("language") # assign non required stuff birth_year, birth_year_valid = intTryParse(request.POST.get("birth_year")) if not birth_year_valid: birth_year = 0 address = request.POST.get("address") if not address: address = "" try: validate_email(email) except ValidationError: # return invalid email template return render(request, 'sign_up/invalid_email.html') #Check email is not being used if is_email_in_use(email): return render(request, 'sign_up/email_in_use.html') new_signup = SignUp.objects.create(name=name, email_address=email, gender=gender, language=language, address=address, birth_year=birth_year) translation.activate(language) return render(request, 'sign_up/check_email.html', { 'new_signup': new_signup, })
def partial_update(self, request, pk=None): ''' Updates title, description and thumbnail of gallery record ''' queryset = Gallery.objects.filter(family_id=request.user.family_id) gallery = get_object_or_404(queryset, pk=pk) title = request.data.get('title') if title: gallery.title = title description = request.data.get('description') if description: gallery.description = description thumbnail_id = request.data.get("thumbnail_id", None) if thumbnail_id is not None: thumbnail_id_number, thumbnail_id_number_valid = intTryParse( thumbnail_id) if thumbnail_id_number_valid: thumbnail_queryset = Image.objects.filter( family_id=request.user.family_id) image = get_object_or_404(thumbnail_queryset, pk=thumbnail_id) gallery.thumbnail = image.thumbnail gallery.thumbnail_height = image.thumbnail_height gallery.thumbnail_width = image.thumbnail_width else: # Remove thumbnail gallery.thumbnail = '' gallery.thumbnail_height = 0 gallery.thumbnail_width = 0 gallery.save() serializer = GallerySerializer(gallery) return Response(serializer.data)
def add_relation_post(request, person_id=0, person=None): ''' Receives post information for a new relation ''' relation_type = int(request.POST.get("relation_type")) if relation_type not in (PARTNERED, RAISED, RAISED_BY): raise Http404 #If person does not exist, create a new person existing_person = int(request.POST.get("existing_person")) if not existing_person: new_name = request.POST.get("name").strip() if len(new_name) == 0: raise Http404 language = request.POST.get("language") #http://stackoverflow.com/a/2917399/1245362 if language not in [x[0] for x in settings.LANGUAGES]: raise Http404 gender = request.POST.get("gender") if gender not in (MALE, FEMALE, OTHER): raise Http404 birth_year, birth_year_valid = intTryParse( request.POST.get("birth_year")) if not birth_year_valid: birth_year = 0 new_person = Person(name=new_name, gender=gender, language=language, family_id=person.family_id, birth_year=birth_year) address = request.POST.get("address") if address: new_person.address = address if relation_type == PARTNERED: new_person.hierarchy_score = person.hierarchy_score elif relation_type == RAISED: new_person.hierarchy_score = person.hierarchy_score + 1 elif relation_type == RAISED_BY: new_person.hierarchy_score = person.hierarchy_score - 1 new_person.save() relation_id = new_person.id else: #Existing person relation_id = int(request.POST.get("relation_id")) new_relation = Relation(from_person_id=person.id, to_person_id=relation_id, relation_type=relation_type) new_relation.save() reevaluate_hierarchy_scores_of_orphans(request.user.family_id) return HttpResponseRedirect('/tree/{0}/'.format(person_id))
def create(self, request): ''' Creates a new person record and links it to another person needs from_person_id, relation_type, name, gender, birth_year and address ''' queryset = Person.objects.filter(family_id=request.user.family_id) from_person_id, from_person_id_valid = intTryParse( request.data.get("from_person_id")) if not from_person_id_valid: return HttpResponse(status=400, content="Invalid from_person_id") from_person = get_object_or_404(queryset, pk=from_person_id) relation_type, relation_type_valid = intTryParse( request.data.get("relation_type")) if not relation_type_valid or relation_type not in (PARTNERED, RAISED, RAISED_BY): return HttpResponse(status=400, content="Invalid relation_type") name = request.data.get("name") if not name or len(name.strip()) == 0: return HttpResponse(status=400, content="Invalid name") gender = request.data.get("gender") if gender not in (MALE, FEMALE, OTHER, NON_BINARY, PREFER_NOT_TO_SAY): return HttpResponse(status=400, content="Invalid gender") birth_year, birth_year_valid = intTryParse( request.POST.get("birth_year")) if not birth_year_valid: birth_year = 0 with reversion.create_revision(): new_person = Person(name=name.strip(), gender=gender, family_id=from_person.family_id, birth_year=birth_year) address = request.data.get("address") if address: new_person.address = address # Hierarchy scores will eventually be deprecated if relation_type == PARTNERED: new_person.hierarchy_score = from_person.hierarchy_score elif relation_type == RAISED: new_person.hierarchy_score = from_person.hierarchy_score + 1 elif relation_type == RAISED_BY: new_person.hierarchy_score = from_person.hierarchy_score - 1 new_person.save() # Store some meta-information. reversion.set_user(request.user) reversion.set_comment('Create ' + request.META.get('HTTP_X_REAL_IP')) relation = create_relation(request.user, from_person, new_person, relation_type) relation_serializer = RelationSerializer(relation) person_serializer = PersonSerializer(new_person) return Response({ 'person': person_serializer.data, 'relation': relation_serializer.data })