Exemplo n.º 1
0
    def post(self, request):
        """
        Updates the details of the currently logged in user
        """
        # Short-Circuit for Authentication Errors caused by invalid Device IDs

        if request.yoo["new_version"]:
            if not request.yoo["user"] and request.yoo["auth"].get("__internal__reject__", False):
                raise exceptions.WebServiceAuthorizationFailed()
            user = request.yoo["user"]
            device = request.yoo["device"]

            try:
                data = json.loads(request.body)
            except:
                data = request.POST
            print data
            form = UserUpdateForm(data)
            if not form.is_valid():
                raise exceptions.WebServiceFormValidationFailed(form)
            email = form.cleaned_data["email"]
            password = form.cleaned_data["password"]
            # Routing Logic
            if email and password:
                self._modify_email(user, email, password)
            elif email:
                self._modify_email(user, email, password=None)
            elif password:
                self._modify_password(user, password)

            existing = dict([[x.name, x] for x in user.tokens.all()])
            for key in ["fbToken", "twitterToken", "contestEmail", "requestedEmail"]:
                if key not in data:
                    continue

                if not form.cleaned_data[key]:
                    if key in existing:
                        existing[key].delete()
                else:
                    if key in existing:
                        existing[key].token = form.cleaned_data[key]
                    else:
                        existing[key] = UserToken(user=user, name=key, token=form.cleaned_data[key])

                    existing[key].save()

            if "pushToken" in form.cleaned_data and form.cleaned_data["pushToken"]:
                request.yoo["device"].device_token = form.cleaned_data["pushToken"]
                request.yoo["device"].save()

            if "referral" in form.cleaned_data and form.cleaned_data["referral"]:
                user.referral = form.cleaned_data["referral"]
                user.save()
            print "the email obtained is", type(email)
            try:
                ClientLoginRecord, created = UserClientLogin.objects.get_or_create(
                    device=request.yoo["device"], defaults={}
                )
            except:
                ClientLoginRecord = UserClientLogin.objects.filter(device=request.yoo["device"])[0]
            try:
                if data["fbToken"] != "":
                    ClientLoginRecord.client_login = data["user_email"]
                    email = data["user_email"]
                elif data["twitterToken"] != "":
                    ClientLoginRecord.client_login = data["user_email"]
                    email = data["user_email"]
                else:
                    ClientLoginRecord.client_login = email
            except:
                try:
                    if data["twitterToken"] != "":
                        ClientLoginRecord.client_login = data["user_email"]
                        email = data["user_email"]
                    else:
                        ClientLoginRecord.client_login = email
                except:
                    ClientLoginRecord.client_login = email
            ClientLoginRecord.email_source = data["email_source"]
            ClientLoginRecord.save()
            if email:
                try:
                    if data["email_source"] == "facebook":
                        coins_record, created = EmailCoins.objects.get_or_create(
                            email=email,
                            email_source=data["email_source"],
                            defaults={"coins": 0.5, "reset_date": datetime.datetime.now()},
                        )
                    else:
                        coins_record, created = EmailCoins.objects.get_or_create(
                            email=email,
                            email_source=data["email_source"],
                            defaults={"coins": 0, "reset_date": datetime.datetime.now()},
                        )
                except:
                    coins_record = EmailCoins.objects.filter(email=email, email_source=data["email_source"])[0]
                try:
                    device_record, created = DeviceCoins.objects.get_or_create(
                        device_id=request.yoo["device"], defaults={"coins": 1, "reset_date": datetime.datetime.now()}
                    )
                except:
                    device_record = DeviceCoins.objects.filter(device_id=request.yoo["device"])[0]
                if device_record.coins != 0:
                    coins_record.coins = coins_record.coins + device_record.coins
                    device_record.coins = 0
                    device_record.save()
                coins_record.save()
            if data["email_source"] == "facebook" or data["email_source"] == "twitter":
                try:
                    login_details, created = DeviceLoginDetails.objects.get_or_create(
                        device=request.yoo["device"], email=data["user_email"], user=request.yoo["user"]
                    )
                except:
                    login_details = DeviceLoginDetails.objects.filter(
                        device=request.yoo["device"], email=data["user_email"], user=request.yoo["user"]
                    )[0]
                print request.yoo["device"]
                print data["user_email"]
                print login_details
            # print reset_coins_record

            if user.password and user.email or user.tokens.filter(name="requestedEmail").exists():
                try:
                    CoinTicketGenericTransaction.objects.get(user=user, type="register")
                except CoinTicketGenericTransaction.DoesNotExist:
                    transaction = CoinTransaction(wallet=user.get_wallet())
                    transaction.save()

                    _coins = CoinTicketGenericTransaction(user=user, type="register", transaction=transaction)
                    _coins.save()

                    transaction.amount = _coins.amount
                    transaction.save()
                    try:
                        device_record = DeviceCoins.objects.get(device_id=request.yoo["device"])
                    except:
                        device_record = DeviceCoins.objects.filter(device_id=request.yoo["device"])[0]
                    device_record.coins = F("coins") + int(_coins.amount)
                    device_record.save()

            return self.get(request)
        else:

            # Short-Circuit for Authentication Errors caused by invalid Device IDs
            if not request.yoo["user"] and request.yoo["auth"].get("__internal__reject__", False):
                raise exceptions.WebServiceAuthorizationFailed()

            user = request.yoo["user"]
            device = request.yoo["device"]

            try:
                data = json.loads(request.body)
            except:
                data = request.POST

            form = UserUpdateForm(data)
            if not form.is_valid():
                raise exceptions.WebServiceFormValidationFailed(form)

            email = form.cleaned_data["email"]
            password = form.cleaned_data["password"]

            # Routing Logic
            if email and password:
                self._modify_email(user, email, password)
            elif email:
                self._modify_email(user, email, password=None)
            elif password:
                self._modify_password(user, password)

            existing = dict([[x.name, x] for x in user.tokens.all()])

            for key in ["fbToken", "twitterToken", "contestEmail", "requestedEmail"]:
                if key not in data:
                    continue

                if not form.cleaned_data[key]:
                    if key in existing:
                        existing[key].delete()
                else:
                    if key in existing:
                        existing[key].token = form.cleaned_data[key]
                    else:
                        existing[key] = UserToken(user=user, name=key, token=form.cleaned_data[key])

                    existing[key].save()

            if "pushToken" in form.cleaned_data and form.cleaned_data["pushToken"]:
                request.yoo["device"].device_token = form.cleaned_data["pushToken"]
                request.yoo["device"].save()

            if "referral" in form.cleaned_data and form.cleaned_data["referral"]:
                user.referral = form.cleaned_data["referral"]
                user.save()

            if user.tokens.filter(name="requestedEmail").exists():
                try:
                    CoinTicketGenericTransaction.objects.get(user=user, type="register")
                except CoinTicketGenericTransaction.DoesNotExist:
                    transaction = CoinTransaction(wallet=user.get_wallet())
                    transaction.save()

                    _coins = CoinTicketGenericTransaction(user=user, type="register", transaction=transaction)
                    _coins.save()

                    transaction.amount = _coins.amount
                    transaction.save()

            return self.get(request)
