def create_file(self, request, validated_data): try: # Extract our upload file data content = validated_data.get('upload_content') filename = validated_data.get('upload_filename') if settings.DEBUG: filename = "QA_"+filename # NOTE: Attach `QA_` prefix if server running in QA mode. content_file = get_content_file_from_base64_string(content, filename) # REACT-DJANGO UPLOAD | STEP 3 OF 4: Convert to `ContentFile` type. # Create our file. private_file = PrivateFileUpload.objects.create( is_archived = False, user = request.user, data_file = content_file, # REACT-DJANGO UPLOAD | STEP 4 OF 4: When you attack a `ContentImage`, Django handles all file uploading. created_by = request.user, created_from = request.client_ip, created_from_is_public = request.client_ip_is_routable, last_modified_by = request.user, last_modified_from = request.client_ip, last_modified_from_is_public = request.client_ip_is_routable, ) logger.info("Private image was been created.") return private_file except Exception as e: print(e) private_file = None return None
def create(self, validated_data): """ Override the `create` function to add extra functinality. """ request = self.context.get("request") type_of = self.context.get("type_of") category = validated_data.get('category') description = validated_data.get('description') who_news_for = validated_data.get('who_news_for') location = validated_data.get('location') external_url = validated_data.get('external_url') photos = validated_data.get('photos', []) item_type = ItemType.objects.filter(slug=category).first() item = Item.objects.create( type_of=item_type, description=description, who_news_for=who_news_for, location=location, external_url=external_url, created_by=request.user, created_from=request.client_ip, created_from_is_public=request.client_ip_is_routable, last_modified_by=request.user, last_modified_from=request.client_ip, last_modified_from_is_public=request.client_ip_is_routable, ) # Proccess the uploaded photos which are encoded in `base64` format. # The following code will convert the `base64` string into a Python # binary data and save it in our database. if photos != None and photos != "" and len(photos) > 0: for photo in photos: data = photo['data'] filename = photo['file_name'] if settings.DEBUG: filename = "QA_" + filename # NOTE: Attach `QA_` prefix if server running in QA mode. content_file = get_content_file_from_base64_string( data, filename) private_image = PrivateImageUpload.objects.create( item=item, user=request.user, image_file=content_file, created_by=request.user, created_from=request.client_ip, created_from_is_public=request.client_ip_is_routable, last_modified_by=request.user, last_modified_from=request.client_ip, last_modified_from_is_public=request.client_ip_is_routable, ) # raise serializers.ValidationError({ # Uncomment when not using this code but do not delete! # "error": "Terminating for debugging purposes only." # }) return item
def create(self, validated_data): """ Override the `create` function to add extra functinality. """ # Extract the data we are processing. slug = validated_data.get('member') member = Member.objects.select_for_update().get(user__slug=slug) request = self.context.get('request') # Extract our upload file data content = validated_data.get('upload_content') filename = validated_data.get('upload_filename') if settings.DEBUG: filename = "QA_" + filename # NOTE: Attach `QA_` prefix if server running in QA mode. content_file = get_content_file_from_base64_string( content, filename ) # REACT-DJANGO UPLOAD | STEP 3 OF 4: Convert to `ContentFile` type. # Create our private image upload if it was not done previously, # else we update the member's avatar. if member.avatar_image == None or member.avatar_image is None: member.avatar_image = PrivateImageUpload.objects.create( image_file= content_file, # REACT-DJANGO UPLOAD | STEP 4 OF 4: When you attack a `ContentFile`, Django handles all file uploading. user=member.user, created_by=request.user, created_from=request.client_ip, created_from_is_public=request.client_ip_is_routable, last_modified_by=request.user, last_modified_from=request.client_ip, last_modified_from_is_public=request.client_ip_is_routable, ) member.last_modified_by = request.user member.last_modified_from = request.client_ip member.last_modified_from_is_public = request.client_ip_is_routable member.save() print( "MemberAvatarCreateOrUpdateOperationSerializer --> create() --> CREATED IMAGE" ) else: member.avatar_image.image_file = content_file member.avatar_image.last_modified_by = request.user member.avatar_image.last_modified_from = request.client_ip member.avatar_image.last_modified_from_is_public = request.client_ip_is_routable member.avatar_image.save() member.last_modified_by = request.user member.last_modified_from = request.client_ip member.last_modified_from_is_public = request.client_ip_is_routable member.save() print( "MemberAvatarCreateOrUpdateOperationSerializer --> create() --> UPDATED IMAGE" ) # raise serializers.ValidationError({ # Uncomment when not using this code but do not delete! # "error": "Terminating for debugging purposes only." # }) return validated_data
def update(self, instance, validated_data): request = self.context.get("request") instance.type_of = validated_data.get('type_of') instance.description = validated_data.get('description') instance.name = validated_data.get('name') instance.counselor_name = validated_data.get('counselor_name', None) instance.counselor_email = validated_data.get('counselor_email', None) instance.counselor_phone = validated_data.get('counselor_phone', None) instance.website_url = validated_data.get('website_url', None) instance.facebook_url = validated_data.get('facebook_url', None) instance.created_by = request.user instance.created_from = request.client_ip instance.created_from_is_public = request.client_ip_is_routable instance.last_modified_by = request.user instance.last_modified_from = request.client_ip instance.last_modified_from_is_public = request.client_ip_is_routable instance.save() logger.info("New district was been updated.") try: # Extract our upload file data content = validated_data.get('upload_content', None) filename = validated_data.get('upload_filename', None) if content and filename: if settings.DEBUG: filename = "QA_" + filename # NOTE: Attach `QA_` prefix if server running in QA mode. content_file = get_content_file_from_base64_string( content, filename ) # REACT-DJANGO UPLOAD | STEP 3 OF 4: Convert to `ContentFile` type. # Create our file. private_file = PrivateImageUpload.objects.create( is_archived=False, user=request.user, image_file= content_file, # REACT-DJANGO UPLOAD | STEP 4 OF 4: When you attack a `ContentImage`, Django handles all file uploading. created_by=request.user, created_from=request.client_ip, created_from_is_public=request.client_ip_is_routable, last_modified_by=request.user, last_modified_from=request.client_ip, last_modified_from_is_public=request.client_ip_is_routable, ) logger.info("Private file was been created.") instance.logo_image = private_file instance.save() except Exception as e: print(e) private_file = None # raise serializers.ValidationError({ # Uncomment when not using this code but do not delete! # "error": "Terminating for debugging purposes only." # }) return instance
def create(self, validated_data): """ Override the `create` function to add extra functinality. """ # Extract the associate we are processing. associate = validated_data.get('associate') # Extract our upload file data content = validated_data.get('upload_content') filename = validated_data.get('upload_filename') if settings.DEBUG: filename = "QA_" + filename # NOTE: Attach `QA_` prefix if server running in QA mode. content_file = get_content_file_from_base64_string( content, filename ) # REACT-DJANGO UPLOAD | STEP 3 OF 4: Convert to `ContentFile` type. # Create our private image upload if it was not done previously, # else we update the associate's avatar. if associate.avatar_image == None or associate.avatar_image is None: associate.avatar_image = PrivateImageUpload.objects.create( image_file= content_file, # REACT-DJANGO UPLOAD | STEP 4 OF 4: When you attack a `ContentFile`, Django handles all file uploading. created_by=self.context['created_by'], created_from=self.context['created_from'], created_from_is_public=self.context['created_from_is_public'], last_modified_by=self.context['created_by'], last_modified_from=self.context['created_from'], last_modified_from_is_public=self. context['created_from_is_public'], ) associate.last_modified_by = self.context['created_by'] associate.last_modified_from = self.context['created_from'] associate.last_modified_from_is_public = self.context[ 'created_from_is_public'] associate.save() else: associate.avatar_image.image_file = content_file associate.avatar_image.last_modified_by = self.context[ 'created_by'] associate.avatar_image.last_modified_from = self.context[ 'created_from'] associate.avatar_image.last_modified_from_is_public = self.context[ 'created_from_is_public'] associate.avatar_image.save() associate.last_modified_by = self.context['created_by'] associate.last_modified_from = self.context['created_from'] associate.last_modified_from_is_public = self.context[ 'created_from_is_public'] associate.save() return validated_data
def create(self, validated_data): """ Override the `create` function to add extra functinality. """ customer = validated_data.get('customer') # Extract our upload file data content = validated_data.get('upload_content') filename = validated_data.get('upload_filename') if settings.DEBUG: filename = "QA_" + filename # NOTE: Attach `QA_` prefix if server running in QA mode. content_file = get_content_file_from_base64_string( content, filename ) # REACT-DJANGO UPLOAD | STEP 3 OF 4: Convert to `ContentFile` type. #--------------------------- # Create our file. #--------------------------- private_file = PrivateFileUpload.objects.create( title=validated_data.get('title'), description=validated_data.get('description'), is_archived=validated_data.get('is_archived'), customer=customer, data_file= content_file, # REACT-DJANGO UPLOAD | STEP 4 OF 4: When you attack a `ContentFile`, Django handles all file uploading. created_by=self.context['created_by'], created_from=self.context['created_from'], created_from_is_public=self.context['created_from_is_public'], last_modified_by=self.context['created_by'], last_modified_from=self.context['created_from'], last_modified_from_is_public=self. context['created_from_is_public'], ) tags = validated_data.get('tags', None) if tags is not None: if len(tags) > 0: private_file.tags.set(tags) # For debugging purposes only. print("Created private file #", private_file) #--------------------------- # Attach our comment. #--------------------------- text = _( "A file named \"%(filename)s\" has been uploaded to this customer's record by %(name)s." ) % { 'filename': str(filename), 'name': str(self.context['created_by']), } comment = Comment.objects.create( created_by=self.context['created_by'], last_modified_by=self.context['created_by'], text=text, created_from=self.context['created_from'], created_from_is_public=self.context['created_from_is_public']) CustomerComment.objects.create( about=customer, comment=comment, ) print("Created comment #", comment) # Return our validated data. return private_file
def create(self, validated_data): """ Override the `create` function to add extra functinality. """ slug = validated_data.get('user') user = SharedUser.objects.get(slug=slug) request = self.context.get("request") #----------------------------- # Create our file. #----------------------------- # Extract our upload file data content = validated_data.get('upload_content') filename = validated_data.get('upload_filename') if settings.DEBUG: filename = "QA_" + filename # NOTE: Attach `QA_` prefix if server running in QA mode. content_file = get_content_file_from_base64_string( content, filename ) # REACT-DJANGO UPLOAD | STEP 3 OF 4: Convert to `ContentImage` type. # Create our file. private_file = PrivateImageUpload.objects.create( title=validated_data.get('title'), description=validated_data.get('description'), is_archived=validated_data.get('is_archived'), user=user, data_file= content_file, # REACT-DJANGO UPLOAD | STEP 4 OF 4: When you attack a `ContentImage`, Django handles all file uploading. created_by=request.user, created_from=request.client_ip, created_from_is_public=request.client_ip_is_routable, last_modified_by=request.user, last_modified_from=request.client_ip, last_modified_from_is_public=request.client_ip_is_routable, ) tags = validated_data.get('tags', None) if tags is not None: if len(tags) > 0: private_file.tags.set(tags) # For debugging purposes only. print("Created private file #", private_file) #----------------------------- # Create our `Comment` object. #----------------------------- text = _( "A file named \"%(filename)s\" has been uploaded to this member's record by %(name)s." ) % { 'filename': str(filename), 'name': str(request.user), } comment = Comment.objects.create( created_by=request.user, created_from=request.client_ip, created_from_is_public=request.client_ip_is_routable, last_modified_by=request.user, last_modified_from=request.client_ip, last_modified_from_is_public=request.client_ip_is_routable, text=text) if user.is_member: MemberComment.objects.create( member=user.member, comment=comment, ) print("Created comment for member") elif user.is_area_coordinator: AreaCoordinatorComment.objects.create( area_coordinator=user.area_coordinator, comment=comment, ) print("Created comment for area coordinator") else: raise serializers.ValidationError({ "error": "Programmer did not write the code for this yet.", }) # raise serializers.ValidationError({ # Uncomment when not using this code but do not delete! # "error": "Terminating for debugging purposes only." # }) # Return our validated data. return private_file
def update(self, instance, validated_data): """ Override the `create` function to add extra functinality. """ request = self.context.get("request") title = validated_data.get("title") description = validated_data.get('description') event_logo_image = validated_data.get('event_logo_image') external_url = validated_data.get('external_url') shown_to_whom = validated_data.get('shown_to_whom') can_be_posted_on_social_media = validated_data.get( 'can_be_posted_on_social_media') # DEVELOPERS NOTE: # (1) The following code will either update the `event_logo_image` or # create a new image. # (2) Check to see if a previous image was uploaded and if so then # we need to delete it. event_logo_image_slug = event_logo_image.get("slug") if event_logo_image_slug: instance.event_logo_image__slug = event_logo_image_slug else: # print(event_logo_image) # For debugging purposes only. data = event_logo_image.get('data', None) filename = event_logo_image.get('file_name', None) if settings.DEBUG: filename = "QA_" + filename # NOTE: Attach `QA_` prefix if server running in QA mode. content_file = get_content_file_from_base64_string(data, filename) if instance.event_logo_image: instance.event_logo_image.delete() instance.event_logo_image = None private_image = PrivateImageUpload.objects.create( item=instance, user=request.user, image_file=content_file, created_by=request.user, created_from=request.client_ip, created_from_is_public=request.client_ip_is_routable, last_modified_by=request.user, last_modified_from=request.client_ip, last_modified_from_is_public=request.client_ip_is_routable, ) instance.event_logo_image = private_image # print("Created - event_logo_image") # Save it. instance.title = title instance.description = description instance.external_url = external_url instance.shown_to_whom = shown_to_whom instance.can_be_posted_on_social_media = can_be_posted_on_social_media instance.last_modified_by = request.user instance.last_modified_from = request.client_ip instance.last_modified_from_is_public = request.client_ip_is_routable instance.save() # raise serializers.ValidationError({ # Uncomment when not using this code but do not delete! # "error": "Terminating for debugging purposes only." # }) return instance
def create(self, validated_data): """ Override the `create` function to add extra functinality. """ request = self.context.get("request") type_of = validated_data.get('type_of') description = validated_data.get('description') name = validated_data.get('name') counselor_name = validated_data.get('counselor_name') counselor_email = validated_data.get('counselor_email') counselor_phone = validated_data.get('counselor_phone') website_url = validated_data.get('website_url') try: # Extract our upload file data content = validated_data.get('upload_content') filename = validated_data.get('upload_filename') if settings.DEBUG: filename = "QA_"+filename # NOTE: Attach `QA_` prefix if server running in QA mode. content_file = get_content_file_from_base64_string(content, filename) # REACT-DJANGO UPLOAD | STEP 3 OF 4: Convert to `ContentFile` type. # Create our file. private_file = PrivateImageUpload.objects.create( is_archived = False, user = request.user, image_file = content_file, # REACT-DJANGO UPLOAD | STEP 4 OF 4: When you attack a `ContentImage`, Django handles all file uploading. created_by = request.user, created_from = request.client_ip, created_from_is_public = request.client_ip_is_routable, last_modified_by = request.user, last_modified_from = request.client_ip, last_modified_from_is_public = request.client_ip_is_routable, ) logger.info("Private file was been created.") except Exception as e: print(e) private_file = None # Create the district. district = District.objects.create( type_of=type_of, description=description, name=name, counselor_name=counselor_name, counselor_email=counselor_email, counselor_phone=counselor_phone, website_url=website_url, logo_image=private_file, created_by = request.user, created_from = request.client_ip, created_from_is_public = request.client_ip_is_routable, last_modified_by = request.user, last_modified_from = request.client_ip, last_modified_from_is_public = request.client_ip_is_routable, ) logger.info("New district was been created.") # print(private_file) # print("\n") # raise serializers.ValidationError({ # Uncomment when not using this code but do not delete! # "error": "Terminating for debugging purposes only." # }) return district
def create(self, validated_data): """ Override the `create` function to add extra functinality. """ request = self.context.get("request") type_of = self.context.get("type_of") category = validated_data.get('category') is_all_day_event = validated_data.get('is_all_day_event') start_date_time = validated_data.get('start_date_time') finish_date_time = validated_data.get('finish_date_time') title = validated_data.get('title') description = validated_data.get('description') external_url = validated_data.get('external_url') shown_to_whom = validated_data.get('shown_to_whom') can_be_posted_on_social_media = validated_data.get( 'can_be_posted_on_social_media') event_logo_image = validated_data.get('event_logo_image', None) photos = validated_data.get('photos', []) item_type = ItemType.objects.filter(slug=category).first() item = Item.objects.create( type_of=item_type, start_at=start_date_time, is_all_day_event=is_all_day_event, finish_at=finish_date_time, title=title, description=description, external_url=external_url, shown_to_whom=shown_to_whom, can_be_posted_on_social_media=can_be_posted_on_social_media, created_by=request.user, created_from=request.client_ip, created_from_is_public=request.client_ip_is_routable, last_modified_by=request.user, last_modified_from=request.client_ip, last_modified_from_is_public=request.client_ip_is_routable, ) # Proccess the uploaded photos which are encoded in `base64` format. # The following code will convert the `base64` string into a Python # binary data and save it in our database. if photos != None and photos != "" and len(photos) > 0: for photo in photos: data = photo['data'] filename = photo['file_name'] if settings.DEBUG: filename = "QA_" + filename # NOTE: Attach `QA_` prefix if server running in QA mode. content_file = get_content_file_from_base64_string( data, filename) private_image = PrivateImageUpload.objects.create( item=item, user=request.user, image_file=content_file, created_by=request.user, created_from=request.client_ip, created_from_is_public=request.client_ip_is_routable, last_modified_by=request.user, last_modified_from=request.client_ip, last_modified_from_is_public=request.client_ip_is_routable, ) if event_logo_image: # print(event_logo_image) # For debugging purposes only. data = event_logo_image.get('data', None) filename = event_logo_image.get('file_name', None) if settings.DEBUG: filename = "QA_" + filename # NOTE: Attach `QA_` prefix if server running in QA mode. content_file = get_content_file_from_base64_string(data, filename) private_image = PrivateImageUpload.objects.create( item=item, user=request.user, image_file=content_file, created_by=request.user, created_from=request.client_ip, created_from_is_public=request.client_ip_is_routable, last_modified_by=request.user, last_modified_from=request.client_ip, last_modified_from_is_public=request.client_ip_is_routable, ) item.event_logo_image = private_image item.save() # print("Created - event_logo_image") # raise serializers.ValidationError({ # Uncomment when not using this code but do not delete! # "error": "Terminating for debugging purposes only." # }) return item