def __call__(self, req): if req.method != "POST": return JsonResponse(ajax_error(405, 'Accepts only POST request')) try: ajax_method = req.POST['ajax_method'] if not hasattr(self, ajax_method): return JsonResponse( ajax_error(405, "Unknown ajax method '%s'" % ajax_method)) method = getattr(self, ajax_method) post = dict(req.POST) del post['ajax_method'] kwargs = {} for name in post: value = post[name] if len(value) == 1: value = value[0] kwargs[str(name)] = value return JsonResponse(method(req, **kwargs)) except Exception, e: try: if dblogger: exc_info = sys.exc_info() report = ExceptionReporter(req, *exc_info) dblogger.exception(e, report.get_traceback_html()) except: pass return JsonResponse( ajax_error(405, "Server Exception: %s:%s" % (e.__class__, e)))
def __call__(self, req): if req.method!="POST": return JsonResponse(ajax_error(405, 'Accepts only POST request')) try: ajax_method=req.POST['ajax_method'] if not hasattr(self, ajax_method): return JsonResponse(ajax_error(405,"Unknown ajax method '%s'" % ajax_method)) method = getattr(self, ajax_method) post=dict(req.POST) del post['ajax_method'] kwargs = {} for name in post: value = post[name] if len(value)==1: value=value[0] kwargs[str(name)] = value return JsonResponse(method(req, **kwargs)) except Exception, e: try: if dblogger: exc_info = sys.exc_info() report = ExceptionReporter(req, *exc_info) dblogger.exception(e, report.get_traceback_html()) except: pass return JsonResponse(ajax_error(405,"Server Exception: %s:%s" % (e.__class__, e)))
def open_user(self, req, user_name): if not req.user.is_authenticated(): return ajax_error(404, 'User not authenticated') try: user_to = User.objects.get(is_active=True, username = user_name) except: return False last_messages=[] ids=[] for msg in Message.objects.distinct().filter( models.Q(author = user_to, to = req.user) | models.Q(author = req.user, to = user_to) ).order_by('-create')[:LAST_MESSAGES_LIMIT]: last_messages.append(self._msg_format(msg, req)) if not msg.is_delivered and msg.to==req.user: ids.append(msg.id) Message.objects.filter(id__in=ids).update(is_delivered=True) req.user.im_incoming-=len(ids) #TODO: optimize.... to req.user.save() last_messages.reverse() return { 'user_full_name': user_to.get_full_name(), 'last_messages': last_messages }
def get_settings(self, req): if not req.user.is_authenticated(): return ajax_error(404, 'User not authenticated') return { 'full_name': req.user.get_full_name() }
def send_msg(self, req, to_user, text): if not req.user.is_authenticated(): return ajax_error(404, 'User not authenticated') to = User.objects.get(is_active=True, username = to_user) msg=Message( author = req.user, to = to, create = datetime.datetime.today(), text = text, is_delivered = False ) msg.save() return self._msg_format(msg, req)
def check_new(self, req): if not req.user.is_authenticated(): return ajax_error(404, 'User not authenticated') new_list = [] ids=[] for msg in Message.objects.filter(to = req.user, is_delivered=False).order_by('create')[:NEW_MESSAGES_LIMIT]: ids.append(msg.id) new_list.append(self._msg_format(msg, req)) Message.objects.filter(id__in=ids).update(is_delivered=True) req.user.im_incoming-=len(new_list) #TODO: optimize.... to req.user.save() return new_list