def register_mongo(): if current_user.is_authenticated: return redirect(url_for('home')) form = RegistrationForm() if form.validate_on_submit(): loc_dict = record2dict(form.location.data) hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user = { '_id': str(ObjectId()), 'username': form.username.data, 'email': form.email.data, 'location': { 'city': loc_dict['city'], 'postal_code': str(loc_dict['postal_code']) }, 'password': hashed_password, 'phone': form.phone.data, 'role_id': 1, 'post_ids': [], 'following': [], 'image_file': 'default.jpg' } m_db.users.insert_one(user) flash('Your account has been created! You are now able to log in', 'success') return redirect(url_for('login_mongo')) return render_template('register.html', title='Register', form=form)
def makeoffer(request, post_id): userobj = request.user latest_post_list = Post.objects.filter(user=userobj).order_by('-id') postobj = Post.objects.filter(id=post_id)[:1].get() if request.method == 'POST': regform = RegistrationForm(request.POST) postform = PostForm(request.POST, request.FILES) offerform = OfferForm(request.POST) if offerform.is_valid(): offerobj = offerform.save(commit=False) offerobj.user = request.user offerobj.post = Post.objects.filter(id=post_id)[:1].get() offerobj.save() return redirect('/') elif regform.is_valid(): regform.save() return redirect('/') elif postform.is_valid(): obj = postform.save(commit=False) obj.user = request.user obj.image = postform.cleaned_data['image'] obj.save() postform.save_m2m() return redirect('/') else: postform = PostForm() regform = RegistrationForm() offerform = OfferForm() context = { 'regform': regform, 'postform': postform, 'offerform': offerform, 'posts': latest_post_list, 'postobj': postobj, } return login(request, context, template_name='marketplace/err.html') else: regform = RegistrationForm() postform = PostForm() offerform = OfferForm() context = { 'regform': regform, 'postform': postform, 'offerform': offerform, 'posts': latest_post_list, 'postobj': postobj, } return render(request, 'marketplace/offer.html', context)
def register(): if current_user.is_authenticated: return redirect(url_for('home')) form = RegistrationForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user = User(username=form.username.data, email=form.email.data, location=form.location.data, password=hashed_password, phone=form.phone.data) db.session.add(user) db.session.commit() flash('Your account has been created! You are now able to log in', 'success') return redirect(url_for('login')) return render_template('register.html', title='Register', form=form)
def userdetails(request, user_id): userobj = get_object_or_404(User, pk=user_id) user_latest_post = Post.objects.filter(user=userobj).order_by('-id') offers = Offer.objects.filter(post__in=user_latest_post).order_by('-id') myoffers = Offer.objects.filter(user=request.user, post__in=user_latest_post).order_by('-id') if request.method == 'POST': regform = RegistrationForm(request.POST) postform = PostForm(request.POST, request.FILES) if regform.is_valid(): regform.save() return redirect('/') elif request.user.is_authenticated(): if postform.is_valid(): obj = postform.save(commit=False) obj.user = request.user obj.image = postform.cleaned_data['image'] obj.save() postform.save_m2m() return redirect('/') else: postform = PostForm() regform = RegistrationForm() context = { 'userprofile': userobj, 'latest_posts': user_latest_post, 'regform': regform, 'postform': postform, 'offers': offers, 'myoffers': myoffers, } return login(request, context, template_name='marketplace/err.html') else: regform = RegistrationForm() postform = PostForm() context = { 'userprofile': userobj, 'latest_posts': user_latest_post, 'regform': regform, 'postform': postform, 'offers': offers, 'myoffers': myoffers, } return render(request, 'marketplace/user.html', context)
def tagresults(request, tag_name): postobj = Post.objects.filter(tags__name__in=[tag_name]).distinct() if request.method == 'POST': regform = RegistrationForm(request.POST) postform = PostForm(request.POST, request.FILES) if regform.is_valid(): regform.save() return redirect('/') elif request.user.is_authenticated(): if postform.is_valid(): obj = postform.save(commit=False) obj.user = request.user obj.image = postform.cleaned_data['image'] obj.save() postform.save_m2m() return redirect('/') else: postform = PostForm() regform = RegistrationForm() context = { 'query_item': tag_name, 'post': postobj, 'regform': regform, 'postform': postform, } return login(request, context, template_name='marketplace/err.html') else: regform = RegistrationForm() postform = PostForm() context = { 'query_item': tag_name, 'post': postobj, 'regform': regform, 'postform': postform, } return render(request, 'marketplace/results.html', context)
def photo(request, post_id): postobj = Post.objects.filter(id=post_id) if request.method == 'POST': regform = RegistrationForm(request.POST) postform = PostForm(request.POST, request.FILES) if regform.is_valid(): regform.save() return redirect('/') elif request.user.is_authenticated(): if postform.is_valid(): obj = postform.save(commit=False) obj.user = request.user obj.image = postform.cleaned_data['image'] obj.save() postform.save_m2m() return redirect('/') else: postform = PostForm() regform = RegistrationForm() context = { 'post': postobj, 'regform': regform, 'postform': postform, } return login(request, context, template_name='marketplace/err.html') else: regform = RegistrationForm() postform = PostForm() context = { 'post': postobj, 'regform': regform, 'postform': postform, } return render(request, 'marketplace/image.html', context)
def index(request): latest_post_list = Post.objects.all().order_by('-id') if request.user.is_authenticated(): offers = Offer.objects.filter(user=request.user) offers2 = Offer.objects.filter(post__in=Post.objects.filter( user=request.user).order_by('-id')) else: offers = None offers2 = None search = request.GET.get('query') page = request.GET.get('page') itemnum = request.GET.get('c') if search: latest_post_list = latest_post_list.filter( tags__name__in=[search]).distinct() paginator = Paginator(latest_post_list, 10) if itemnum: paginator = Paginator(latest_post_list, itemnum) if request.method == 'POST': regform = RegistrationForm(request.POST) postform = PostForm(request.POST, request.FILES) if regform.is_valid(): regform.save() return redirect('/') elif request.user.is_authenticated(): if postform.is_valid(): obj = postform.save(commit=False) obj.user = request.user obj.image = postform.cleaned_data['image'] obj.save() postform.save_m2m() return redirect('/') else: postform = PostForm() regform = RegistrationForm() context = { 'latest_post_list': latest_post_list, 'regform': regform, 'postform': postform, 'offers': offers, 'offers2': offers2, } return login(request, context, template_name='marketplace/err.html') else: postform = PostForm() regform = RegistrationForm() try: latest_post_list = paginator.page(page) except PageNotAnInteger: latest_post_list = paginator.page(1) except EmptyPage: latest_post_list = paginator.page(paginator.num_pages) context = { 'latest_post_list': latest_post_list, 'regform': regform, 'postform': postform, 'offers': offers, 'offers2': offers2, } return render(request, 'marketplace/index.html', context)