def addsubject(request, subtheme_id): try: subtheme = SubTheme.objects.get(id=subtheme_id) if request.method == 'GET': logger.debug("GET addsubject") form = AddSubjectForm() context = {'pagetitle': subtheme.title, 'subtheme': SubThemeDto(subtheme), 'form': form} return myRender(request, 'addsubject.html', context) else: logger.debug('POST addsubject') form = AddSubjectForm(request.POST) if form.is_valid(): try: Subject.objects.get(subtheme=subtheme, title=form.cleaned_data['title']) context = {'pagetitle': subtheme.title, 'subtheme': SubThemeDto(subtheme), 'form': form, 'error': "Le sujet existe déjà"} except ObjectDoesNotExist: subject = Subject.objects.create_subject(subtheme, form.cleaned_data['title']) logger.info(subject.title + " created") return redirect('forum') else: context = {'pagetitle': subtheme.title, 'subtheme': SubThemeDto(subtheme), 'form': form, 'error': "Le titre n'est pas valable"} return myRender(request, 'addsubject.html', context) except ObjectDoesNotExist: return redirect('forum')
def subscribe(request): context = {"pagetitle": "Inscription"} if request.method == "GET": form = SubscribeForm() context['form'] = form return myRender(request, "subscribe.html", context) else: form = SubscribeForm(request.POST) context['form'] = form if form.is_valid(): logger.debug("Subscription form is valid") username = form.cleaned_data['username'] email = form.cleaned_data['email'] password = form.cleaned_data['password'] confirm = form.cleaned_data['confirm_password'] if User.objects.filter(username=username).exists(): context["error"] = "Le nom d'utilisateur existe déjà ! Ré-essayez !" logger.error("Username already present in database") elif User.objects.filter(email=email).exists(): context["error"] = "L'email est déjà utilisé ! Ré-essayez !" logger.error("Email already present in database") elif confirm != password: context["error"] = "Les mots de passe ne correspondent pas ! Ré-essayez !" logger.error("Passwords does not match") else: user = User.objects.create_user(username=username, email=email, password=password) # login(request, user) logger.info("New user created : " + str(user)) return redirect('forumindex') return myRender(request, "subscribe.html", context)
def profilePost(request): user = request.user form = ProfileForm(request.POST, request.FILES) if form.is_valid(): profiles = Profile.objects.filter(user=user) context = { 'pagetitle': 'Profil', 'success': "Votre profil a bien été mis à jour !", 'form': form } if profiles.exists(): profile = profiles[0] profile.pseudo = form.cleaned_data['pseudo'] profile.firstname = form.cleaned_data['firstname'] profile.lastname = form.cleaned_data['lastname'] profile.birthdate = form.cleaned_data['birthdate'] if form.cleaned_data['image'] is not None: form.cleaned_data[ 'image'].name = profile.pseudo + '.' + form.cleaned_data[ 'image'].name.split('.')[1].lower() profile.image = form.cleaned_data['image'] logger.debug('Image uploaded : ' + str(profile.image)) profile.save() logger.debug(profile.pseudo + ' edited his/her profile') else: if Profile.objects.filter( pseudo=form.cleaned_data['pseudo']).exists(): context = { 'pagetitle': 'Première connexion', 'error': "Le pseudo est déjà utilisé", 'form': form } return myRender(request, "profile.html", context) else: profile = Profile.objects.create_profile( user, form.cleaned_data['pseudo'], form.cleaned_data['firstname'], form.cleaned_data['lastname'], form.cleaned_data['birthdate'], form.cleaned_data['image']) if profile.image: context['imageurl'] = '/' + profile.image.url else: context['imageurl'] = '/forum/profiles/Profil.jpg' return myRender(request, "profile.html", context) else: return redirect("profile")
def addsubject(request, subtheme_id): try: subtheme = SubTheme.objects.get(id=subtheme_id) if request.method == 'GET': logger.debug("GET addsubject") form = AddSubjectForm() context = { 'pagetitle': subtheme.title, 'subtheme': SubThemeDto(subtheme), 'form': form } return myRender(request, 'addsubject.html', context) else: logger.debug('POST addsubject') form = AddSubjectForm(request.POST) if form.is_valid(): try: Subject.objects.get(subtheme=subtheme, title=form.cleaned_data['title']) context = { 'pagetitle': subtheme.title, 'subtheme': SubThemeDto(subtheme), 'form': form, 'error': "Le sujet existe déjà" } except ObjectDoesNotExist: subject = Subject.objects.create_subject( subtheme, form.cleaned_data['title']) logger.info(subject.title + " created") return redirect('forum') else: context = { 'pagetitle': subtheme.title, 'subtheme': SubThemeDto(subtheme), 'form': form, 'error': "Le titre n'est pas valable" } return myRender(request, 'addsubject.html', context) except ObjectDoesNotExist: return redirect('forum')
def error_404(request): context = { 'pagetitle': 'Page non trouvée', 'referer': request.META.HTTP_REFERER, 'message': "La page que vous cherchez n'éxiste pas !" } return myRender(request, 'error.html', context)
def forum(request): context = {'pagetitle': "Forum"} themes = Theme.objects.all() theme_dtos = [] for theme in themes: subthemes = SubTheme.objects.filter(theme=theme) subtheme_dtos = [] for subtheme in subthemes: subjects = Subject.objects.filter(subtheme=subtheme) subjects_dtos = [SubjectDto(subject) for subject in subjects] if request.user.is_authenticated(): profile = Profile.objects.get(user=request.user) last_login_datetime = profile.previous_login for subject in subjects_dtos: unread_posts = UnreadPost.objects.filter(profile=profile, subject=subject.subject) subject.newpost = len(unread_posts) subtheme_dtos.append(SubThemeDto(subtheme, subjects_dtos)) theme_dtos.append(ThemeDto(theme, subtheme_dtos)) context['themes'] = theme_dtos return myRender(request, 'forum.html', context)
def profileGet(request): profiles = Profile.objects.filter(user=request.user) imageurl = 'forum/profiles/Profil.jpg' if profiles: profile = profiles[0] form = ProfileForm( initial={ 'pseudo': profile.pseudo, 'firstname': profile.firstname, 'lastname': profile.lastname, 'birthdate': profile.birthdate }) imageurl = profile.image.url if profile.image else 'forum/profiles/Profil.jpg' context = {'pagetitle': 'Profil'} else: form = ProfileForm() context = {'pagetitle': 'Première connexion', 'noprofile': True} context['imageurl'] = imageurl context['form'] = form context['background_color'] = 'white' return myRender(request, "profile.html", context)
def forum(request): context = {'pagetitle': "Forum"} themes = Theme.objects.all() theme_dtos = [] for theme in themes: subthemes = SubTheme.objects.filter(theme=theme) subtheme_dtos = [] for subtheme in subthemes: subjects = Subject.objects.filter(subtheme=subtheme) subjects_dtos = [SubjectDto(subject) for subject in subjects] if request.user.is_authenticated(): profile = Profile.objects.get(user=request.user) last_login_datetime = profile.previous_login for subject in subjects_dtos: unread_posts = UnreadPost.objects.filter( profile=profile, subject=subject.subject) subject.newpost = len(unread_posts) subtheme_dtos.append(SubThemeDto(subtheme, subjects_dtos)) theme_dtos.append(ThemeDto(theme, subtheme_dtos)) context['themes'] = theme_dtos return myRender(request, 'forum.html', context)
def profile_infos(request, profile_id): try: profile = ProfileDto(Profile.objects.get(id=profile_id)) context = {'pagetitle': 'Informations de ' + profile.pseudo, 'profile': profile} return myRender(request, 'profileinfos.html', context) except ObjectDoesNotExist: return redirect('forum')
def subject(request, subject_id): try: subject = Subject.objects.get(id=subject_id) context = {'pagetitle': subject.title, 'subject': subject} if request.method == 'POST': form = AddPostForm(request.POST) if form.is_valid(): profile = Profile.objects.get(user=request.user) post_id = request.POST['postId'] title = form.cleaned_data['title'] if len(title) == 0: title = subject.title content = form.cleaned_data['content'] if post_id == -1: post = Post.objects.create_post(subject, profile, title, content) else: post = Post.objects.filter(id=post_id, profile=profile)[0] post.title = title post.content = content post.save() logger.info(post.title + ' created') form = AddPostForm() else: error_list = [] for e in dict(form.errors): error_list.append('Le champ ' + form[e].label + ' a une erreur : ' + form.errors[e].as_text()[2:]) context['error'] = '\n'.join(error_list) else: if request.user.is_authenticated(): profile = Profile.objects.get(user=request.user) UnreadPost.objects.filter(profile=profile, subject=subject).delete() logger.debug('UnreadPosts of ' + subject.title + ' deleted for ' + profile.pseudo) form = AddPostForm() posts = Post.objects.filter(subject=subject) posts_dtos = [PostDto(post) for post in posts] context['posts'] = posts_dtos context['form'] = form return myRender(request, "subject.html", context) except ObjectDoesNotExist: return redirect('forum')
def indexError(request): context = {'pagetitle': 'Accueil', 'error': "Mauvais nom d'utilisateur ou mauvais mot de passe"} if request.user.is_authenticated(): profiles = Profile.objects.filter(user=request.user) if profiles.exists(): context['profile'] = profiles[0] else: logger.warn("Pas de profil") return myRender(request, 'index.html', context)
def profilePost(request): user = request.user form = ProfileForm(request.POST, request.FILES) if form.is_valid(): profiles = Profile.objects.filter(user=user) context = {'pagetitle': 'Profil', 'success': "Votre profil a bien été mis à jour !", 'form': form} if profiles.exists(): profile = profiles[0] profile.pseudo = form.cleaned_data['pseudo'] profile.firstname = form.cleaned_data['firstname'] profile.lastname = form.cleaned_data['lastname'] profile.birthdate = form.cleaned_data['birthdate'] if form.cleaned_data['image'] is not None: form.cleaned_data['image'].name = profile.pseudo + '.' + form.cleaned_data['image'].name.split('.')[1].lower() profile.image = form.cleaned_data['image'] logger.debug('Image uploaded : ' + str(profile.image)) profile.save() logger.debug(profile.pseudo + ' edited his/her profile') else: if Profile.objects.filter(pseudo=form.cleaned_data['pseudo']).exists(): context = {'pagetitle': 'Première connexion', 'error': "Le pseudo est déjà utilisé", 'form': form} return myRender(request, "profile.html", context) else: profile = Profile.objects.create_profile(user, form.cleaned_data['pseudo'], form.cleaned_data['firstname'], form.cleaned_data['lastname'], form.cleaned_data['birthdate'], form.cleaned_data['image']) if profile.image: context['imageurl'] = '/' + profile.image.url else: context['imageurl'] = '/forum/profiles/Profil.jpg' return myRender(request, "profile.html", context) else: return redirect("profile")
def profile_infos(request, profile_id): try: profile = ProfileDto(Profile.objects.get(id=profile_id)) context = { 'pagetitle': 'Informations de ' + profile.pseudo, 'profile': profile } return myRender(request, 'profileinfos.html', context) except ObjectDoesNotExist: return redirect('forum')
def indexError(request): context = { 'pagetitle': 'Accueil', 'error': "Mauvais nom d'utilisateur ou mauvais mot de passe" } if request.user.is_authenticated(): profiles = Profile.objects.filter(user=request.user) if profiles.exists(): context['profile'] = profiles[0] else: logger.warn("Pas de profil") return myRender(request, 'index.html', context)
def subscribe(request): context = {"pagetitle": "Inscription"} if request.method == "GET": form = SubscribeForm() context['form'] = form return myRender(request, "subscribe.html", context) else: form = SubscribeForm(request.POST) context['form'] = form if form.is_valid(): logger.debug("Subscription form is valid") username = form.cleaned_data['username'] email = form.cleaned_data['email'] password = form.cleaned_data['password'] confirm = form.cleaned_data['confirm_password'] if User.objects.filter(username=username).exists(): context[ "error"] = "Le nom d'utilisateur existe déjà ! Ré-essayez !" logger.error("Username already present in database") elif User.objects.filter(email=email).exists(): context["error"] = "L'email est déjà utilisé ! Ré-essayez !" logger.error("Email already present in database") elif confirm != password: context[ "error"] = "Les mots de passe ne correspondent pas ! Ré-essayez !" logger.error("Passwords does not match") else: user = User.objects.create_user(username=username, email=email, password=password) # login(request, user) logger.info("New user created : " + str(user)) return redirect('forumindex') return myRender(request, "subscribe.html", context)
def profileGet(request): profiles = Profile.objects.filter(user=request.user) imageurl = 'forum/profiles/Profil.jpg' if profiles: profile = profiles[0] form = ProfileForm(initial={'pseudo' : profile.pseudo, 'firstname' : profile.firstname, 'lastname' : profile.lastname, 'birthdate' : profile.birthdate}) imageurl = profile.image.url if profile.image else 'forum/profiles/Profil.jpg' context = {'pagetitle': 'Profil'} else: form = ProfileForm() context = {'pagetitle': 'Première connexion', 'noprofile': True} context['imageurl'] = imageurl context['form'] = form context['background_color'] = 'white' return myRender(request, "profile.html", context)
def search(request): search = request.GET.get('search') context = {'pagetitle': 'Résultats', 'search': search} if request.user.is_authenticated(): profiles = Profile.objects.filter(Q(pseudo__contains=search) | Q(firstname__contains=search) | Q(lastname__contains=search)) context['profiles'] = profiles themes = Theme.objects.filter(title__icontains=search) context['themes'] = themes subthemes = SubTheme.objects.filter(title__icontains=search) context['subthemes'] = subthemes subjects = Subject.objects.filter(title__icontains=search) context['subjects'] = subjects posts = Post.objects.filter(Q(title__icontains=search) | Q(content__icontains=search)) context['posts'] = posts return myRender(request, 'search.html', context)
def search(request): search = request.GET.get('search') context = {'pagetitle': 'Résultats', 'search': search} if request.user.is_authenticated(): profiles = Profile.objects.filter( Q(pseudo__contains=search) | Q(firstname__contains=search) | Q(lastname__contains=search)) context['profiles'] = profiles themes = Theme.objects.filter(title__icontains=search) context['themes'] = themes subthemes = SubTheme.objects.filter(title__icontains=search) context['subthemes'] = subthemes subjects = Subject.objects.filter(title__icontains=search) context['subjects'] = subjects posts = Post.objects.filter( Q(title__icontains=search) | Q(content__icontains=search)) context['posts'] = posts return myRender(request, 'search.html', context)
def error_404(request): context = {'pagetitle': 'Page non trouvée', 'referer': request.META.HTTP_REFERER, 'message': "La page que vous cherchez n'éxiste pas !"} return myRender(request, 'error.html', context)
def error_500(request): context = { 'pagetitle': 'Erreur serveur', 'message': "Une erreur côté serveur est apparue !" } return myRender(request, 'error.html', context)
def error_500(request): context = {'pagetitle': 'Erreur serveur', 'message': "Une erreur côté serveur est apparue !"} return myRender(request, 'error.html', context)