예제 #1
0
    def post(self, request):
        try:
            from_user = request.user
            for_user = User.objects.get(pk=request.data["for_user"])
            request_type = request.data["request_type"]
            field_type = request.data["field_type"]

            print for_user.id
            print request.user.id

            is_shown = Contact.create_add_request(from_user=request.user, for_user=for_user)
            print is_shown
            profile_request = ProfileRequest.objects.filter(for_user_id=for_user.id, from_user=from_user,
                                                            request_type=request_type)

            if profile_request:
                profile_request = profile_request[0]
                if profile_request.status == "declined":
                    profile_request.status = "new"
                    profile_request.save()
                    profile_request.notify(is_shown)
                else:
                    return JSONResponse(REQUEST_ALREADY_EXISTS, status=200)
            else:
                profile_request = ProfileRequest(for_user_id=for_user.id, from_user=from_user,
                                                 request_type=request_type, field_type=field_type)
                profile_request.save()
                profile_request.notify(is_shown)

                return JSONResponse(PROFILE_REQUEST_CREATED, status=200)

        except Exception as e:
            beam(e, request)
            return JSONResponse(ERROR_MESSAGE, status=200)
예제 #2
0
    def post(self, request):

        try:
            number = request.data['number']
            otp = request.data['otp']

            if OTPToken.objects.filter(updated_on__gte=(timezone.now() - datetime.timedelta(hours=2)), number=number,
                                       otp=otp).exists():
                if User.objects.filter(mobile_number=number).exists():
                    user = User.objects.filter(mobile_number=number)[0]
                    token = user.get_access_token(request.META)
                    result = {
                            "is_new_user": False,
                            "success":     True,
                            "auth_token":  token.access_token
                            }
                else:
                    result = {
                            "is_new_user": True,
                            "success": True,
                            "auth_token": None
                             }
            else:
                result = {
                        "is_new_user": False,
                        "success": False,
                        "auth_token": None
                        }

            return JSONResponse(result, status=200)
        except Exception as e:
            beam(e, request)
            return JSONResponse(ERROR_MESSAGE, status=200)
예제 #3
0
    def post(self, request):

        try:
            introduced_user = User.objects.get(pk=request.data["introduced_user"])
            message = request.data["message"]
            introduced_to_users = [int(user_id) for user_id in request.data["introduced_to_users"].split(",")]
            print introduced_to_users
            for user in introduced_to_users:
                #Check if user being introduced to already has this person as a contag
                user = User.objects.get(pk=user)
                if not Contact.objects.filter(user=user, contact_contag_user_id=introduced_user):

                    requested_to = Contact.objects.create(user=user, contact_name=introduced_user.name,
                                           contact_number=introduced_user.mobile_number,is_local_profile_updated=False)
                    requested_to.create_feed(request.user, message)

                if not Contact.objects.filter(user=introduced_user, contact_contag_user_id=user):
                    #Add to requesters contact book
                    requested_by = Contact.objects.create(user=introduced_user, contact_name=user.name,
                                           contact_number=user.mobile_number, is_local_profile_updated=False)
                    requested_by.create_feed(request.user, message)

            return JSONResponse(SUCCESS_MESSAGE, status=200)
        except Exception as e:
            beam(e, request)
            return JSONResponse(ERROR_MESSAGE, status=200)
예제 #4
0
    def delete(self, request):

        try:
            print request.query_params["notification_id"]
            Notification.objects.filter(pk=request.query_params["notification_id"]).update(is_shown=False)
            return JSONResponse([], status=200)
        except Exception as e:
            beam(e, request)
            return JSONResponse(ERROR_MESSAGE, status=200)
예제 #5
0
 def delete(self, request):
     try:
         id = request.query_params["id"]
         SocialProfile.objects.filter(user=request.user, social_platform_id=id).delete()
         return JSONResponse(SUCCESS_MESSAGE, status=200)
     except Exception as e:
         print traceback.format_exc(e)
         beam(e, request)
         return JSONResponse(ERROR_MESSAGE, status=400)
예제 #6
0
    def get(self, request):
        try:
            platforms = SocialPlatform.objects.all()
            platforms = SocialPlatformSerializer(instance=platforms, many=True).data

            return JSONResponse(platforms, status=200)
        except Exception as e:
            beam(e, request)
            return JSONResponse(ERROR_MESSAGE, status=200)
예제 #7
0
 def post(self, request):
     try:
         number = request.data['number']
         otp = OTPToken.create(number=number)
         otp.save()
         otp.send_otp()
         return JSONResponse(SUCCESS_MESSAGE, status=200)
     except Exception as e:
         beam(e, request)
         return JSONResponse(ERROR_MESSAGE, status=200)
