Advertisement
Guest User

views.py

a guest
Jan 10th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.29 KB | None | 0 0
  1. views.py
  2.  
  3. from django.shortcuts import render, get_object_or_404
  4. from django.utils import timezone
  5. from .models import Ad
  6. from .forms import AdForm
  7. from django.shortcuts import redirect
  8. from .filters import AdFilter
  9. import requests
  10. from django.views import View
  11. from django.views.generic import ListView, DetailView, TemplateView
  12. from django.contrib.auth.decorators import login_required
  13.  
  14.  
  15. def post_list(request):
  16.     filter = AdFilter(request.GET, queryset=Ad.objects.all())
  17.     return render(request, 'Rent/post_list.html', {'filter': filter})
  18.  
  19.  
  20. def post_new(request):
  21.     api_key = "AIzaSyBywMAwBbcFPq-nDOYm_WRGqkCwS53fTVo"
  22.     if request.method == "POST":
  23.         form = AdForm(request.POST)
  24.         if form.is_valid():
  25.             ad = form.save(commit=False)
  26.             ad.author = request.user
  27.             ad.published_date = timezone.now()
  28.             address = ad.localization
  29.             api_response = requests.get(
  30.                 'https://maps.googleapis.com/maps/api/geocode/json?address={0}&key={1}'.format(address, api_key))
  31.             api_response_dict = api_response.json()
  32.             ad.latitude = api_response_dict['results'][0]['geometry']['location']['lat']
  33.             ad.longitude = api_response_dict['results'][0]['geometry']['location']['lng']
  34.             ad.save()
  35.             return redirect('post_detail', pk=ad.pk)
  36.     else:
  37.         form = AdForm()
  38.     return render(request, 'Rent/post_edit.html', {'form': form})
  39.  
  40.  
  41. def post_detail(request, pk):
  42.     ad = get_object_or_404(Ad, pk=pk)
  43.     return render(request, 'Rent/post_detail.html', {'ad': ad})
  44.  
  45.  
  46. def post_edit(request, pk):
  47.     ad = get_object_or_404(Ad, pk=pk)
  48.     api_key = "AIzaSyBywMAwBbcFPq-nDOYm_WRGqkCwS53fTVo"
  49.  
  50.     if request.method == "POST":
  51.         form = AdForm(request.POST, instance=ad)
  52.         if form.is_valid():
  53.             ad = form.save(commit=False)
  54.             ad.author = request.user
  55.             ad.published_date = timezone.now()
  56.             address = ad.localization
  57.             api_response = requests.get(
  58.                 'https://maps.googleapis.com/maps/api/geocode/json?address={0}&key={1}'.format(address, api_key))
  59.             api_response_dict = api_response.json()
  60.             ad.latitude = api_response_dict['results'][0]['geometry']['location']['lat']
  61.             ad.longitude = api_response_dict['results'][0]['geometry']['location']['lng']
  62.             ad.save()
  63.             return redirect('post_detail', pk=ad.pk)
  64.     else:
  65.         form = AdForm(instance=ad)
  66.     return render(request, 'Rent/post_edit.html', {'form': form})
  67.  
  68.  
  69. def localization_change(address, pk):
  70.     ad = get_object_or_404(Ad, pk=pk)
  71.     api_key = "AIzaSyBywMAwBbcFPq-nDOYm_WRGqkCwS53fTVo"
  72.     address = ad.localization
  73.     api_response = requests.get(
  74.         'https://maps.googleapis.com/maps/api/geocode/json?address={0}&key={1}'.format(address, api_key))
  75.     api_response_dict = api_response.json()
  76.     ad.latitude = api_response_dict['results'][0]['geometry']['location']['lat']
  77.     ad.longitude = api_response_dict['results'][0]['geometry']['location']['lng']
  78.  
  79.  
  80. def user_profile(request):
  81.     user = request.user
  82.     user_posts = Ad.objects.filter(author=request.user)
  83.     template = 'Rent/profile.html'
  84.     return render(request, template, {'user_posts': user_posts, 'user': user})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement