def get_titles(request): try: if request.method == 'GET' and request.is_ajax(): posts = _get_posts(0, 10) return HttpResponse(posts, mimetype="application/json") except Exception as e: ajax_log("blog.mobile.get_titles: %s" % e) return HttpResponseBadRequest('')
def get_next(request): try: if request.method == 'GET' and request.is_ajax(): count = int(request.GET['count']) posts = _get_posts(count, count+10) return HttpResponse(posts, mimetype="application/json") except Exception as e: ajax_log("blog.mobile.get_next: %s" % e) return HttpResponseBadRequest('')
def get_by_categ(request): try: if request.method == 'GET' and request.is_ajax(): categ = Categorie.objects.get(Nom=request.GET['categ']) posts = [] [ posts.append(_convert_to_dict(post)) for post in Post.objects.filter(Publish=True, Categorie=categ).only('CreationDateTime', 'Title', 'Shortcut', 'Content') ] return HttpResponse(json.dumps({ 'titles' : posts, 'categ': categ.Nom }), mimetype="application/json") except Exception as e: ajax_log("blog.mobile.get_by_tag: %s " % e) return HttpResponseBadRequest('')
def get_by_tag(request): try: if request.method == 'GET' and request.is_ajax(): tag = Tag.objects.get(Nom=request.GET['tag']) posts = [] [ posts.append(_convert_to_dict(post)) for post in Post.objects.filter(Publish=True, Tags=tag) ] return HttpResponse(json.dumps({ 'titles' : posts, 'tag': tag.Nom }), mimetype="application/json") except Exception as e: ajax_log("blog.mobile.get_by_tag: %s " % e) return HttpResponseBadRequest('')
def get_page(request): try: if request.method == 'GET' and request.is_ajax(): shortcut = request.GET['shortcut'] post = Post.objects.get(Shortcut=shortcut, Publish=True) jpost = { 'Title' : post.Title, 'Content': post.Content, 'CreationDateTime': date(post.CreationDateTime, "j F Y") } return HttpResponse(json.dumps(jpost, ensure_ascii=False), mimetype="application/json") except Exception as e: ajax_log("blog.mobile.get_page: %s" % e) return HttpResponseBadRequest('')
def getnext(request): try: articles = int(request.GET.get("articles")) pref = Preference.objects.get(id=1) context = RequestContext(request,{ 'blog_last' : { 'get_last_posts' : Post.objects.filter(Publish=True)[articles:articles+6], 'showFullArticle' : pref.ShowFullArticle, 'articleSampleLength' : pref.ArticleSampleLength } }) return HttpResponse(loader.get_template('blog/nextpost.html').render(context)) except Exception as e: ajax_log("%s" % e) return HttpResponseBadRequest("Bad request")
def get_history(request): try: if request.method == 'GET' and request.is_ajax(): year,month = request.GET['year'].split('-') posts = [] for post in Post.objects.filter(CreationDateTime__year=year, CreationDateTime__month=month, Publish=True).only('CreationDateTime', 'Title', 'Shortcut', 'Content'): posts.append(_convert_to_dict(post)) result = { 'titles' : posts, 'month' : MONTH[int(month)], 'year' : year } return HttpResponse(json.dumps(result, ensure_ascii=False), mimetype="application/json") except Exception as e: ajax_log("blog.mobile.get_history: %s" % e) return HttpResponseBadRequest('')
def dynamic_comment(request): """ Add a comment in ajax way """ try: if request.method == 'POST' and request.is_ajax(): post = Post.objects.get(pk=request.POST['article_id']) if request.user.is_authenticated(): name = request.user.username email = request.user.email else: name = strip_tags(request.POST['name'].strip()) email = strip_tags(request.POST['email'].strip()) request.session['email'] = email request.session['nom'] = name body = request.POST['body'] comment = Comment( Email = email, UserName = name, Comment = body, IPAddress = request.META['REMOTE_ADDR'], post = post, Show=True ) comment.save() try: notification_send(settings.BLOG_CONFIG.EmailTemplates.newcomment) except: pass # Create the new capatcha captcha = Capatcha(request) request.esssion['capatcha'] = captcha data = { 'comment' : render_to_string(settings.BLOG_CONFIG.Templates.comment, RequestContext(request, { 'blog_post' : post, 'comment': comment})), 'captcha': captcha.path } return HttpResponse(json.dumps(data, ensure_asciii=False), mimetype="application/json") except Exception as e: ajax_log("blog.views.dynamic_comment: %s " % e) return HttpResponseBadRequest('')
def render(*args, **kwargs): try: meta = args[0].META if is_excludable(args[0]) == False: view = View() view.last_view = datetime.datetime.now() view.ip = meta.get('REMOTE_ADDR', '') view.browser = meta.get('HTTP_USER_AGENT', '') view.internal_url = meta.get('PATH_INFO', '') view.lang = meta.get('HTTP_ACCEPT_LANGUAGE', 'Not set') try: view.session_key = args[0].session.session_key except Exception as e: ajax_log("app_view.view_count (get session key) : %s " % e) view.session_key = "error" view.save() except Exception as e: # Google which use python urllib make this routine crash. Ignore it. ajax_log("app_view.view_count : %s" % e) return f(*args, **kwargs)
def search(request): try: if request.method == 'GET' and request.is_ajax(): search_str = request.GET['keyword'] if len(search_str) >= 3: keywords = search_str.split(' ') query = Q() for key in keywords: query |= Q(Content__icontains=key) posts = [] for post in Post.objects.filter(query & Q(Publish=True)).only('CreationDateTime', 'Title', 'Shortcut', 'Content'): posts.append(_convert_to_dict(post)) return HttpResponse(json.dumps({ 'titles' : posts, 'keywords' : keywords}), mimetype="application/json") except Exception as e: ajax_log("blog.mobile.search : %s" %e) return HttpResponseBadRequest('')
def comment(request): """ Add a comment """ try: if request.method == 'POST': context = RequestContext(request) post = request.POST article_id = post['blog'] context['blog_post'] = Post.objects.get(id=article_id) error = False if request.user.is_authenticated(): name = request.user.username email = request.user.email body = post['body'] else: name = strip_tags(post['name'].strip()) email = strip_tags(post['email'].strip()) body = strip_tags(post['body'].strip()) error = False if not len(name): context['name_err'] = _("Thanks to leave name") error = True if not len(email): context['email_err'] = _("Your email address is empty") error = True else: if not email_re.match(email): context['email_err'] = _('Your email address is not a valid one') error = True if not len(body): context['body_err'] = _("The message body is empty") error = True if not request.session['capatcha'].isValid(post['capatcha']): error = True context['capat_err'] = _("Wrong secure code") if error: context['name'] = name context['email'] = email context['body'] = body return render_to_response(settings.BLOG_CONFIG.Templates.post, context) else: request.session['email'] = email request.session['nom'] = name Comment.objects.create( Email = email, UserName = name, Comment = body.replace("\n", "<br/>"), IPAddress = request.META['REMOTE_ADDR'], post = context['blog_post'], CreationDateTime = datetime.datetime.now(), Show = Preference.objects.immediate_publishing() ) try: notification_send(settings.BLOG_CONFIG.EmailTemplates.newcomment) except: pass messages.add_message(request, messages.INFO, "Votre commentaire a été posté avec succés") return HttpResponseRedirect(reverse('show_article', args=(context['blog_post'].Shortcut,))) except Exception, e: ajax_log("Erreur in comment: %s " % e)