def login(request): if request.POST: if 'login-submit' in request.POST.keys(): try: header = base64.b64encode( bytes(request.POST['username'] + ':' + request.POST['password'], encoding='utf-8')).decode("ascii") services.get_user_data(header) request.session['base64'] = header return HttpResponseRedirect('/') except requests.exceptions.RequestException as err: if err.response.status_code == 401: messages.warning(request, 'Password incorrect.') return render(request, 'login.html', {'title': 'Login page', 'login': '******', 'register': ''}) else: raise err elif 'register-submit' in request.POST.keys(): if len(request.POST['username']) > 30: messages.warning(request, 'Username is too long') if len(request.POST['password']) > 30: messages.warning(request, 'Password is too long') if len(request.POST['password']) < 3: messages.warning(request, 'Password must be at least 3 characters long.') if len(request.POST['username']) < 3: messages.warning(request, 'Username must be at least 3 characters long.') if not re.match(r'[A-Za-z\-_\.0-9]+', request.POST['username']): messages.warning(request, 'Username must contain only letters, numbers or "_","-",".",.') if not re.match(r'[A-Za-z\-_\.0-9]+', request.POST['password']): messages.warning(request, 'Passwords must contain only letters, numbers or "_","-",".",.') if request.POST['password'] != request.POST['confirm-password']: messages.warning(request, 'Passowords does not match.') if not messages.get_messages(request): try: services.register(request.POST['username'], request.POST['password'], request.POST['email']) return HttpResponseRedirect('/') except requests.exceptions.RequestException as err: if err.response.status_code == 409: messages.warning(request, 'Username already used.') return render(request, 'login.html', {'title': 'Login page', 'login': '', 'register': 'active'}) else: raise err else: return render(request, 'login.html', {'title': 'Login page', 'login': '', 'register': 'active'}) if 'base64' in request.session: return redirect('main') return render(request, 'login.html', {'title': 'Login page', 'login': '******', 'register': '', 'errors': ''})
def user_page(request): data = services.get_user_data(request.session['base64']) if data: return render(request, 'user_page.html', {'title': 'user page', 'image_source': 'static/img/home-bg.jpg', 'heading': 'Here you can manage your blogs.', 'blogs': data['blogs']}) return HttpResponseRedirect('/login')
def blog(request, blog_id): blog = services.get_blog(blog_id) if logged(request): blog['user_blogs'] = filter(lambda x: x['id'] != blog['id'], services.get_user_data(request.session['base64'])['blogs']) if request.POST: a='' for key, value in request.POST.items(): if key != 'csrfmiddlewaretoken' and value == 'on': a=services.post_recommendation(key, blog['id'], request.session['base64']) if a: blog.update(a) return render(request, 'blog.html', blog)
def add_post(request, blog_id): user_data = services.get_user_data(request.session['base64']) if user_data: if user_data['username'] not in services.get_blog(blog_id)['owners']: return HttpResponse(status=404) if request.method == 'POST': data = dict(request.POST) services.post_post(data, blog_id, request.session['base64']) messages.info(request,'Post created.') return HttpResponseRedirect('/user_page/blog/' + blog_id) return render(request, 'add_post.html', {'title': 'add post', 'image_source': 'https://snap-photos.s3.amazonaws.com/img-thumbs/960w/I98ZOPE9FM.jpg', 'heading': 'Here you can add a post.', 'subheading': 'To add an image type link in new line with prefix #img:'}) return HttpResponseRedirect('/login')
def user_post(request, post_id): user_data = services.get_user_data(request.session['base64']) post = services.get_post(post_id) return render(request, 'user_post.html', post)
def user_blog(request, blog_id): user_data = services.get_user_data(request.session['base64']) blog = services.get_blog(blog_id) if user_data['username'] not in blog['owners']: return HttpResponseRedirect('/') return render(request, 'user_blog.html', blog)