from django.shortcuts import render
from .forms import *
from django.shortcuts import redirect,get_object_or_404
from django.contrib.auth.decorators import login_required
from . models import *
from django.views import generic

@login_required(login_url='/accounts/login/')
def home(request):
    mylocs = Myloc.objects.all()
    return render(request, 'home.html',{"mylocs":mylocs,})

@login_required(login_url='accounts/login/')
def add_profile(request):
    current_user = request.user
    profile = Profile.objects.filter(id = current_user.id)
    if request.method == 'POST':
        form = NewProfileForm(request.POST, request.FILES)
        if form.is_valid():
            caption = form.save(commit=False)
            caption.user = current_user
            caption.save()
            return redirect('myprofile')
    else:
        form = NewProfileForm()
    return render(request, 'edit.html', {"form":form})    

@login_required(login_url='accounts/login/')
def my_profile(request):
    current_user = request.user
    my_my_area = Myloc.objects.filter(user = current_user)
    my_profile = Profile.objects.filter(user = current_user).first
    return render(request, 'profile.html', {"my_my_areas":my_my_areas, "my_profile":my_profile})

@login_required(login_url='/accounts/login/')
def addmy_area(request):
    current_user = request.user
    if request.method == 'POST':
        form = MylocForm(request.POST, request.FILES)
        if form.is_valid():
            image = form.save(commit=False)
            image.user = current_user
            image.save()
        return redirect('home')
    else:
        form = MylocForm()
    return render(request, 'addmy_area.html', {"form": form}) 
    
def myloc_details(request,myloc_id):
    activities=Activity.objects.filter(myloc=myloc_id)
    posts=Post.objects.filter(myloc=myloc_id)
    myloc=Myloc.objects.get(pk=myloc_id)
    return render(request,'details.html',{'myloc':myloc,'activities':activities,'posts':posts})

@login_required(login_url="/accounts/login/")
def new_activity(request,pk):
    current_user = request.user
    myloc = get_object_or_404(Myloc,pk=pk)
    if request.method == 'POST':
        activity_form = NewActivityForm(request.POST, request.FILES)
        if activity_form.is_valid():
            activity = activity_form.save(commit=False)
            activity.user = current_user
            activity.myloc=myloc
            activity.save()
        return redirect('detail', myloc_id=myloc.id)
    else:
        activity_form = NewActivityForm()
    return render(request, 'new_activity.html', {"form": activity_form,'myloc':myloc})

@login_required(login_url="/accounts/login/")
def new_post(request,pk):
    current_user = request.user
    myloc = get_object_or_404(Myloc,pk=pk)
    if request.method == 'POST':
        post_form = NewPostForm(request.POST, request.FILES)
        if post_form.is_valid():
            post = post_form.save(commit=False)
            post.user = current_user
            post.myloc=myloc
            post.save()
        return redirect('detail', myloc_id=myloc.id)
    else:
        post_form = NewPostForm()
    return render(request, 'new_post.html', {"form": post_form,'myloc':myloc})


@login_required(login_url='/accounts/login/')
def search_project(request):
    if 'project_name' in request.GET and request.GET["project_name"]:
        search_term = request.GET.get("project_name")
        searched_project = Myloc.search_by_location(search_term)
        message = f"{search_term}"
        return render(request, "search.html",{"message":message,"project": searched_project})
    else:
        message = "No search history"
        return render(request, 'search.html',{"message":message})


