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})


[((8, 1, 8, 45), 'django.contrib.auth.decorators.login_required', 'login_required', (), '', False, 'from django.contrib.auth.decorators import login_required\n'), ((13, 1, 13, 44), 'django.contrib.auth.decorators.login_required', 'login_required', (), '', False, 'from django.contrib.auth.decorators import login_required\n'), ((28, 1, 28, 44), 'django.contrib.auth.decorators.login_required', 'login_required', (), '', False, 'from django.contrib.auth.decorators import login_required\n'), ((35, 1, 35, 45), 'django.contrib.auth.decorators.login_required', 'login_required', (), '', False, 'from django.contrib.auth.decorators import login_required\n'), ((55, 1, 55, 45), 'django.contrib.auth.decorators.login_required', 'login_required', (), '', False, 'from django.contrib.auth.decorators import login_required\n'), ((71, 1, 71, 45), 'django.contrib.auth.decorators.login_required', 'login_required', (), '', False, 'from django.contrib.auth.decorators import login_required\n'), ((88, 1, 88, 45), 'django.contrib.auth.decorators.login_required', 'login_required', (), '', False, 'from django.contrib.auth.decorators import login_required\n'), ((11, 11, 11, 58), 'django.shortcuts.render', 'render', ({(11, 18, 11, 25): 'request', (11, 27, 11, 38): '"""home.html"""', (11, 39, 11, 57): "{'mylocs': mylocs}"}, {}), "(request, 'home.html', {'mylocs': mylocs})", False, 'from django.shortcuts import render\n'), ((26, 11, 26, 54), 'django.shortcuts.render', 'render', ({(26, 18, 26, 25): 'request', (26, 27, 26, 38): '"""edit.html"""', (26, 40, 26, 53): "{'form': form}"}, {}), "(request, 'edit.html', {'form': form})", False, 'from django.shortcuts import render\n'), ((33, 11, 33, 96), 'django.shortcuts.render', 'render', ({(33, 18, 33, 25): 'request', (33, 27, 33, 41): '"""profile.html"""', (33, 43, 33, 95): "{'my_my_areas': my_my_areas, 'my_profile': my_profile}"}, {}), "(request, 'profile.html', {'my_my_areas': my_my_areas, 'my_profile':\n    my_profile})", False, 'from django.shortcuts import render\n'), ((47, 11, 47, 61), 'django.shortcuts.render', 'render', ({(47, 18, 47, 25): 'request', (47, 27, 47, 44): '"""addmy_area.html"""', (47, 46, 47, 60): "{'form': form}"}, {}), "(request, 'addmy_area.html', {'form': form})", False, 'from django.shortcuts import render\n'), ((53, 11, 53, 95), 'django.shortcuts.render', 'render', ({(53, 18, 53, 25): 'request', (53, 26, 53, 40): '"""details.html"""', (53, 41, 53, 94): "{'myloc': myloc, 'activities': activities, 'posts': posts}"}, {}), "(request, 'details.html', {'myloc': myloc, 'activities': activities,\n    'posts': posts})", False, 'from django.shortcuts import render\n'), ((58, 12, 58, 42), 'django.shortcuts.get_object_or_404', 'get_object_or_404', (), '', False, 'from django.shortcuts import redirect, get_object_or_404\n'), ((69, 11, 69, 86), 'django.shortcuts.render', 'render', ({(69, 18, 69, 25): 'request', (69, 27, 69, 46): '"""new_activity.html"""', (69, 48, 69, 85): "{'form': activity_form, 'myloc': myloc}"}, {}), "(request, 'new_activity.html', {'form': activity_form, 'myloc': myloc})", False, 'from django.shortcuts import render\n'), ((74, 12, 74, 42), 'django.shortcuts.get_object_or_404', 'get_object_or_404', (), '', False, 'from django.shortcuts import redirect, get_object_or_404\n'), ((85, 11, 85, 78), 'django.shortcuts.render', 'render', ({(85, 18, 85, 25): 'request', (85, 27, 85, 42): '"""new_post.html"""', (85, 44, 85, 77): "{'form': post_form, 'myloc': myloc}"}, {}), "(request, 'new_post.html', {'form': post_form, 'myloc': myloc})", False, 'from django.shortcuts import render\n'), ((44, 15, 44, 31), 'django.shortcuts.redirect', 'redirect', ({(44, 24, 44, 30): '"""home"""'}, {}), "('home')", False, 'from django.shortcuts import redirect, get_object_or_404\n'), ((66, 15, 66, 52), 'django.shortcuts.redirect', 'redirect', (), '', False, 'from django.shortcuts import redirect, get_object_or_404\n'), ((82, 15, 82, 52), 'django.shortcuts.redirect', 'redirect', (), '', False, 'from django.shortcuts import redirect, get_object_or_404\n'), ((94, 15, 94, 93), 'django.shortcuts.render', 'render', ({(94, 22, 94, 29): 'request', (94, 31, 94, 44): '"""search.html"""', (94, 45, 94, 92): "{'message': message, 'project': searched_project}"}, {}), "(request, 'search.html', {'message': message, 'project':\n    searched_project})", False, 'from django.shortcuts import render\n'), ((97, 15, 97, 65), 'django.shortcuts.render', 'render', ({(97, 22, 97, 29): 'request', (97, 31, 97, 44): '"""search.html"""', (97, 45, 97, 64): "{'message': message}"}, {}), "(request, 'search.html', {'message': message})", False, 'from django.shortcuts import render\n'), ((23, 19, 23, 40), 'django.shortcuts.redirect', 'redirect', ({(23, 28, 23, 39): '"""myprofile"""'}, {}), "('myprofile')", False, 'from django.shortcuts import redirect, get_object_or_404\n')]