Exemplo n.º 2
0
    def post(self, request):
        """
        Updates the details of the currently logged in user
        """
        
        if request.yoo["new_version"]:
             if not request.yoo["user"] and request.yoo["auth"].get("__internal__reject__", False):
                raise exceptions.WebServiceAuthorizationFailed()
             user = request.yoo["user"]
             device = request.yoo["device"]
            
             try:
                data = json.loads(request.body)
             except:
                data = request.POST
                
             form = UserUpdateForm(data)
             if not form.is_valid():
                raise exceptions.WebServiceFormValidationFailed(form)

             email = form.cleaned_data["email"]
             password = form.cleaned_data["password"]
                
             # Routing Logic        
             '''if email and password:
                self._modify_email(user, email, password)
             elif email:
                self._modify_email(user, email, password=None)
             elif password:
                self._modify_password(user, password)'''
            
             existing = dict([[x.name, x] for x in user.tokens.all()])
            
             for key in ["fbToken", "twitterToken", "contestEmail", "requestedEmail"]:
                if key not in data:
                    continue
                
                if not form.cleaned_data[key]:
                    if key in existing:
                        existing[key].delete()
                else:
                    if key in existing:
                        existing[key].token = form.cleaned_data[key]
                    else:
                        existing[key] = UserToken(user=user, name=key, token=form.cleaned_data[key])
                
                    existing[key].save()
                    
             if "pushToken" in form.cleaned_data and form.cleaned_data["pushToken"]:
                request.yoo["device"].device_token = form.cleaned_data["pushToken"]
                request.yoo["device"].save()
                
             if "referral" in form.cleaned_data and form.cleaned_data["referral"]:
                user.referral = form.cleaned_data["referral"]
                user.save()

             if email:
                try:
                    coins_record, created = EmailCoins.objects.get_or_create(email=email,defaults={'coins': 0})
                except:
                    coins_record = EmailCoins.objects.filter(email=email)[0]
                coins_record.save()

            
             if user.password and user.email or user.tokens.filter(name="requestedEmail").exists():
                try:
                    CoinTicketGenericTransaction.objects.get(user=user, type="register")
                except CoinTicketGenericTransaction.DoesNotExist:
                    transaction = CoinTransaction(wallet=user.get_wallet())
                    transaction.save()
                    
                    _coins = CoinTicketGenericTransaction(user=user, type="register", transaction=transaction)
                    _coins.save()
                                    
                    transaction.amount = _coins.amount
                    transaction.save()
                try:
                    device_record = DeviceCoins.objects.filter(device_id=request.yoo['device'])[0]
                except:
                    device_record,created = DeviceCoins.objects.get_or_create(device_id=request.yoo['device'])
                device_record.coins = F('coins') + int(_coins.amount)
                device_record.save()

             try:
                ClientLoginRecord, created = UserClientLogin.objects.get_or_create(device=request.yoo['device'], defaults={})
             except:
                ClientLoginRecord  = UserClientLogin.objects.filter(device=request.yoo['device'])[0]
             if data.has_key('fbToken')  or data.has_key('twitterToken'):
                ClientLoginRecord.client_login = data['user_email']
             else:
                ClientLoginRecord.client_login = email
             ClientLoginRecord.save()
             return self.get(request)
        else:
      
         # Short-Circuit for Authentication Errors caused by invalid Device IDs
             if not request.yoo["user"] and request.yoo["auth"].get("__internal__reject__", False):
                raise exceptions.WebServiceAuthorizationFailed()
            
             user = request.yoo["user"]
             device = request.yoo["device"]
            
             try:
                data = json.loads(request.body)
             except:
                data = request.POST
            
             form = UserUpdateForm(data)
             if not form.is_valid():
                raise exceptions.WebServiceFormValidationFailed(form)

             email = form.cleaned_data["email"]
             password = form.cleaned_data["password"]
                    
             # Routing Logic        
             if email and password:
                self._modify_email(user, email, password)
             elif email:
                self._modify_email(user, email, password=None)
             elif password:
                self._modify_password(user, password)
            
             existing = dict([[x.name, x] for x in user.tokens.all()])
            
             for key in ["fbToken", "twitterToken", "contestEmail", "requestedEmail"]:
                if key not in data:
                    continue
                
                if not form.cleaned_data[key]:
                    if key in existing:
                        existing[key].delete()
                else:
                    if key in existing:
                        existing[key].token = form.cleaned_data[key]
                    else:
                        existing[key] = UserToken(user=user, name=key, token=form.cleaned_data[key])
                
                    existing[key].save()
                    
             if "pushToken" in form.cleaned_data and form.cleaned_data["pushToken"]:
                request.yoo["device"].device_token = form.cleaned_data["pushToken"]
                request.yoo["device"].save()
                
             if "referral" in form.cleaned_data and form.cleaned_data["referral"]:
                user.referral = form.cleaned_data["referral"]
                user.save()
            
             if user.tokens.filter(name="requestedEmail").exists():
                try:
                    CoinTicketGenericTransaction.objects.get(user=user, type="register")
                except CoinTicketGenericTransaction.DoesNotExist:
                    transaction = CoinTransaction(wallet=user.get_wallet())
                    transaction.save()
                    
                    _coins = CoinTicketGenericTransaction(user=user, type="register", transaction=transaction)
                    _coins.save()
                                    
                    transaction.amount = _coins.amount
                    transaction.save()
            
             return self.get(request)
        
        def _modify_password(self, user, password):
            user.password = hashers.make_password(password)
            user.save()                
        
        def _modify_email(self, user, email, password):
            # Search Others
            other = YooLottoUser.objects.filter(email=email)
            
            # Exclude current user
            if user:
                other = other.exclude(pk=user.pk)
            
            if not other:
                user.email = email
                
                if password:
                    user.password = hashers.make_password(password)
                
                user.save()
                
                return
            
            other = other[0]
            if not other.password:
                self._merge(user, email, other)
                return
                                    
            if not password:
                raise exceptions.WebServiceAuthorizationFailed("AUTH_REQUIRED")
            
            if hashers.check_password(password, other.password):
                self._merge(user, email, other)            
                return
            
            raise exceptions.WebServiceAuthorizationFailed("AUTH_INVALID")
            
        def _merge(self, user, email, other):
            from yoolotto.legacy.migration.tickets import MigrateTickets
            from yoolotto.legacy.models import Tickets as LegacyTicket, Users as LegacyUser
            
            # Import Data
            try:
                for legacy in LegacyUser.objects.filter(email=email):
                        MigrateTickets(legacy, user).run()
            except:
                pass
            # Users
            devices = user.devices.all()
            
            # Magic Here...
            for device in devices:
                device.user = other
                device.save()
                
            other.save()        
