from django.http import HttpResponse,Http404
from django.shortcuts import render,redirect
from datetime import datetime
from blog.models import Article, Contact
from django.shortcuts import render, get_object_or_404
from blog.formulaires import ContactForm, ArticleForm, NouveauContactForm
from crepes_bretonnes.settings import BASE_DIR

def home(request):
    """Exemple de page html, non valide pour que l'exemple soit concis"""
    text="""<h1>Bienvenue sur mon blog!</h1>
            <p>Les crêpes bretonnes ça tue des mouettes en plein vol!</p>"""
    return HttpResponse(text)

# Create your views here.

# def view_article(request, id_article):
#     """Vue qui affiche un article selon son identifiant (ou ID, ici un numero)
#        Son ID est le second parametre de la fonction (pour rappel, le premier parametre est toujours la requete de l'utilisateur)"""
#     text = "Vous avez demande l'article #{2} !".format(id_article)
#     return HttpResponse(text)

def view_article(request, id_article):
    #si l'ID est superieur à 100 nous considerons que l'article n'existe pas
    if int(id_article) > 100:
        raise Http404
    return redirect(view_redirection)

def view_redirection(request):
    return HttpResponse("vous avez été redirigé")

# def list_articles(request, month, year):
#     """Liste des articles d'un mois précis"""
#     text = "Vous avez demandé les articles de {0} {1}".format(month,year)
#     return HttpResponse(text)

def list_articles(request, month, year):
    """Liste des articles d'un mois précis"""
    return redirect("https://www.djangoproject.com")

def date_actuelle(request):
    return render(request,'blog/date.html',{'date':datetime.now()})

def addition(request, nombre1, nombre2):
    total = int(nombre1)+int(nombre2)
    #Retourne nombre1, nombre2 et la somme des deux au tp1
    return render(request,'blog/addition.html',locals())

def affiche_temp(request):
    return render(request,'blog/baseBlog.html',locals())

def accueil(request):
    """Afficher tous les articles du blog"""
    articles = Article.objects.all()
    return render(request,'blog/accueil.html',{'derniers_articles':articles})

def lire(request, id, slug):
    article = get_object_or_404(Article, id=id,slug=slug)
    return render(request,'blog/lire.html',{'article':article})

def contact(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)

        if form.is_valid():
            sujet = form.cleaned_data['sujet']
            message = form.cleaned_data['message']
            envoyeur = form.cleaned_data['envoyeur']
            renvoi = form.cleaned_data['renvoi']

            envoi = True

    else :
            form = ContactForm()

    return render(request,'blog/contact.html',locals())

def art_model(request):
    if request.method == 'POST':
        form = ArticleForm(request.POST)
        if form.is_valid():
            form.save()
            titre = form.cleaned_data['titre']    
            slug = ""
            auteur=""
            contenu=""
            categorie=""
            envoi = True
    else :
        form = ArticleForm()
    return render(request,'blog/enregistrer_article.html',locals())

def nouveau_contact(request):
    sauvegarde = False

    if request.method == "POST":
        form = NouveauContactForm(request.POST, request.FILES)
        if form.is_valid():
            contact = Contact()
            contact.nom = form.cleaned_data["nom"]
            contact.adresse = form.cleaned_data["adresse"]
            contact.photo = form.cleaned_data["photo"]
            contact.save()

            sauvegarde = True
        else :
            form = NouveauContactForm()

    return render(request, 'blog/contact.html', locals())

def voir_contacts(request):
    dossier = BASE_DIR+'/photos/'
    contacts = Contact.objects.all()
    return render(request,'blog/voir_contacts.html',{'contacts':contacts,'dossier':dossier})
977a82e8ef07cfbab5cd150422325ab101b77d1b
<mask token>


def list_articles(request, month, year):
    """Liste des articles d'un mois précis"""
    return redirect('https://www.djangoproject.com')


<mask token>