[((229, 273), 'django.contrib.auth.decorators.login_required', 'login_required', ([], {'login_url': '"""/accounts/login/"""'}), "(login_url='/accounts/login/')\n", (243, 273), False, 'from django.contrib.auth.decorators import login_required\n'), ((387, 430), 'django.contrib.auth.decorators.login_required', 'login_required', ([], {'login_url': '"""accounts/login/"""'}), "(login_url='accounts/login/')\n", (401, 430), False, 'from django.contrib.auth.decorators import login_required\n'), ((925, 968), 'django.contrib.auth.decorators.login_required', 'login_required', ([], {'login_url': '"""accounts/login/"""'}), "(login_url='accounts/login/')\n", (939, 968), False, 'from django.contrib.auth.decorators import login_required\n'), ((1251, 1295), 'django.contrib.auth.decorators.login_required', 'login_required', ([], {'login_url': '"""/accounts/login/"""'}), "(login_url='/accounts/login/')\n", (1265, 1295), False, 'from django.contrib.auth.decorators import login_required\n'), ((1989, 2033), 'django.contrib.auth.decorators.login_required', 'login_required', ([], {'login_url': '"""/accounts/login/"""'}), "(login_url='/accounts/login/')\n", (2003, 2033), False, 'from django.contrib.auth.decorators import login_required\n'), ((2630, 2674), 'django.contrib.auth.decorators.login_required', 'login_required', ([], {'login_url': '"""/accounts/login/"""'}), "(login_url='/accounts/login/')\n", (2644, 2674), False, 'from django.contrib.auth.decorators import login_required\n'), ((3220, 3264), 'django.contrib.auth.decorators.login_required', 'login_required', ([], {'login_url': '"""/accounts/login/"""'}), "(login_url='/accounts/login/')\n", (3234, 3264), False, 'from django.contrib.auth.decorators import login_required\n'), ((337, 385), 'django.shortcuts.render', 'render', (['request', '"""home.html"""', "{'mylocs': mylocs}"], {}), "(request, 'home.html', {'mylocs': mylocs})\n", (343, 385), False, 'from django.shortcuts import render\n'), ((875, 919), 'django.shortcuts.render', 'render', (['request', '"""edit.html"""', "{'form': form}"], {}), "(request, 'edit.html', {'form': form})\n", (881, 919), False, 'from django.shortcuts import render\n'), ((1163, 1254), 'django.shortcuts.render', 'render', (['request', '"""profile.html"""', "{'my_my_areas': my_my_areas, 'my_profile': my_profile}"], {}), "(request, 'profile.html', {'my_my_areas': my_my_areas, 'my_profile':\n    my_profile})\n", (1169, 1254), False, 'from django.shortcuts import render\n'), ((1655, 1705), 'django.shortcuts.render', 'render', (['request', '"""addmy_area.html"""', "{'form': form}"], {}), "(request, 'addmy_area.html', {'form': form})\n", (1661, 1705), False, 'from django.shortcuts import render\n'), ((1902, 1997), 'django.shortcuts.render', 'render', (['request', '"""details.html"""', "{'myloc': myloc, 'activities': activities, 'posts': posts}"], {}), "(request, 'details.html', {'myloc': myloc, 'activities': activities,\n    'posts': posts})\n", (1908, 1997), False, 'from django.shortcuts import render\n'), ((2108, 2139), 'django.shortcuts.get_object_or_404', 'get_object_or_404', (['Myloc'], {'pk': 'pk'}), '(Myloc, pk=pk)\n', (2125, 2139), False, 'from django.shortcuts import redirect, get_object_or_404\n'), ((2552, 2629), 'django.shortcuts.render', 'render', (['request', '"""new_activity.html"""', "{'form': activity_form, 'myloc': myloc}"], {}), "(request, 'new_activity.html', {'form': activity_form, 'myloc': myloc})\n", (2558, 2629), False, 'from django.shortcuts import render\n'), ((2745, 2776), 'django.shortcuts.get_object_or_404', 'get_object_or_404', (['Myloc'], {'pk': 'pk'}), '(Myloc, pk=pk)\n', (2762, 2776), False, 'from django.shortcuts import redirect, get_object_or_404\n'), ((3149, 3218), 'django.shortcuts.render', 'render', (['request', '"""new_post.html"""', "{'form': post_form, 'myloc': myloc}"], {}), "(request, 'new_post.html', {'form': post_form, 'myloc': myloc})\n", (3155, 3218), False, 'from django.shortcuts import render\n'), ((1590, 1606), 'django.shortcuts.redirect', 'redirect', (['"""home"""'], {}), "('home')\n", (1598, 1606), False, 'from django.shortcuts import redirect, get_object_or_404\n'), ((2451, 2488), 'django.shortcuts.redirect', 'redirect', (['"""detail"""'], {'myloc_id': 'myloc.id'}), "('detail', myloc_id=myloc.id)\n", (2459, 2488), False, 'from django.shortcuts import redirect, get_object_or_404\n'), ((3056, 3093), 'django.shortcuts.redirect', 'redirect', (['"""detail"""'], {'myloc_id': 'myloc.id'}), "('detail', myloc_id=myloc.id)\n", (3064, 3093), False, 'from django.shortcuts import redirect, get_object_or_404\n'), ((3533, 3618), 'django.shortcuts.render', 'render', (['request', '"""search.html"""', "{'message': message, 'project': searched_project}"], {}), "(request, 'search.html', {'message': message, 'project':\n    searched_project})\n", (3539, 3618), False, 'from django.shortcuts import render\n'), ((3675, 3727), 'django.shortcuts.render', 'render', (['request', '"""search.html"""', "{'message': message}"], {}), "(request, 'search.html', {'message': message})\n", (3681, 3727), False, 'from django.shortcuts import render\n'), ((800, 821), 'django.shortcuts.redirect', 'redirect', (['"""myprofile"""'], {}), "('myprofile')\n", (808, 821), False, 'from django.shortcuts import redirect, get_object_or_404\n')]