예제 #8
0
    def delete(self, request):
        try:
            user = request.user

            user.logout(headers=request.META)

            return JSONResponse(SUCCESS_MESSAGE, status=200)
        except Exception as e:
            beam(e, request)
            return JSONResponse(ERROR_MESSAGE, status=200)
예제 #9
0
    def post(self, request):
        try:
            print "Here about to create an add request"
            user_id = request.data["id"]
            user = User.objects.get(pk=user_id)
            Contact.create_add_request(from_user=request.user, for_user = user)

            return JSONResponse([], status=200)
        except Exception as e:
            beam(e, request)
            return JSONResponse(ERROR_MESSAGE, status=400)
예제 #10
0
    def put(self, request):

        try:
            from django.core.files.images import ImageFile
            user = request.user
            image_file = request.FILES["avatar_url"]
            user.avatar_url = image_file
            user.save()
            return JSONResponse({"result": True, "avatar_url": user.avatar_url.url}, status=200)
        except Exception, e:
            beam(e, request)
            return JSONResponse({"result": False, "avatar_url": user.avatar_url.url}, status=400)
예제 #11
0
    def get(self, request):
        try:
            slug = request.query_params['slug']

            interests = Interests.objects.filter(interest__istartswith=slug)

            interests = InterestSerializer(instance=interests, many=True).data

            return JSONResponse(interests, status=200)
        except Exception as e:
            beam(e, request)
            return JSONResponse(ERROR_MESSAGE, status=200)
예제 #12
0
def user_count(request):

    try:
        from api.models import AccessToken
        from datetime import datetime, timedelta
        count = User.objects.count()
        logged_in = AccessToken.objects.filter(active=1).count()
        active_count = User.objects.filter(updated_on__gte = datetime.now() - timedelta(days=28)).count()

        return JSONResponse({"total_user_count": count, "logged_in_count": logged_in, "active_users": active_count}, status=200)
    except Exception as e:
        beam(e, request)
        return JSONResponse(ERROR_MESSAGE, status=400)
예제 #13
0
 def put(self, request):
     try:
         profile = ProfileEditSerializer()
         print "Updating user's profile"
         print request.data['user']
         profile_category = request.data["profile_category"] if "profile_category" in request.data else -1
         profile = profile.update(instance=request.user, validated_data=request.data['user'],
                                  profile_category=profile_category)
         profile = MyProfileSerializer(instance=profile).data
         return JSONResponse(profile, status=200)
     except Exception as e:
         print traceback.format_exc(e)
         beam(e, request)
         return JSONResponse(ERROR_MESSAGE, status=200)
예제 #14
0
    def get(self, request):

        try:
            start_index = request.query_params["start_index"]
            end_index = request.query_params["end_index"]
            print start_index
            print end_index

            notifications = Notification.objects.filter(user=request.user,
                                                        is_shown=True).order_by('-created_on')[start_index:end_index]
            response = NotificationSerializer(instance=notifications, many=True).data
            return JSONResponse(response, status=200)
        except Exception as e:
            beam(e, request)
            return JSONResponse(ERROR_MESSAGE, status=200)
예제 #15
0
    def post(self, request):

        try:
            users = User.objects.filter(contag=request.data['contag_id'])
            # Check if an existing user exists with the given contag id
            if users:
                return JSONResponse({"success": False, "auth_token": None}, status=200)
            else:
                user = User(mobile_number=request.data['number'], contag=request.data['contag_id'])
                user.save()
                token = user.get_access_token(request.META)
                return JSONResponse({"success": True, "auth_token": token.access_token}, status=200)
        except Exception as e:
            print traceback.format_exc(e)
            beam(e, request)
            return JSONResponse(ERROR_MESSAGE, status=200)
예제 #16
0
    def post(self, request):

        try:
            user = request.user

            UserInterest.objects.filter(user=user).delete()

            if request.data["interest_ids"]:
                interest_ids = request.data["interest_ids"].split(",")
                for interest_id in interest_ids :
                    UserInterest.objects.create(user=user, interest_id=interest_id)

            return JSONResponse(SUCCESS_MESSAGE, status=200)
        except Exception as e:
            beam(e, request)
            return JSONResponse(ERROR_MESSAGE, status=200)
예제 #17
0
    def get(self, request):

        try:
            if "id" in request.query_params:
                user = User.objects.get(pk=request.query_params["id"])
                user.set_visible_profile(current_user_id=request.user.id)
                response = ProfileViewSerializer(instance=user, context={"current_user": request.user}).data
            else:
                user = request.user
                response = MyProfileSerializer(instance=user).data

            return JSONResponse(response, status=200)
        except Exception as e:
            print traceback.format_exc(e)
            beam(e, request)
            return JSONResponse(OBJECT_DOES_NOT_EXIST, status=200)
