def register(request): error = '' if request.method == "POST": login = request.POST['login'] username = request.POST['username'] email = request.POST['email'] password = request.POST['password'] bd = base(request) bd.execute( "select login, username from users where login = '******' and username = '******'" % (login, username)) res = bd.fetchone() if res is not None: return HttpResponseRedirect('/invalid') else: bd.execute( "insert into USERS (login, username, email, password) VALUES ('%s','%s','%s','%s')" % (login, username, email, password)) # добавление пользователя bd.execute("select id from users where login = '******'" % (login)) id = bd.fetchone() if id is None: error = 'no string' else: id = id[0] request.session.modefied = True request.session.save() return HttpResponseRedirect('/detail/') return render(request, 'registration/regform.html', {'error': error})
def get_user(request): id = get_session(request) if isinstance(id, HttpResponseRedirect): return id bd = base(request) bd.execute("select id, email, username from users where id = '%s'" % id) tup = bd.fetchone() # tup = (7, '*****@*****.**', 'sfsdfs') # user = {'id': tup[0], 'email': tup[1], 'username': tup[2]} user = dict(zip(('id', 'email', 'username'), tup)) return user
def add(request): user = get_user(request) id = user['id'] bd = base(request) a = request.POST.__contains__('add') if a is True: ad = request.POST['add_contact'] if ad is not None: bd.execute("select id from users where login = '******'" % (ad)) # проверка на наличие логина в базе contact_id = bd.fetchone()[0] if contact_id is not None: bd.execute( "insert into contacts (contact_id, id_user) VALUES ('%s', '%s')" % (contact_id, id)) # добавление пользователя return HttpResponseRedirect('/adressbook/')
def confirm_account(request, username, key): context = base(req=request) confirm_obj = get_object_or_404(UserConfrimationKeys, user__username=username, key=key, expired_date__gte=datetime.now(), expired=False) confirm_obj.expired = True confirm_obj.user.is_active = True confirm_obj.user.save() confirm_obj.save() # next_url = request.GET.get('next_url') # if next_url: # pass # else: context['message'] = _('Your account has been confirmed successfully') response = render(request, 'base-user/confirm.html', context=context) return response
def delete(request): user = get_user(request) id = user['id'] bd = base(request) d = request.POST.__contains__('del') if d is True: de = request.POST['del_contact'] if de is not None: bd.execute("select id from users where login = '******'" % (de)) # проверка на наличие логина в базе del_id = bd.fetchone()[0] if del_id is not None: bd.execute( "select id, contact_id, id_user from contacts where contact_id = '%s' AND id_user = '******'" % (del_id, id) ) # проверка на наличие контакта в адресной книге пользователя if bd.rowcount > 0: id = bd.fetchone()[0] bd.execute("DELETE FROM contacts WHERE id = '%s'" % (id)) return HttpResponseRedirect('/adressbook/')
def signup(request): signup_form = UserRegistrationForm(request.POST or None) context = base(req=request) if request.method == 'POST': if signup_form.is_valid(): clean_data = signup_form.cleaned_data name = clean_data.get('name') surname = clean_data.get('surname') username = clean_data.get('username') email = clean_data.get('email') phone = clean_data.get('phone') password = clean_data.get('password') retype_password = clean_data.get('retype_password') random_string = str(random.random()).encode('utf8') salt = hashlib.sha1(random_string).hexdigest()[:5] # activation_key = hashlib.sha1(salted).hexdigest() # # key_expires = datetime.datetime.today() + datetime.timedelta(1) password = make_password(password, salt=salt) user_obj = GUser(first_name=name, last_name=surname, email=email, username=username, phone=phone, password=password, usertype=3, is_active=False) user_obj.save() context['signup_message'] = _('Please confirm your email') signup_form = UserRegistrationForm() context['signup_form'] = signup_form response = render(request, 'base-user/signup.html', context=context) return response
def adressbook(request): user = get_user(request) id = user['id'] if isinstance(user, HttpResponseRedirect): return HttpResponseRedirect('/') bd = base(request) # вывод всех контактов из адресной книги bd.execute("select id, contact_id from contacts WHERE id_user = '******'" % (id)) logins = [] if bd.rowcount > 0: contact_ids = [] for c in bd.fetchall(): contact_ids.append(str(c[1])) bd.execute("select id, login from users WHERE id IN (%s)" % (','.join(contact_ids))) for c in bd.fetchall(): logins.append(str(c[1])) template_params = {'user': user, 'logins': logins} return render(request, 'adressbook/adbook.html', template_params)
def chat(request): user = get_user(request) contact = request.path_info.replace('/chat/', '') bd = base(request) if isinstance(user, HttpResponseRedirect): return HttpResponseRedirect('/') messages = {} id_cont = get_id_contact(request, contact) id = user['id'] user_logins = {id: user['username'], id_cont: contact} temp = { 'contact': contact, 'user': user, 'mes': messages, 'logins': user_logins } ids = (','.join(map(str, (id, id_cont)))) bd.execute( "select id_mes, id_sender, id_recipient, text, send_at from messages where id_sender IN (%s) AND " "id_recipient IN (%s) ORDER BY send_at" % (ids, ids)) if bd.rowcount > 0: for item in bd.fetchall(): item = dict( zip(('id', 'sender', 'recipient', 'text', 'send'), item)) item['send'] = item['send'].strftime("%H:%M:%S %d.%m.%Y") item['login'] = user_logins[item['sender']] messages[item['id']] = item if request.method == 'POST': text = request.POST['sender'] d = datetime.strftime(datetime.now(), "%Y.%m.%d %H:%M:%S") if text != '': bd.execute( "insert into messages (id_sender, id_recipient, send_at, text) VALUES ('%s', '%s', '%s', '%s')" % (id, id_cont, d, text)) return HttpResponseRedirect('/chat/%s' % temp['contact']) return render(request, 'chat/chat.html', temp)
def get_id_contact(request, cont): bd = base(request) bd.execute("select id from users where login = '******'" % (cont)) id_cont = bd.fetchone()[0] return id_cont
def log_in(request): login_form = LoginForm(request.POST or None) context = base(req=request) if request.user.is_authenticated(): return HttpResponseRedirect(reverse('panel:dashboard')) context['login_form'] = login_form next_url = request.GET.get('next_url') context['next_url'] = next_url # return HttpResponse(next_url) message_login = '' if request.method == 'POST': if login_form.is_valid(): clean_data = login_form.cleaned_data email = clean_data.get('lemail') password = clean_data.get('lpassword') remember_me = clean_data.get('remember_me') # if remember_me: # remember_me = True # else: # remember_me = False print('valid') try: # user_email = GUser.objects.get(email=email) # print("user_email={}".format(user_email.username)) a_user = auth.authenticate(username=email, password=password) if a_user is not None: if a_user.is_active: print("user.is_active") auth.login(request, a_user) # return HttpResponse(next_url) if next_url == 'None' or not next_url: next_url = reverse('panel:dashboard') else: pass # return HttpResponse(next_url) message_login = _("you are logined") print(message_login) return HttpResponseRedirect(next_url) else: print("user.is_active not ") message_login = _("Please wait for confirmed account") else: message_login = _("Email or password is incorrect") print( "----------------------------------------------------------------------------------------" ) print(message_login) except: message_login = _("Email or password is incorrect") print( "----------------------------------------------------------------------------------------" ) print(message_login) # else: # # print("user.is_active not") # # print(a_user) # message_login = _("email_or_password_incorrect") context['message_login'] = message_login response = render(request, 'base-user/signin.html', context=context) return response