def get_location_feeds(request):
    result=dict()
    result['success']=True
    result['message']='success'
    
    sw = request.GET.get('sw',False)
    ne = request.GET.get('ne',False)
    if not sw or not ne:
        return my_utils.return_error('No GPS')
    
    try:
        sw_lat, sw_lng = sw.split(",")
        ne_lat, ne_lng = ne.split(",")
    except Exception as e:
        print str(e)
        return my_utils.return_error('GPS Error')
        
    
    try:
        base_id = request.GET.get('base_id',False)
        additional = Q()
        if base_id:
            additional = Q(id__lt = base_id)
        feeds = Message.objects.filter(additional, lat__range=(sw_lat,ne_lat),lng__range=(sw_lng,ne_lng), is_deleted=False).order_by('-id')[:DEFAULT_LOAD_LENGTH]
        if len(feeds) == DEFAULT_LOAD_LENGTH:
                result['load_more']=True
        
        result['feeds']=my_utils.process_messages(request,feeds)
    except Exception as e:
        print str(e)
        return my_utils.return_error('Error')
    return HttpResponse(json.dumps(result, indent=4), mimetype='application/json')
def get_user_feed(request,user_name):
    result=dict()
    result['success']=True
    result['message']='success'
    
    try:
        user = User.objects.get(username=user_name)
        base_id = request.GET.get("base_id",False)
        additional = Q()
        if base_id:
            additional = Q(id__lt=base_id)
        
        try:
            messages = Message.objects.filter(additional, author=user,is_deleted=False).order_by('-reg_date')[:DEFAULT_LOAD_LENGTH]
            result['feeds']=my_utils.process_messages(request,messages)
            
            if len(messages) == DEFAULT_LOAD_LENGTH:
                result['load_more']=True
            
        except:
            result['success']=True
            result['message']='Do not have any message'
    except:
            return my_utils.return_error('No Such User')
            
    return HttpResponse(json.dumps(result, indent=4), mimetype='application/json')
def load_my_timeline(request):
    result=dict()
    result['success']=True
    result['message']='success'
    try:
        user = User.objects.get(username=request.user.username)
        try:
            base_id = request.GET.get("base_id",False)
            to_id = request.GET.get("to_id",False)
            sort_method = request.GET.get("sort","reg_date")
            additional = Q()
            load_length=DEFAULT_LOAD_LENGTH
            if base_id:
                try:
                    if sort_method == 'reg_date':
                        timeline = UserTimeline.objects.get(id=base_id)
                        additional = Q(message__reg_date__lt=timeline.message.reg_date)
                    else:
                        timeline = UserTimeline.objects.get(id=base_id)
                        additional = Q(message__update_date__lt=timeline.message.update_date)
                except:
                    pass
            if to_id:
                try:
                    timeline = UserTimeline.objects.get(id=base_id)
                    additional = Q(update_date__gte=timeline.update_date)
                except:
                    pass               
            
            
            if sort_method == 'reg_date':
                timelines = UserTimeline.objects.filter(additional,user=user,message__is_deleted=False).order_by('-message__reg_date')[:load_length]
            else:
                timelines = UserTimeline.objects.filter(additional,user=user,message__is_deleted=False).order_by('-message__update_date')[:load_length]
            
            if len(timelines) == DEFAULT_LOAD_LENGTH:
                result['load_more']=True
            messages = list()
            for timeline in timelines:
                try:
                    if not timeline.message.is_deleted:
                        timeline.message.base_id=timeline.id
                        messages.append(timeline.message)
                except:
                    pass
            
            result['feeds']=my_utils.process_messages(request,messages)
                
        except Exception as e:
            print str(e)
            result['success']=True
            result['message']='Do not have any message'
    except:
            return my_utils.return_error('No Such User')
            
    return HttpResponse(json.dumps(result, indent=4), mimetype='application/json')   
def get_random_feeds(request):
    result=dict()
    result['success']=True
    result['message']='success'
    
    try:
        feeds = Message.objects.filter(~Q(lat='') & ~Q(lng=''), is_deleted=False).order_by('?').order_by('-id')[:DEFAULT_LOAD_LENGTH]
        
        result['feeds']=my_utils.process_messages(request,feeds)
    except Exception as e:
        print str(e)
    return HttpResponse(json.dumps(result, indent=4), mimetype='application/json')
def load_topic_timeline(request,topic_id):
    result=dict()
    result['success']=True
    result['message']='success'
    
    try:
        base_id = request.GET.get("base_id",False)
        additional = Q()
        if base_id:
            additional = Q(id__lt=base_id)
        
        topic = Topic.objects.get(id=topic_id)
        try:
            timelines = TopicTimeline.objects.filter(additional, topic=topic).order_by('-update_date')[:DEFAULT_LOAD_LENGTH]
            if not timelines:
                result['feeds']=my_utils.process_messages(request, list())
                return HttpResponse(json.dumps(result, indent=4), mimetype='application/json')
            messages = list()
            for timeline in timelines:
                try:
                    if not timeline.message.is_deleted:
                        timeline.message.base_id=timeline.id
                        messages.append(timeline.message)
                except:
                    pass
            
            if len(timelines) == DEFAULT_LOAD_LENGTH:
                result['load_more']=True
            
            result['feeds']=my_utils.process_messages(request, messages)
                
        except Exception as e:
            print str(e)
            result['success']=True
            result['message']='Do not have any message'
    except:
            return my_utils.return_error('No Such Topic')
            
    return HttpResponse(json.dumps(result, indent=4), mimetype='application/json')
