def recover_password_for_user(request): email = None try: email = request.POST['email'] except: return HttpResponse( "O Email não apresenta uma sintaxe correcta.") if not validateEmail(email): return HttpResponse( "O Email não apresenta uma sintaxe correcta.") new_password = generate_random_string() user = Page_Users.all().filter('email =', email).get() if user == None: return 'Utilizador inexistente' erros = change_password_for_user(user.id, new_password) if erros == "Success": mail_utils.sendPasswordReset(user.email, new_password, user.nome) return HttpResponse(erros)
def recover_password_for_user(request): email = None try: email = request.POST['email'] except: return HttpResponse("O Email não apresenta uma sintaxe correcta.") if not validateEmail(email): return HttpResponse("O Email não apresenta uma sintaxe correcta.") new_password = generate_random_string() user = Page_Users.all().filter('email =',email).get() if user == None: return 'Utilizador inexistente' erros = change_password_for_user(user.id,new_password) if erros == "Success": mail_utils.sendPasswordReset(user.email,new_password,user.nome) return HttpResponse(erros)
def get_random_card_prepare(request): WINNER = False raridade = None accepted_rarities = ["Common","Uncommon","Rare"] try: raridade = request.POST['rarity'] except: return "Erro###Raridade inválida" if raridade not in accepted_rarities: return "Erro###Raridade inválida" #Get user user = Page_Users.all().filter("email =", request.user.email).get() if user.clicks_total % constants.NUMBER_OCCURRENCES == 0 and user.clicks_total != 0 and constants.WINNERS_ACTIVE: WINNER = True #RETIRAR GUITA AO GAJO #Actualiza o numero de clicks de acordo com raridade #faz reset aos clicks nas seguintes condicoes #rara = 100 -> reset #incomum = 150 -> reset #comum = 150 -> reset user = page_users_utils.change_saldo_user(user,raridade,winner=WINNER) if user == None: return "Erro###Saldo insuficiente para efectuar a compra" #gets card #carta = get_random_card(raridade) carta = prepare_get_random_card(raridade,logica_de_negocio(user,raridade)) #ADD CARTA AND USER TO MODEL COMPRAS!!! compras_historico_utils.add_to_compras(request.user.email,carta) #-1 NO STOCK DA CARTA card_database_utils.decrement_card_stock(carta) #Adiciona cartas ao return if WINNER: toReturn = object_card_to_string(carta,winner=True) else: toReturn = object_card_to_string(carta) #Adiciona novo saldo toReturn = toReturn + "###" + str(user.saldo) logging.info("O user " + request.user.email + " comprou a carta " + carta.nome + " com raridade " + carta.raridade) return toReturn
def page_login(request): username = "" password = "" try: username = request.POST['username'] password = request.POST['password'] except: try: if not user.is_active: return direct_to_template(request, "index.html", {'error': "Conta desabilitada"}) if request.user.is_staff: request.session['Pais'] = get_pais_from_email( request.user.email) return HttpResponseRedirect("staff_admin") if request.user.is_authenticated(): request.session['Pais'] = get_pais_from_email( request.user.email) return HttpResponseRedirect("main") except: return direct_to_template(request, "index.html") username = username.lower() #get real username real_user = Page_Users.all().filter('email =', username).get() if real_user == None: return direct_to_template( request, "index.html", {'error': "Username ou password incorrectos."}) username = real_user.id user = authenticate(username=username, password=password) if user is not None: if user.is_active: login(request, user) else: return direct_to_template(request, "index.html", {'error': "Conta desabilitada"}) else: return direct_to_template( request, "index.html", {'error': "Username ou password incorrectos."}) if request.user.is_staff: request.session['Pais'] = get_pais_from_email(request.user.email) return HttpResponseRedirect("staff_admin") if request.user.is_authenticated(): request.session['Pais'] = get_pais_from_email(request.user.email) return HttpResponseRedirect("main") if not request.user.is_authenticated(): return direct_to_template( request, "index.html", {'error': "Username ou password incorrectos."}) return direct_to_template(request, "index.html", {'error': "Username ou password incorrectos."})
def change_info(request): user = request.user.email morada = None codigopostal = None codigopostal2 = None telefone = None #pais = None try: morada = request.POST['morada'] except: return HttpResponse('Morada mal preenchida') try: codigopostal = request.POST['codigopostal'] except: return HttpResponse('Código postal mal preenchido') try: codigopostal2 = request.POST['codigopostal2'] except: return HttpResponse('Código postal mal preenchido') try: telefone = request.POST['telefone'] except: return HttpResponse('Telefone mal preenchido') #try: # pais = request.POST['pais'] #except: # return HttpResponse('País mal preenchido') cod_postal = codigopostal+"-"+codigopostal2 #check server side info error = verify_partial_user_info(morada,codigopostal,telefone) #print in page error if any if error != None: return HttpResponse(error) else: user = Page_Users.all().filter("email =", user).get() user.morada = morada user.codigopostal = cod_postal user.telefone = telefone #user.pais = pais user.put() return HttpResponse("Success")
def change_info(request): user = request.user.email morada = None codigopostal = None codigopostal2 = None telefone = None #pais = None try: morada = request.POST['morada'] except: return HttpResponse('Morada mal preenchida') try: codigopostal = request.POST['codigopostal'] except: return HttpResponse('Código postal mal preenchido') try: codigopostal2 = request.POST['codigopostal2'] except: return HttpResponse('Código postal mal preenchido') try: telefone = request.POST['telefone'] except: return HttpResponse('Telefone mal preenchido') #try: # pais = request.POST['pais'] #except: # return HttpResponse('País mal preenchido') cod_postal = codigopostal + "-" + codigopostal2 #check server side info error = verify_partial_user_info(morada, codigopostal, telefone) #print in page error if any if error != None: return HttpResponse(error) else: user = Page_Users.all().filter("email =", user).get() user.morada = morada user.codigopostal = cod_postal user.telefone = telefone #user.pais = pais user.put() return HttpResponse("Success")
def update_saldo(request): user = None plus_saldo = None try: user = request.POST['email'] except: return HttpResponse('Valor no utilizador incorrecto') try: plus_saldo = int(request.POST['saldo']) except : return HttpResponse('Valor no saldo incorrecto') saldo_existente = 0 if not validateEmail(user): return HttpResponse('Email incorrecto') user = user.lower() user = Page_Users.all().filter("email =", user) if user.count() == 0: return HttpResponse('Utilizador inexistente') if user.count() != 1: return HttpResponse('Existe mais que um user com o mesmo email, erro severo') #update saldo user_actual = user[0] saldo_existente = user_actual.saldo user_actual.saldo = saldo_existente + plus_saldo #update saldo total saldo_total = user_actual.saldo_total saldo_total = saldo_total + plus_saldo user_actual.saldo_total = saldo_total #put user_actual.put() logging.info("CARREGAMENTO: O user " + request.user.email + " carregou o user " + user_actual.email + " com " + str(plus_saldo) + " Creditos") return HttpResponse('Efectuado com sucesso')
def get_pais_from_email(email): #user from page user = Page_Users.all().filter("email =", email) if user.count() != 1: return "" if user.count() == 0: return "" user_actual = user[0] return user_actual.pais
def update_saldo(request): user = None plus_saldo = None try: user = request.POST['email'] except: return HttpResponse('Valor no utilizador incorrecto') try: plus_saldo = int(request.POST['saldo']) except: return HttpResponse('Valor no saldo incorrecto') saldo_existente = 0 if not validateEmail(user): return HttpResponse('Email incorrecto') user = user.lower() user = Page_Users.all().filter("email =", user) if user.count() == 0: return HttpResponse('Utilizador inexistente') if user.count() != 1: return HttpResponse( 'Existe mais que um user com o mesmo email, erro severo') #update saldo user_actual = user[0] saldo_existente = user_actual.saldo user_actual.saldo = saldo_existente + plus_saldo #update saldo total saldo_total = user_actual.saldo_total saldo_total = saldo_total + plus_saldo user_actual.saldo_total = saldo_total #put user_actual.put() logging.info("CARREGAMENTO: O user " + request.user.email + " carregou o user " + user_actual.email + " com " + str(plus_saldo) + " Creditos") return HttpResponse('Efectuado com sucesso')
def page_login(request): username = "" password = "" try: username = request.POST['username'] password = request.POST['password'] except: try: if not user.is_active: return direct_to_template(request, "index.html", {'error': "Conta desabilitada"}) if request.user.is_staff: request.session['Pais'] = get_pais_from_email(request.user.email) return HttpResponseRedirect("staff_admin") if request.user.is_authenticated(): request.session['Pais'] = get_pais_from_email(request.user.email) return HttpResponseRedirect("main") except: return direct_to_template(request, "index.html") username = username.lower() #get real username real_user = Page_Users.all().filter('email =',username).get() if real_user == None: return direct_to_template(request, "index.html", {'error': "Username ou password incorrectos."}) username = real_user.id user = authenticate(username=username, password=password) if user is not None: if user.is_active: login(request, user) else: return direct_to_template(request, "index.html", {'error': "Conta desabilitada"}) else: return direct_to_template(request, "index.html", {'error': "Username ou password incorrectos."}) if request.user.is_staff: request.session['Pais'] = get_pais_from_email(request.user.email) return HttpResponseRedirect("staff_admin") if request.user.is_authenticated(): request.session['Pais'] = get_pais_from_email(request.user.email) return HttpResponseRedirect("main") if not request.user.is_authenticated(): return direct_to_template(request, "index.html", {'error': "Username ou password incorrectos."}) return direct_to_template(request, "index.html", {'error': "Username ou password incorrectos."})
def change_saldo_user_checkout_by_metodo(user, metodo): CORREIO_INTERNACIONAL = constants.CORREIO_INTERNACIONAL CORREIO_AZUL = constants.CORREIO_AZUL CORREIO_REGISTADO = constants.CORREIO_REGISTADO CORREIO_INTERNACIONAL_COM_RASTREIO = constants.CORREIO_INTERNACIONAL_COM_RASTREIO user = Page_Users.all().filter("email =", user).get() saldo = user.saldo if metodo == "Internacional": if saldo < CORREIO_INTERNACIONAL: return False else: saldo = saldo - CORREIO_INTERNACIONAL user.saldo = saldo user.put() return True if metodo == "Azul": if saldo < CORREIO_AZUL: return False else: saldo = saldo - CORREIO_AZUL user.saldo = saldo user.put() return True if metodo == "Registado": if saldo < CORREIO_REGISTADO: return False else: saldo = saldo - CORREIO_REGISTADO user.saldo = saldo user.put() return True if metodo == "Internacional Rastreio": if saldo < CORREIO_INTERNACIONAL_COM_RASTREIO: return False else: saldo = saldo - CORREIO_INTERNACIONAL_COM_RASTREIO user.saldo = saldo user.put() return True
def change_saldo_user_checkout_by_metodo(user,metodo): CORREIO_INTERNACIONAL = constants.CORREIO_INTERNACIONAL CORREIO_AZUL = constants.CORREIO_AZUL CORREIO_REGISTADO = constants.CORREIO_REGISTADO CORREIO_INTERNACIONAL_COM_RASTREIO = constants.CORREIO_INTERNACIONAL_COM_RASTREIO user = Page_Users.all().filter("email =", user).get() saldo = user.saldo if metodo == "Internacional": if saldo < CORREIO_INTERNACIONAL: return False else: saldo = saldo - CORREIO_INTERNACIONAL user.saldo = saldo user.put() return True if metodo == "Azul": if saldo < CORREIO_AZUL: return False else: saldo = saldo - CORREIO_AZUL user.saldo = saldo user.put() return True if metodo == "Registado": if saldo < CORREIO_REGISTADO: return False else: saldo = saldo - CORREIO_REGISTADO user.saldo = saldo user.put() return True if metodo == "Internacional Rastreio": if saldo < CORREIO_INTERNACIONAL_COM_RASTREIO: return False else: saldo = saldo - CORREIO_INTERNACIONAL_COM_RASTREIO user.saldo = saldo user.put() return True
def sell_card(request): nome = None edicao = None raridade = None try: nome = request.POST['nome'] edicao = request.POST['edicao'] raridade = request.POST['raridade'] except: return "Erro###Campo com valor incorrecto" #card = cartas_mtg.all().filter('nome =', nome).filter('edicao > ',edicao).filter('raridade =',raridade).get() card_query = cartas_mtg.search(nome + " " + edicao + " " + raridade) try: card = card_query[0] except: return "Erro###Carta inexistente" #remover das compras found = compras_historico_utils.delete_card_from_compras(request.user.email, nome, edicao, raridade) if not found: return "Erro###Carta inexistente nas suas compras" #Get user user = Page_Users.all().filter("email =", request.user.email).get() #adicionar ao saldo do gajo user = page_users_utils.change_saldo_user(user, raridade, True) #adicionar a +1 ao stock da carta card_database_utils.increment_card_stock(card) return "Saldo###" + str(user.saldo)
def add_permanent_user(email, nome, morada, codigopostal, telefone, pais): id = None while (True): id = uuid.uuid4().hex[:30] user = Page_Users.all().filter("id =", id).get() if user == None: break #My DB user = Page_Users(email=email, id=id, nome=nome, morada=morada, codigopostal=codigopostal, telefone=telefone, pais=pais) user.put() #Django user = User.objects.create_user(id, email, password) user.is_staff = False user.save()
def change_password_for_user(user,new_password): #u user of django try: u = User.objects.get(username__exact=user) except: return 'Utilizador inexistente' #user from page user = Page_Users.all().filter("id =", user).get() if user == None: return 'Utilizador inexistente' #change in the django u.set_password(new_password) u.save() #change in the user user.password = new_password user.put() return "Success"
def change_password_for_user(user, new_password): #u user of django try: u = User.objects.get(username__exact=user) except: return 'Utilizador inexistente' #user from page user = Page_Users.all().filter("id =", user).get() if user == None: return 'Utilizador inexistente' #change in the django u.set_password(new_password) u.save() #change in the user user.password = new_password user.put() return "Success"
def add_permanent_user(email,nome,morada,codigopostal,telefone,pais): id = None while(True): id = uuid.uuid4().hex[:30] user = Page_Users.all().filter("id =", id).get() if user == None: break #My DB user = Page_Users(email = email, id = id, nome = nome, morada = morada, codigopostal = codigopostal, telefone = telefone, pais = pais) user.put() #Django user = User.objects.create_user(id, email, password) user.is_staff = False user.save()
def search_users(request): keywords = request.POST['keywords'] users = Page_Users.search(keywords) return HttpResponse(users_to_table(users))
def change_saldo_user(user_temp, rarity, inverted=False, winner=False): value = None if rarity == "Rare": value = VALUE_OF_RARE_CARD if rarity == "Mytic": value = VALUE_OF_RARE_CARD if rarity == "Uncommon": value = VALUE_OF_UNCOMMON_CARD if rarity == "Common": value = VALUE_OF_COMMON_CARD user_actual = Page_Users.all().filter("email =", user_temp.email).get() saldo_existente = user_actual.saldo saldo_novo = None if inverted: saldo_novo = saldo_existente + value / 2 else: saldo_novo = saldo_existente - value if saldo_novo < 0: return None user_actual.saldo = saldo_novo #WON (Y clicks) if winner: user_actual.saldo = saldo_novo + constants.WINNER_VALUE if inverted == False: n_clicks = user_actual.clicks_total n_clicks = n_clicks + 1 user_actual.clicks_total = n_clicks if rarity == "Rare": user_actual.clicks_total_rare = user_actual.clicks_total_rare + 1 user_actual.clicks_total_rare_fix = user_actual.clicks_total_rare_fix + 1 if user_actual.clicks_total_rare > constants.RESET_CLICKS_RARE: user_actual.clicks_total_rare = 1 if rarity == "Mytic": user_actual.clicks_total_rare = user_actual.clicks_total_rare + 1 user_actual.clicks_total_rare_fix = user_actual.clicks_total_rare_fix + 1 if user_actual.clicks_total_rare > constants.RESET_CLICKS_RARE: user_actual.clicks_total_rare = 1 if rarity == "Uncommon": user_actual.clicks_total_uncommon = user_actual.clicks_total_uncommon + 1 user_actual.clicks_total_uncommon_fix = user_actual.clicks_total_uncommon_fix + 1 if user_actual.clicks_total_uncommon > constants.RESET_CLICKS_UNCOMMON: user_actual.clicks_total_uncommon = 1 if rarity == "Common": user_actual.clicks_total_common = user_actual.clicks_total_common + 1 user_actual.clicks_total_common_fix = user_actual.clicks_total_common_fix + 1 if user_actual.clicks_total_common > constants.RESET_CLICKS_COMMON: user_actual.clicks_total_common = 1 user_actual.put() return user_actual
def update_saldo_from_user(user,value): user = Page_Users.all().filter("email =", user).get() user.saldo = user.saldo + value user.put()
def change_saldo_user(user_temp,rarity, inverted=False, winner=False): value = None if rarity == "Rare": value = VALUE_OF_RARE_CARD if rarity == "Mytic": value = VALUE_OF_RARE_CARD if rarity == "Uncommon": value = VALUE_OF_UNCOMMON_CARD if rarity == "Common": value = VALUE_OF_COMMON_CARD user_actual = Page_Users.all().filter("email =", user_temp.email).get() saldo_existente = user_actual.saldo saldo_novo = None if inverted: saldo_novo = saldo_existente + value/2 else: saldo_novo = saldo_existente - value if saldo_novo < 0: return None user_actual.saldo = saldo_novo #WON (Y clicks) if winner: user_actual.saldo = saldo_novo + constants.WINNER_VALUE if inverted == False: n_clicks = user_actual.clicks_total n_clicks = n_clicks + 1 user_actual.clicks_total = n_clicks if rarity == "Rare": user_actual.clicks_total_rare = user_actual.clicks_total_rare + 1 user_actual.clicks_total_rare_fix = user_actual.clicks_total_rare_fix + 1 if user_actual.clicks_total_rare > constants.RESET_CLICKS_RARE: user_actual.clicks_total_rare = 1 if rarity == "Mytic": user_actual.clicks_total_rare = user_actual.clicks_total_rare + 1 user_actual.clicks_total_rare_fix = user_actual.clicks_total_rare_fix + 1 if user_actual.clicks_total_rare > constants.RESET_CLICKS_RARE: user_actual.clicks_total_rare = 1 if rarity == "Uncommon": user_actual.clicks_total_uncommon = user_actual.clicks_total_uncommon + 1 user_actual.clicks_total_uncommon_fix = user_actual.clicks_total_uncommon_fix + 1 if user_actual.clicks_total_uncommon > constants.RESET_CLICKS_UNCOMMON: user_actual.clicks_total_uncommon = 1 if rarity == "Common": user_actual.clicks_total_common = user_actual.clicks_total_common + 1 user_actual.clicks_total_common_fix = user_actual.clicks_total_common_fix + 1 if user_actual.clicks_total_common > constants.RESET_CLICKS_COMMON: user_actual.clicks_total_common = 1 user_actual.put() return user_actual
def update_saldo_from_user(user, value): user = Page_Users.all().filter("email =", user).get() user.saldo = user.saldo + value user.put()
def get_saldo_from_email(email): #user from page user = Page_Users.all().filter("email =", email).get() return user.saldo
def sell_all(request , admin_user=None): user = None if admin_user != None: #get user #ADMIN ONLY user = Page_Users.all().filter("email =", admin_user).get() logging.info("ADMIN CALLING") logging.info(user.email) else: #get user user = Page_Users.all().filter("email =", request.user.email).get() #compras carrinho = None if admin_user != None: carrinho = compras.all().filter("user ="******"user ="******"Magic 2012","2012 Core Set (M12)") #END logging.info(raridade) card_query = cartas_mtg.search(nome + " " + edicao + " " + raridade) card = card_query[0] toAdd = 15 #adicionar saldo if raridade == "Rare": toAdd = constants.VALUE_OF_RARE_CARD/2 elif raridade == "Mytic": toAdd = constants.VALUE_OF_RARE_CARD/2 elif raridade == "Uncommon": toAdd = constants.VALUE_OF_UNCOMMON_CARD/2 elif raridade == "Common": toAdd = constants.VALUE_OF_COMMON_CARD/2 valor = valor + toAdd #adicionar a +1 ao stock da carta card_database_utils.increment_card_stock(card) #actualizar carrinho carrinho.cartas = [] carrinho.put() #actualizar user saldo_antigo = user.saldo novo_saldo = saldo_antigo + valor user.saldo = novo_saldo user.put()
def get_user_info(email): return Page_Users.all().filter("email =", email).get()