Exemplo n.º 3
0
    def post(self, request):
        """
        Updates the details of the currently logged in user
        """
        # Short-Circuit for Authentication Errors caused by invalid Device IDs
	

	if request.yoo["new_version"]:
         if not request.yoo["user"] and request.yoo["auth"].get("__internal__reject__", False):
            raise exceptions.WebServiceAuthorizationFailed()
         user = request.yoo["user"]
         device = request.yoo["device"]
        
         try:
            data = json.loads(request.body)
         except:
            data = request.POST
            
         form = UserUpdateForm(data)
         if not form.is_valid():
            raise exceptions.WebServiceFormValidationFailed(form)

         email = form.cleaned_data["email"]
         password = form.cleaned_data["password"]
	        
         # Routing Logic        
         if email and password:
            self._modify_email(user, email, password)
         elif email:
            self._modify_email(user, email, password=None)
         elif password:
            self._modify_password(user, password)
        
         existing = dict([[x.name, x] for x in user.tokens.all()])
        
         for key in ["fbToken", "twitterToken", "contestEmail", "requestedEmail"]:
            if key not in data:
                continue
            
            if not form.cleaned_data[key]:
                if key in existing:
                    existing[key].delete()
            else:
                if key in existing:
                    existing[key].token = form.cleaned_data[key]
                else:
                    existing[key] = UserToken(user=user, name=key, token=form.cleaned_data[key])
            
                existing[key].save()
                
         if "pushToken" in form.cleaned_data and form.cleaned_data["pushToken"]:
            request.yoo["device"].device_token = form.cleaned_data["pushToken"]
            request.yoo["device"].save()
            
         if "referral" in form.cleaned_data and form.cleaned_data["referral"]:
            user.referral = form.cleaned_data["referral"]
            user.save()

         if email:
            try:
                coins_record, created = EmailCoins.objects.get_or_create(email=email,defaults={'coins': 0})
            except:
                coins_record = EmailCoins.objects.filter(email=email)[0]
            coins_record.save()

        
         if user.password and user.email or user.tokens.filter(name="requestedEmail").exists():
            try:
                CoinTicketGenericTransaction.objects.get(user=user, type="register")
            except CoinTicketGenericTransaction.DoesNotExist:
                transaction = CoinTransaction(wallet=user.get_wallet())
                transaction.save()
                
                _coins = CoinTicketGenericTransaction(user=user, type="register", transaction=transaction)
                _coins.save()
                                
                transaction.amount = _coins.amount
                transaction.save()
		try:
			device_record = DeviceCoins.objects.get(device_id=request.yoo['device'])
		except:
			device_record = DeviceCoins.objects.filter(device_id=request.yoo['device'])[0]
                device_record.coins = F('coins') + int(_coins.amount)
                device_record.save()

         try:
        	ClientLoginRecord, created = UserClientLogin.objects.get_or_create(device=request.yoo['device'], defaults={})
	 except:
		ClientLoginRecord  = UserClientLogin.objects.filter(device=request.yoo['device'])[0]
         if data.has_key('fbToken')  or data.has_key('twitterToken'):
            ClientLoginRecord.client_login = data['user_email']
         else:
            ClientLoginRecord.client_login = email
        # ClientLoginRecord.save()

         try:
            if created:
                token_info = UserToken.objects.filter(user_id = user.id)
                if token_info.name == "fbToken":
                    coins_record.coins = F('coins')+0.5
                    coins_record.save()
         except:
            pass


         
         return self.get(request)
        else:
	  
         # Short-Circuit for Authentication Errors caused by invalid Device IDs
         if not request.yoo["user"] and request.yoo["auth"].get("__internal__reject__", False):
            raise exceptions.WebServiceAuthorizationFailed()
        
         user = request.yoo["user"]
         device = request.yoo["device"]
        
         try:
            data = json.loads(request.body)
         except:
            data = request.POST
        
         form = UserUpdateForm(data)
         if not form.is_valid():
            raise exceptions.WebServiceFormValidationFailed(form)

         email = form.cleaned_data["email"]
         password = form.cleaned_data["password"]
                
         # Routing Logic        
         if email and password:
            self._modify_email(user, email, password)
         elif email:
            self._modify_email(user, email, password=None)
         elif password:
            self._modify_password(user, password)
        
         existing = dict([[x.name, x] for x in user.tokens.all()])
        
         for key in ["fbToken", "twitterToken", "contestEmail", "requestedEmail"]:
            if key not in data:
                continue
            
            if not form.cleaned_data[key]:
                if key in existing:
                    existing[key].delete()
            else:
                if key in existing:
                    existing[key].token = form.cleaned_data[key]
                else:
                    existing[key] = UserToken(user=user, name=key, token=form.cleaned_data[key])
            
                existing[key].save()
                
         if "pushToken" in form.cleaned_data and form.cleaned_data["pushToken"]:
            request.yoo["device"].device_token = form.cleaned_data["pushToken"]
            request.yoo["device"].save()
            
         if "referral" in form.cleaned_data and form.cleaned_data["referral"]:
            user.referral = form.cleaned_data["referral"]
            user.save()
        
         if user.tokens.filter(name="requestedEmail").exists():
            try:
                CoinTicketGenericTransaction.objects.get(user=user, type="register")
            except CoinTicketGenericTransaction.DoesNotExist:
                transaction = CoinTransaction(wallet=user.get_wallet())
                transaction.save()
                
                _coins = CoinTicketGenericTransaction(user=user, type="register", transaction=transaction)
                _coins.save()
                                
                transaction.amount = _coins.amount
                transaction.save()
        
         return self.get(request)