def addition(request, nombre1, nombre2):
    total = int(nombre1) + int(nombre2)
    return render(request, 'blog/addition.html', locals())


def affiche_temp(request):
    return render(request, 'blog/baseBlog.html', locals())


def accueil(request):
    """Afficher tous les articles du blog"""
    articles = Article.objects.all()
    return render(request, 'blog/accueil.html', {'derniers_articles': articles}
        )


def lire(request, id, slug):
    article = get_object_or_404(Article, id=id, slug=slug)
    return render(request, 'blog/lire.html', {'article': article})


<mask token>


def art_model(request):
    if request.method == 'POST':
        form = ArticleForm(request.POST)
        if form.is_valid():
            form.save()
            titre = form.cleaned_data['titre']
            slug = ''
            auteur = ''
            contenu = ''
            categorie = ''
            envoi = True
    else:
        form = ArticleForm()
    return render(request, 'blog/enregistrer_article.html', locals())


<mask token>


def voir_contacts(request):
    dossier = BASE_DIR + '/photos/'
    contacts = Contact.objects.all()
    return render(request, 'blog/voir_contacts.html', {'contacts': contacts,
        'dossier': dossier})

<mask token>


def view_article(request, id_article):
    if int(id_article) > 100:
        raise Http404
    return redirect(view_redirection)


def view_redirection(request):
    return HttpResponse('vous avez été redirigé')


def list_articles(request, month, year):
    """Liste des articles d'un mois précis"""
    return redirect('https://www.djangoproject.com')


<mask token>


def addition(request, nombre1, nombre2):
    total = int(nombre1) + int(nombre2)
    return render(request, 'blog/addition.html', locals())


def affiche_temp(request):
    return render(request, 'blog/baseBlog.html', locals())


def accueil(request):
    """Afficher tous les articles du blog"""
    articles = Article.objects.all()
    return render(request, 'blog/accueil.html', {'derniers_articles': articles}
        )


def lire(request, id, slug):
    article = get_object_or_404(Article, id=id, slug=slug)
    return render(request, 'blog/lire.html', {'article': article})


<mask token>


def art_model(request):
    if request.method == 'POST':
        form = ArticleForm(request.POST)
        if form.is_valid():
            form.save()
            titre = form.cleaned_data['titre']
            slug = ''
            auteur = ''
            contenu = ''
            categorie = ''
            envoi = True
    else:
        form = ArticleForm()
    return render(request, 'blog/enregistrer_article.html', locals())


def nouveau_contact(request):
    sauvegarde = False
    if request.method == 'POST':
        form = NouveauContactForm(request.POST, request.FILES)
        if form.is_valid():
            contact = Contact()
            contact.nom = form.cleaned_data['nom']
            contact.adresse = form.cleaned_data['adresse']
            contact.photo = form.cleaned_data['photo']
            contact.save()
            sauvegarde = True
        else:
            form = NouveauContactForm()
    return render(request, 'blog/contact.html', locals())


def voir_contacts(request):
    dossier = BASE_DIR + '/photos/'
    contacts = Contact.objects.all()
    return render(request, 'blog/voir_contacts.html', {'contacts': contacts,
        'dossier': dossier})

<mask token>


def view_article(request, id_article):
    if int(id_article) > 100:
        raise Http404
    return redirect(view_redirection)


def view_redirection(request):
    return HttpResponse('vous avez été redirigé')


def list_articles(request, month, year):
    """Liste des articles d'un mois précis"""
    return redirect('https://www.djangoproject.com')


<mask token>


def addition(request, nombre1, nombre2):
    total = int(nombre1) + int(nombre2)
    return render(request, 'blog/addition.html', locals())


def affiche_temp(request):
    return render(request, 'blog/baseBlog.html', locals())


def accueil(request):
    """Afficher tous les articles du blog"""
    articles = Article.objects.all()
    return render(request, 'blog/accueil.html', {'derniers_articles': articles}
        )


