def post(self, request, _id): user = request.yoo["user"] ticket = LotteryTicket.objects.get(pk=_id) if ticket.user != user: raise exceptions.WebServiceAuthorizationFailed() try: data = json.loads(request.body) except: data = {} type = data.get("type", None) if type not in ["facebook", "twitter"]: raise exceptions.WebServiceException("Unsupported Share Type") if not ticket.coin_share_transaction.filter(type=type).exists(): parent = CoinTransaction(wallet=user.get_wallet()) parent.save() transaction = CoinShareTransaction(ticket=ticket, type=type, transaction=parent) transaction.save() parent.amount = transaction.amount parent.save() return self.get(request, _id)
def update(self): if self._device: self._allocated = 0 # Process Checked v.s. Unchecked if not CoinTicketTransaction.objects.filter(ticket=self._ticket).exists(): transaction = CoinTransaction(wallet=self._wallet) transaction.save() try: self.add_coins() except: pass # Create Record linking Ticket to Transaction transaction_ticket = CoinTicketTransaction(ticket=self._ticket, transaction=transaction) transaction_ticket.save() else: transaction_ticket = CoinTicketTransaction.objects.get(ticket=self._ticket) transaction = transaction_ticket.transaction # Allocate Coins # Changed: This will never de-allocate a coin, if you submit and check # ONCE you get the coin, if you add more submissions and do not check # this coin is not lost. coins = COINS_PER_PLAY if self._ticket.unchecked == 0 else 0 if transaction.amount < coins: self.add_coins() transaction.amount = coins transaction_ticket.checked = True transaction.save() transaction_ticket.save() self._allocated += coins del transaction # Process for Submissions # For every submission we grant COINS_PER_PLAY __regardless__ of how # many plays there are for that submission. In addition there is a limit # of COINS_PER_DAY_MAX via this mechanism (others are not effected) submissions = self._ticket.submissions.all() for submission in submissions: if not CoinSubmissionTransaction.objects.filter(submission=submission).exists(): transaction = CoinTransaction(wallet=self._wallet) transaction.save() # Create Record linking Ticket to Transaction transaction_submission = CoinSubmissionTransaction(submission=submission, transaction=transaction) transaction_submission.save() else: transaction_submission = CoinSubmissionTransaction.objects.get(submission=submission) transaction = transaction_submission.transaction coins = COINS_PER_PLAY delta = coins - transaction.amount if not delta > 0: continue # Maximum number of submission coins that remain to be granted for # the day remaining = COINS_PER_DAY_MAX - CoinTicketManager.for_user_on_day( self._wallet.user, submission.added_at ) if not remaining: continue transaction.amount = coins transaction.save() self._allocated += coins else: self._allocated = 0 # Process Checked v.s. Unchecked if not CoinTicketTransaction.objects.filter(ticket=self._ticket).exists(): transaction = CoinTransaction(wallet=self._wallet) transaction.save() # Create Record linking Ticket to Transaction transaction_ticket = CoinTicketTransaction(ticket=self._ticket, transaction=transaction) transaction_ticket.save() else: transaction_ticket = CoinTicketTransaction.objects.get(ticket=self._ticket) transaction = transaction_ticket.transaction # Allocate Coins # Changed: This will never de-allocate a coin, if you submit and check # ONCE you get the coin, if you add more submissions and do not check # this coin is not lost. coins = COINS_PER_PLAY if self._ticket.unchecked == 0 else 0 if transaction.amount < coins: transaction.amount = coins transaction_ticket.checked = True transaction.save() transaction_ticket.save() self._allocated += coins del transaction # Process for Submissions # For every submission we grant COINS_PER_PLAY __regardless__ of how # many plays there are for that submission. In addition there is a limit # of COINS_PER_DAY_MAX via this mechanism (others are not effected) submissions = self._ticket.submissions.all() for submission in submissions: if not CoinSubmissionTransaction.objects.filter(submission=submission).exists(): transaction = CoinTransaction(wallet=self._wallet) transaction.save() # Create Record linking Ticket to Transaction transaction_submission = CoinSubmissionTransaction( submission=submission, transaction=transaction ) transaction_submission.save() else: transaction_submission = CoinSubmissionTransaction.objects.get(submission=submission) transaction = transaction_submission.transaction coins = COINS_PER_PLAY delta = coins - transaction.amount if not delta > 0: continue # Maximum number of submission coins that remain to be granted for # the day remaining = COINS_PER_DAY_MAX - CoinTicketManager.for_user_on_day( self._wallet.user, submission.added_at ) if not remaining: continue transaction.amount = coins transaction.save() self._allocated += coins
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()
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)
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)