def update(self, instance, validated_data, profile_category): no_notification_columns = ['name', 'gender', 'avatar_url', 'contag'] public_columns = ['name', 'gender', 'avatar_url', 'contag', 'mobile_number'] print type(validated_data) validated_data = json.loads(validated_data) for field in validated_data: column_name = field.keys()[0] if column_name.__eq__('avatar_url'): instance.update_profile_picture_from_url(field[column_name]) else: existing_value = getattr(instance, column_name) #If the new value is blank set it as null new_value = None if not field[column_name] else field[column_name] is_new = False if existing_value else True #if new value is null or same as old value then notification should not be created is_updated = True if (existing_value != new_value) else False setattr(instance, column_name, new_value) is_public = False if column_name not in public_columns: right = ProfileRight.objects.filter(from_user=instance, unit_type=column_name) if not right: right = ProfileRight.objects.create(from_user=instance, unit_type=column_name) else: right = right[0] is_public = right.is_public #Change existing add requests to share requests if not existing_value and new_value: existing_requests = ProfileRequest.objects.filter(for_user=instance.id, request_type=column_name) Notification.objects.filter(user=instance.id, notification_type="profile_request_add", request__in = existing_requests).update(notification_type="profile_request_share") if is_updated and column_name not in no_notification_columns: make_notification.profile_updated(from_user=instance, column_name=column_name, is_public=is_public, is_new=is_new) instance.save() return instance
def update(self, instance, validated_data): instance.platform_id = validated_data.get("platform_id") instance.platform_token = validated_data.get("platform_token", None) instance.platform_secret = validated_data.get("platform_secret", None) instance.platform_permissions = validated_data.get("platform_permissions", None) instance.platform_email = validated_data.get("platform_email", None) instance.platform_username = validated_data.get("platform_username", None) instance.save() make_notification.profile_updated(from_user=instance.user, column_name=instance.social_platform.platform_name, is_new=False) return instance
def create(self, validated_data): user = self.context["current_user"] profile = SocialProfile.objects.create(user=user, **validated_data) platform_name = profile.social_platform.platform_name right = ProfileRight.objects.filter(from_user=user, unit_type=platform_name) existing_requests = ProfileRequest.objects.filter(for_user=user.id, request_type=platform_name) Notification.objects.filter(user=user.id, notification_type="profile_request_add", request__in = existing_requests).update(notification_type="profile_request_share") if not len(right): ProfileRight.objects.create(from_user=user, unit_type=platform_name) make_notification.profile_updated(from_user=user, column_name=platform_name, is_new=True) return profile
def post(self, request): try: print request.data["user_ids"] column_name = request.data["field_name"] print column_name is_public = request.data["is_public"] print is_public custom_share = request.data["user_ids"] is_private = not is_public and not len(custom_share) right = ProfileRight.objects.filter(from_user=request.user, unit_type=column_name) if len(right): right = right[0] right.is_public = is_public right.is_private = is_private right.save() else: right = ProfileRight.objects.create(from_user=request.user, unit_type=column_name, is_private=is_private, is_public=is_public) if is_public: #Create feed only for those who haven't had access to it till now feeds = Feed.objects.filter(from_user=request.user, column_name=column_name, is_shown=1) #Find existing users who have had this feed in the past existing_users = [feed.for_user for feed in feeds] #Find all users for the current user all_users = Contact.objects.filter(user=request.user, is_on_contag=1) #Filter out all users who previously got this feed send_to = [] for contact in all_users: if contact.contact_contag_user not in existing_users: if Contact.objects.filter(user=contact.contact_contag_user, contact_contag_user=request.user).exists(): send_to.append(contact.contact_contag_user) from notifier.make_notification import profile_updated profile_updated(from_user=request.user, for_users=send_to, is_public=False, column_name=column_name) requests = ProfileRequest.objects.filter(for_user=request.user, request_type=column_name) requests.update(status="allow") Notification.objects.filter(request__in = requests).update(is_shown=False) else: existing_shares = ProfileShare.objects.filter(from_user=request.user, unit_type=column_name) if len(custom_share): #print custom_share existing_shares = [users.for_user.id for users in existing_shares] print existing_shares requested_shares = [int(share) for share in custom_share.split(",")] print requested_shares delete_shares = [share for share in existing_shares if share not in requested_shares] print delete_shares ProfileShare.objects.filter(from_user=request.user, unit_type=column_name, for_user_id__in = delete_shares).delete() new_shares = [user_id for user_id in requested_shares if user_id not in existing_shares] print "New Shares: " print new_shares for share in new_shares: print share ProfileShare.objects.create(from_user=request.user, unit_type=column_name, for_user_id=share) make_notification.profile_updated(from_user=request.user, column_name=column_name, is_public=right.is_public, is_new=True, for_users=[User.objects.get(pk=share)]) #In case a request already requests for this profile attribute for the given user from this user #remove it profile_request = ProfileRequest.objects.filter(for_user=request.user, from_user_id=share, request_type=column_name) if profile_request: profile_request[0].status = "allow" profile_request[0].save() Notification.objects.filter(request=profile_request[0]).update(is_shown=False) else: existing_shares.delete() return JSONResponse(SUCCESS_MESSAGE, status=200) except Exception as e: print traceback.format_exc(e) beam(e, request) return JSONResponse(ERROR_MESSAGE, status=20)