def get(self, request, *args, **kwargs): programs = Post.objects.filter( featured=True, categories__title__exact="program").order_by('-timestamp')[0:3] projects = Post.objects.filter( featured=True, categories__title__exact="project").order_by('-timestamp')[0:3] sliderview = Post.objects.filter( slider=True).order_by('-timestamp')[0:3] campus = Post.objects.filter( featured=True, categories__title__exact="campus").order_by('-timestamp')[0:3] form = EmailSignupForm() context = { 'programs': programs, 'projects': projects, 'sliderview': sliderview, 'campus': campus, 'latest': latest, 'now': now, 'most': most_view, 'form': form } return render(request, 'index.html', context)
def all_gifts(request): gifts = Gift.objects.all() subscribe_form = EmailSignupForm() return render(request, 'gifts.html', { 'gifts': gifts, 'subscribe_form': subscribe_form })
def view_profile(request): donor = Donor.objects.get(user_id=request.user.id) subscribe_form = EmailSignupForm() return render(request, 'profile.html', { "donor": donor, 'subscribe_form': subscribe_form })
def registration(request): if request.user.is_authenticated: messages.error(request, "You are already logged in") return redirect(reverse('index')) subscribe_form = EmailSignupForm() if request.method == "POST": registration_form = UserRegistrationForm(request.POST) if registration_form.is_valid(): registration_form.save() user = auth.authenticate(username=request.POST['username'], password=request.POST['password1']) messages.success(request, "Your account has been created") if user: auth.login(user=user, request=request) return redirect(reverse('index')) else: messages.error(request, "Something went wrong, please try again") else: registration_form = UserRegistrationForm() return render(request, 'registration.html', { "registration_form": registration_form, 'subscribe_form': subscribe_form })
def login(request): if request.user.is_authenticated: return redirect(reverse('index')) subscribe_form = EmailSignupForm() if request.method == "POST": login_form = UserLoginForm(request.POST) if login_form.is_valid(): user = auth.authenticate(username=request.POST['username'], password=request.POST['password']) if user: auth.login(user=user, request=request) messages.success(request, "Successfully logged in!") return redirect(reverse('index')) else: login_form = UserLoginForm() messages.error(request, "Username or password is incorrect") else: login_form = UserLoginForm() return render(request, 'login.html', { 'login_form': login_form, 'subscribe_form': subscribe_form })
def edit_profile(request): subscribe_form = EmailSignupForm() if request.method == "POST": edit_profile_form = EditProfileForm(request.POST, instance=request.user) edit_donor_form = DonorForm(request.POST, instance=request.user.profile) if edit_profile_form.is_valid() and edit_donor_form.is_valid(): edit_profile_form.save() edit_donor_form.save() messages.success(request, "Account updated") return redirect('view_profile') else: messages.error(request, ('Please correct the error')) else: edit_profile_form = EditProfileForm(instance=request.user) edit_donor_form = DonorForm(instance=request.user.profile) return render( request, 'edit-profile.html', { "edit_profile_form": edit_profile_form, 'edit_donor_form': edit_donor_form, 'subscribe_form': subscribe_form })
def checkout(request): subscribe_form = EmailSignupForm() if request.method == "POST": order_form = OrderForm(request.POST) payment_form = MakePaymentForm(request.POST) if order_form.is_valid() and payment_form.is_valid(): order = order_form.save(commit=False) order.date = timezone.now() order.save() cart = request.session.get('cart', {}) total = 0 for id, quantity in cart.items(): gift = get_object_or_404(Gift, pk=id) total += quantity * gift.price order_line_item = OrderLineItem(order=order, gift=gift, quantity=quantity) order_line_item.save() try: customer = stripe.Charge.create( amount=int(total * 100), currency="GBP", description=request.user.email, card=payment_form.cleaned_data['stripe_id']) except stripe.error.CardError: messages.error(request, "Your bank declined this card!") if customer.paid: messages.success(request, "You have successfully paid") request.session['cart'] = {} return redirect(reverse('gifts')) else: messages.error(request, "Something went wrong, please try again later") else: print(payment_form.errors) messages.error(request, "Something went wrong with that card!") else: payment_form = MakePaymentForm() order_form = OrderForm() return render( request, "checkout.html", { "order_form": order_form, "payment_form": payment_form, "publishable": settings.STRIPE_PUBLISHABLE, 'subscribe_form': subscribe_form })
def donate(request): subscribe_form = EmailSignupForm() if request.method == "POST": donate_form = DonateForm(request.POST) payment_form = MakePaymentForm(request.POST) if donate_form.is_valid() and payment_form.is_valid(): donate = donate_form.save(commit=False) donate.save() try: customer = stripe.Charge.create( amount=int(donate.total * 100), currency="GBP", description=request.user.email, card=payment_form.cleaned_data['stripe_id']) except stripe.error.CardError: messages.error(request, "Your bank declined this card!") if customer.paid: messages.success(request, "You successfully donated!") return redirect(reverse('index')) else: messages.error(request, "Something went wrong, please try again later") else: print(payment_form.errors) messages.error(request, "Something went wrong with that card!") else: payment_form = MakePaymentForm() donate_form = DonateForm() return render( request, "donate.html", { "donate_form": donate_form, "payment_form": payment_form, "publishable": settings.STRIPE_PUBLISHABLE, 'subscribe_form': subscribe_form })
class ProjectListView(ListView): form = EmailSignupForm() queryset = Post.objects.filter( categories__title__exact="project").order_by('timestamp') template_name = 'program.html' context_object_name = 'queryset' paginate_by = 3 def get_context_data(self, **kwargs): #category_count = get_category_count() #posts=Post.objects.filter(categories__title__exact = "program").order_by('timestamp') #most_recent = Post.objects.order_by('-timestamp')[:3] context = super().get_context_data(**kwargs) #context['most_recent'] = most_recent context['page_request_var'] = "page" #context['category_count'] = category_count context['form'] = self.form # context['posts']=posts header = "PROJECTS" context['header'] = header context['now'] = now context['latest'] = latest context['most'] = most_view return context
from django.shortcuts import render, get_object_or_404, redirect, reverse from django.http import HttpResponse, HttpResponseRedirect, Http404 from django.template.loader import get_template from .models import Post, Author, PostView, Team, TeamView from django.template import Context, Template, RequestContext from subscribe.forms import EmailSignupForm from subscribe.models import Signup from .forms import CommentForm, PostForm from django.utils.decorators import method_decorator from django.views.decorators.csrf import csrf_exempt import datetime now = datetime.datetime.now() latest = Post.objects.order_by('-timestamp')[0:9] form = EmailSignupForm() most_view = sorted(Post.objects.all(), key=lambda t: t.view_count, reverse=True)[0:4] def get_author(user): author = Author.objects.filter(user=user) if author.exists(): return author[0] return None class IndexView(View): # form = EmailSignupForm()
def view_cart(request): subscribe_form = EmailSignupForm() return render(request, "cart.html", {'subscribe_form': subscribe_form})