def feed_detail(request, feed_id):
    t = loader.get_template('feed_detail.html')
    context = RequestContext(request)
    
    user = get_object_or_404(User,username=request.user.username)
    user_profile = get_object_or_404(UserProfile,user=user)
    
    context['user_favorite_topics'] = my_utils.get_favorite_topics(request.user)
    context['current_user'] = user
    context['page_feed'] = "selected"
    context['user_profile'] = user_profile
    
    feed = get_object_or_404(Message,id=feed_id)
    if not feed.is_deleted:
        feed = my_utils.process_messages(request, [feed])
        context['feed'] = feed[0]
    
    return HttpResponse(t.render(context))
def load_comany_feed(request):
    result=dict()
    result['success']=True
    result['message']='success'
    try:
        base_id = request.GET.get("base_id",False)
        to_id = request.GET.get("to_id",False)
        sort_method = request.GET.get("sort","reg_date")
        additional = Q()
        load_length = DEFAULT_LOAD_LENGTH
        if base_id:
            try:
                if sort_method == 'reg_date':
                    message = Message.objects.get(id=base_id)
                    additional = Q(reg_date__lt=message.reg_date)
                else:
                    message = Message.objects.get(id=base_id)
                    additional = Q(update_date__lt=message.update_date)
            except:
                pass
            #additional = Q(id__lt=base_id)
        if to_id:
            load_length = 10000
            additional = Q(id__gte=base_id)
        
        
        if sort_method == 'reg_date':
            messages = Message.objects.filter(additional,is_deleted=False).order_by('-reg_date')[:load_length]
        else:
            messages = Message.objects.filter(additional,is_deleted=False).order_by('-update_date')[:load_length]
        
        
        result['feeds']=my_utils.process_messages(request,messages)
        
        if len(messages) == DEFAULT_LOAD_LENGTH:
            result['load_more']=True
    except Exception as e:
        print str(e)
        result['success']=True
        result['message']='Do not have any message'
            
    return HttpResponse(json.dumps(result, indent=4), mimetype='application/json')
def search_feeds(request, inStr):
    result = list()

    try:
        arr = inStr.split(" ")
        query_type = Q()
        for item in arr:
            query_type = query_type & Q(contents__icontains=item)
        feeds = Message.objects.filter(query_type, is_deleted=False).order_by("-reg_date")
        for feed in feeds:
            try:
                user_profile = UserProfile.objects.get(user=feed.author)
                feed.author_picture = user_profile.picture.url
            except Exception as e:
                feed.author_picture = "/media/default.png"
        result = my_utils.process_messages(request, feeds)
    except Exception as e:
        print str(e)
        pass

    return result
def load_notice(request):
    result=dict()
    result['success']=True
    result['message']='success'
    
    try:
        base_id = request.GET.get("base_id",False)
        additional = Q()
        if base_id:
            additional = Q(id__lt=base_id)
        
        notices = Notice.objects.filter(additional,is_deleted=False).order_by('-reg_date')[:DEFAULT_LOAD_LENGTH]
        result['feeds']=my_utils.process_messages(request,notices)
        
        if len(notices) == DEFAULT_LOAD_LENGTH:
            result['load_more']=True             
    except:
        result['success']=True
        result['message']='Do not have any message'
            
    return HttpResponse(json.dumps(result, indent=4), mimetype='application/json')
def get_user_at_feed(request,user_name):
    if user_name is '':
        return my_utils.return_error('user_name is empty')
    result=dict()
    result['success']=True
    result['message']='success'
    try:
        base_id = request.GET.get("base_id",False)
        additional = Q()
        if base_id:
            additional = Q(id__lt=base_id)
        
        messages = Message.objects.filter(additional, related_users__contains=user_name+',',is_deleted=False).order_by('-reg_date')[:DEFAULT_LOAD_LENGTH]
        result['feeds']=my_utils.process_messages(request,messages)
        
        if len(messages) == DEFAULT_LOAD_LENGTH:
                result['load_more']=True             
    except:
        result['success']=True
        result['message']='Do not have any message'
            
    return HttpResponse(json.dumps(result, indent=4), mimetype='application/json')
def load_favorite(request, user_name):
    result=dict()
    result['success']=True
    result['message']='success'
    
    try:
        user = User.objects.get(username=user_name)
        try:
            base_id = request.GET.get("base_id",False)
            additional = Q()
            if base_id:
                additional = Q(id__lt=base_id)
            
            favorites = UserFavorite.objects.filter(additional, user=user,message__is_deleted=False).order_by('-reg_date')[:DEFAULT_LOAD_LENGTH]
            messages = list()
            
            for favorite in favorites:
                try:
                    if not favorite.message.is_deleted:
                        favorite.message.base_id=favorite.id
                        messages.append(favorite.message)
                except Exception as e:
                    print str(e)
                    pass
            
            if len(favorites) == DEFAULT_LOAD_LENGTH:
                result['load_more']=True
            
            result['feeds']=my_utils.process_messages(request,messages)
                
        except:
            result['success']=True
            result['message']='Do not have any message'
    except:
            return my_utils.return_error('No Such User')
            
    return HttpResponse(json.dumps(result, indent=4), mimetype='application/json')