예제 #18
0
    def put(self, request):
        try:
            #Update the status of the profile request and hide the corresponding notification

            r = ProfileRequest.objects.filter(id=request.data["request_id"])[0]
            r.status=request.data["status"]
            r.save()
            Notification.objects.filter(request_id=request.data["request_id"]).update(is_shown=False)

            if r.status == "allow":
                make_notification.profile_request_response(requested_by=r.from_user, requested_from=r.for_user,
                                                           column_name = r.request_type )

            return JSONResponse(SUCCESS_MESSAGE, status=200)
        except Exception as e:
            beam(e, request)
            return JSONResponse(ERROR_MESSAGE, status=200)
예제 #19
0
    def post(self, request):
        try:
            for data in request.data:
                user = request.user
                social_profile = SocialProfile.objects.filter(user=user, social_platform_id=data["social_platform_id"])

                if social_profile.exists():
                    profile = SocialProfileEditSerializer()
                    profile.update(instance=social_profile[0], validated_data=data)
                else:
                    social_profile = SocialProfileEditSerializer(context={'current_user': request.user})
                    social_profile.create(validated_data=data)
            user = request.user
            response = MyProfileSerializer(instance=user).data
            return JSONResponse(response, status=200)
        except Exception as e:
            beam(e, request)
            return JSONResponse(ERROR_MESSAGE, status=200)
예제 #20
0
    def get(self, request):
        try:
            response = []
            if "contag_id" in request.query_params:
                #Look for a contag by contag id
                user = User.objects.filter(contag=request.query_params["contag_id"])
                if user.exists():
                    user= user[0]
                    user.set_visible_profile(current_user_id=request.user.id)
                    contact = Contact(user=request.user, contact_name=user.name, contact_number=user.mobile_number,
                                      is_on_contag=1, contact_contag_user=user)
                    existing_request = Contact.existing_add_request_exists(from_user=request.user, for_user_id=user.id)
                    response = ContactViewSerializer(instance=[contact], many=True, context={'current_user': request.user.id}).data
                    response[0]["has_existing_add_request"] = existing_request
            else:
                if "user_id" in request.query_params:

                    user = User.objects.get(pk=request.query_params["user_id"])
                    contact = Contact.objects.filter(user=request.user, contact_number=user.mobile_number)

                    if contact.exists():
                        contacts = contact[0]
                        contacts.is_on_contag = True
                        contacts.contact_contag_user = user
                        contacts.is_local_profile_updated = True
                        contacts.save()
                    else:
                        contacts = Contact(user=request.user, contact_name=user.name,
                                      contact_number=user.mobile_number,
                                      is_on_contag=1, contact_contag_user=user)

                    contacts = Contact.set_contag_visibility(contacts=[contacts], current_user_id=request.user.id)
                    response = ContactViewSerializer(instance=contacts, many=True, context={'current_user': request.user.id}).data
                else:
                    contacts = Contact.objects.filter(user=request.user)

                    contacts = Contact.set_contag_visibility(contacts, current_user_id=request.user.id)
                    response = ContactViewSerializer(instance=contacts, many=True, context={'current_user': request.user.id}).data

            return JSONResponse(response, status=200)
        except Exception as e:
            print traceback.format_exc(e)
            beam(e, request)
            return JSONResponse(ERROR_MESSAGE, status=200)
예제 #21
0
    def post(self, request):

        try:

            # Create/Update whatever contacts come from the app
            contact_numbers = [contact["contact_number"] for contact in request.data]
            contact_names = [contact["contact_name"] for contact in request.data]

            contacts = Contact.objects.filter(user=request.user)
            synced_contacts = []
            #Update the existing contacts saved in db, this is an update on the name
            #Explicitly this checks if the saved user is on contag and sets the boolean accordingly
            for contact in contacts.iterator():
                if contact.contact_number in contact_numbers:
                    index = contact_numbers.index(contact.contact_number)
                    contact.contact_name = contact_names[index]
                    contact.save()
                    del contact_numbers[index]
                    del contact_names[index]
                synced_contacts.append(contact)

            new_contacts = []
            for new_contact in contact_numbers:
                index = contact_numbers.index(new_contact)
                if not request.user.mobile_number == new_contact:
                    new = Contact.objects.create(user=request.user, contact_number=new_contact,
                                                 contact_name=contact_names[index])
                    new_contacts.append(new)

            synced_contacts.extend(new_contacts)
            synced_contacts = Contact.set_contag_visibility(contacts=synced_contacts, current_user_id=request.user.id)

            response_data = ContactViewSerializer(instance=synced_contacts, many=True, context={'current_user': request.user.id}).data
            print "Just synced contacts"
            if request.user.is_new:
                print "Yes the user is new, going to create feeds and notifications"
                request.user.new_user_update()

            return JSONResponse(response_data, status=200)
        except Exception as e:
            print traceback.format_exc(e)
            beam(e, request)
            return JSONResponse(VALIDATION_ERROR_MESSAGE, status=200)