def lire(request, id, slug):
    article = get_object_or_404(Article, id=id, slug=slug)
    return render(request, 'blog/lire.html', {'article': article})


def contact(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            sujet = form.cleaned_data['sujet']
            message = form.cleaned_data['message']
            envoyeur = form.cleaned_data['envoyeur']
            renvoi = form.cleaned_data['renvoi']
            envoi = True
    else:
        form = ContactForm()
    return render(request, 'blog/contact.html', locals())


def art_model(request):
    if request.method == 'POST':
        form = ArticleForm(request.POST)
        if form.is_valid():
            form.save()
            titre = form.cleaned_data['titre']
            slug = ''
            auteur = ''
            contenu = ''
            categorie = ''
            envoi = True
    else:
        form = ArticleForm()
    return render(request, 'blog/enregistrer_article.html', locals())


def nouveau_contact(request):
    sauvegarde = False
    if request.method == 'POST':
        form = NouveauContactForm(request.POST, request.FILES)
        if form.is_valid():
            contact = Contact()
            contact.nom = form.cleaned_data['nom']
            contact.adresse = form.cleaned_data['adresse']
            contact.photo = form.cleaned_data['photo']
            contact.save()
            sauvegarde = True
        else:
            form = NouveauContactForm()
    return render(request, 'blog/contact.html', locals())


def voir_contacts(request):
    dossier = BASE_DIR + '/photos/'
    contacts = Contact.objects.all()
    return render(request, 'blog/voir_contacts.html', {'contacts': contacts,
        'dossier': dossier})

<mask token>


def home(request):
    """Exemple de page html, non valide pour que l'exemple soit concis"""
    text = """<h1>Bienvenue sur mon blog!</h1>
            <p>Les crêpes bretonnes ça tue des mouettes en plein vol!</p>"""
    return HttpResponse(text)


def view_article(request, id_article):
    if int(id_article) > 100:
        raise Http404
    return redirect(view_redirection)


def view_redirection(request):
    return HttpResponse('vous avez été redirigé')


def list_articles(request, month, year):
    """Liste des articles d'un mois précis"""
    return redirect('https://www.djangoproject.com')


def date_actuelle(request):
    return render(request, 'blog/date.html', {'date': datetime.now()})


def addition(request, nombre1, nombre2):
    total = int(nombre1) + int(nombre2)
    return render(request, 'blog/addition.html', locals())


def affiche_temp(request):
    return render(request, 'blog/baseBlog.html', locals())


def accueil(request):
    """Afficher tous les articles du blog"""
    articles = Article.objects.all()
    return render(request, 'blog/accueil.html', {'derniers_articles': articles}
        )


def lire(request, id, slug):
    article = get_object_or_404(Article, id=id, slug=slug)
    return render(request, 'blog/lire.html', {'article': article})


def contact(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            sujet = form.cleaned_data['sujet']
            message = form.cleaned_data['message']
            envoyeur = form.cleaned_data['envoyeur']
            renvoi = form.cleaned_data['renvoi']
            envoi = True
    else:
        form = ContactForm()
    return render(request, 'blog/contact.html', locals())


def art_model(request):
    if request.method == 'POST':
        form = ArticleForm(request.POST)
        if form.is_valid():
            form.save()
            titre = form.cleaned_data['titre']
            slug = ''
            auteur = ''
            contenu = ''
            categorie = ''
            envoi = True
    else:
        form = ArticleForm()
    return render(request, 'blog/enregistrer_article.html', locals())


def nouveau_contact(request):
    sauvegarde = False
    if request.method == 'POST':
        form = NouveauContactForm(request.POST, request.FILES)
        if form.is_valid():
            contact = Contact()
            contact.nom = form.cleaned_data['nom']
            contact.adresse = form.cleaned_data['adresse']
            contact.photo = form.cleaned_data['photo']
            contact.save()
            sauvegarde = True
        else:
            form = NouveauContactForm()
    return render(request, 'blog/contact.html', locals())


def voir_contacts(request):
    dossier = BASE_DIR + '/photos/'
    contacts = Contact.objects.all()
    return render(request, 'blog/voir_contacts.html', {'contacts': contacts,
        'dossier': dossier})

from django.http import HttpResponse,Http404
from django.shortcuts import render,redirect
from datetime import datetime
from blog.models import Article, Contact
from django.shortcuts import render, get_object_or_404
from blog.formulaires import ContactForm, ArticleForm, NouveauContactForm
from crepes_bretonnes.settings import BASE_DIR

def home(request):
    """Exemple de page html, non valide pour que l'exemple soit concis"""
    text="""<h1>Bienvenue sur mon blog!</h1>
            <p>Les crêpes bretonnes ça tue des mouettes en plein vol!</p>"""
    return HttpResponse(text)

# Create your views here.

# def view_article(request, id_article):
#     """Vue qui affiche un article selon son identifiant (ou ID, ici un numero)
#        Son ID est le second parametre de la fonction (pour rappel, le premier parametre est toujours la requete de l'utilisateur)"""
#     text = "Vous avez demande l'article #{2} !".format(id_article)
#     return HttpResponse(text)

def view_article(request, id_article):
    #si l'ID est superieur à 100 nous considerons que l'article n'existe pas
    if int(id_article) > 100:
        raise Http404
    return redirect(view_redirection)

def view_redirection(request):
    return HttpResponse("vous avez été redirigé")

# def list_articles(request, month, year):
#     """Liste des articles d'un mois précis"""
#     text = "Vous avez demandé les articles de {0} {1}".format(month,year)
#     return HttpResponse(text)

def list_articles(request, month, year):
    """Liste des articles d'un mois précis"""
    return redirect("https://www.djangoproject.com")

def date_actuelle(request):
    return render(request,'blog/date.html',{'date':datetime.now()})

def addition(request, nombre1, nombre2):
    total = int(nombre1)+int(nombre2)
    #Retourne nombre1, nombre2 et la somme des deux au tp1
    return render(request,'blog/addition.html',locals())

def affiche_temp(request):
    return render(request,'blog/baseBlog.html',locals())

def accueil(request):
    """Afficher tous les articles du blog"""
    articles = Article.objects.all()
    return render(request,'blog/accueil.html',{'derniers_articles':articles})

def lire(request, id, slug):
    article = get_object_or_404(Article, id=id,slug=slug)
    return render(request,'blog/lire.html',{'article':article})

def contact(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)

        if form.is_valid():
            sujet = form.cleaned_data['sujet']
            message = form.cleaned_data['message']
            envoyeur = form.cleaned_data['envoyeur']
            renvoi = form.cleaned_data['renvoi']

            envoi = True

    else :
            form = ContactForm()

    return render(request,'blog/contact.html',locals())

def art_model(request):
    if request.method == 'POST':
        form = ArticleForm(request.POST)
        if form.is_valid():
            form.save()
            titre = form.cleaned_data['titre']    
            slug = ""
            auteur=""
            contenu=""
            categorie=""
            envoi = True
    else :
        form = ArticleForm()
    return render(request,'blog/enregistrer_article.html',locals())

def nouveau_contact(request):
    sauvegarde = False

    if request.method == "POST":
        form = NouveauContactForm(request.POST, request.FILES)
        if form.is_valid():
            contact = Contact()
            contact.nom = form.cleaned_data["nom"]
            contact.adresse = form.cleaned_data["adresse"]
            contact.photo = form.cleaned_data["photo"]
            contact.save()

            sauvegarde = True
        else :
            form = NouveauContactForm()

    return render(request, 'blog/contact.html', locals())

def voir_contacts(request):
    dossier = BASE_DIR+'/photos/'
    contacts = Contact.objects.all()
    return render(request,'blog/voir_contacts.html',{'contacts':contacts,'dossier':dossier})