예제 #22
0
    def get(self, request):

        try:

            start_index = request.query_params["start_index"]
            end_index = request.query_params["end_index"]

            print start_index
            print end_index

            stories = Feed.objects.filter(for_user= request.user, is_shown=True).order_by('-created_on')[start_index:end_index]

            feeds = FeedSerializer(instance=stories, many=True).data

            return JSONResponse(feeds, status=200)
        except Exception as e:
            print traceback.format_exc(e)
            beam(e, request)
            return JSONResponse(ERROR_MESSAGE, status=200)
예제 #23
0
    def put(self, request):
        try:
            print "Responding to an add request"
            notification_id = request.data["notification_id"]
            print notification_id
            notification = Notification.objects.get(pk=notification_id)
            notification.is_shown = False
            notification.save()
            print notification.is_shown
            is_pushed = True

            #Add to requesters contact book - Check if there is already a contact from the requester
            if Contact.objects.filter(user=notification.from_user, contact_number=notification.user.mobile_number).exists():
                requested_by = Contact(user=notification.from_user, contact_name=notification.user.name,
                                       contact_number=notification.user.mobile_number, is_local_profile_updated=False)
                requested_by.save()
                is_pushed = False
                print "Contact created for person who requested"


            #Add to accepters contact book
            requested_to = Contact(user=notification.user, contact_name=notification.from_user.name,
                                   contact_number=notification.from_user.mobile_number,is_local_profile_updated=False)
            requested_to.save()
            requested_to.update_local_contact_book()
            print "Contact created for person who was requested"

            if is_pushed:
                make_notification.contact_request_complete(notification=notification,
                                                           accepted_by=notification.user,
                                                           accepted_for=notification.from_user)

            # Make all profile requests visible
            Notification.objects.filter(user=notification.user, from_user=notification.from_user,
                                        notification_type__in = ["profile_request_add", "profile_request_share"]).update(is_shown=True)

            return JSONResponse([], status=200)
        except Exception as e:
            print traceback.format_exc(e)
            beam(e, request)
            return JSONResponse(ERROR_MESSAGE, status=400)
예제 #24
0
def add_via_nfc(request):

    try:
        from notifier.push_notification import update_profile_notification
        user_id = request.data["id"]
        user = User.objects.get(pk=user_id)

        #create contact for receiver
        Contact.objects.create(contact_name=user.name, contact_number=user.mobile_number, user=request.user, is_on_contag=1,
                               contact_contag_user=user)

        #create contact for sender
        Contact.objects.create(contact_name=request.user.name, contact_number=request.user.mobile_number, user=user, is_on_contag=1,
                               contact_contag_user=request.user)

        #Add contact for sender via silent push notification
        update_profile_notification(for_user=user.id, profile_id=request.user.id)
        return JSONResponse([], status=200)
    except Exception as e:
        beam(e, request)
        return JSONResponse(ERROR_MESSAGE, status=400)
예제 #25
0
def push_promotional(request):

    try:
        from notifier.push_notification import push_promotional
        from api.models import UserInterest

        push_message = request.query_params["push_message"]
        interests = request.query_params["interests"].split(",") if "interests" in request.query_params and len(request.query_params["interests"]) > 0 else None
        upper = request.query_params["pending_count_upper"] if "pending_count_upper" in request.query_params else None
        lower = request.query_params["pending_count_lower"] if "pending_count_lower" in request.query_params else None

        if interests:
            users = UserInterest.objects.filter(interest_id__in = interests).values("user").distinct()
            users = [user["user"] for user in users]
            users = User.objects.filter(id__in = users)
        elif upper and lower:

            query_string = ("Select id, user_id, user_id_count from ( \
                              SELECT id, user_id, count(user_id) as user_id_count FROM \
                              api_notification where is_shown = 1 group by user_id ) \
                              as ab  where user_id_count >= %s and user_id_count <= %s ;" % (lower, upper))


            users = User.objects.raw(query_string)
            # % (lower, upper)
            user_ids = [user.user_id for user in users]

            users = User.objects.filter(id__in=user_ids)

        else:
            users = User.objects.all()[0]

        for user in users:
            push_promotional(push_message=push_message, to_user=user)

        return JSONResponse({"sent_to": len(users)}, status=200)
    except Exception as e:
        beam(e, request)
        return JSONResponse(ERROR_MESSAGE, status=400)
예제 #26
0
    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)