예제 #1
0
파일: models.py 프로젝트: johnccchang/Jingo
    def updateFilter(self, request):
        data = self.readData(request)
        if data['f_repeat'] == 'on':
            data['f_repeat'] = 1
        else:
            data['f_repeat'] = 0

        args = {}
        args['table'] = 'Filter'
        args['attributes'] = [
            'f_start_time', 'f_stop_time', 'f_repeat', 'f_visibility'
        ]
        args['values'] = [
            data['f_start_time'], data['f_stop_time'], data['f_repeat'],
            data['f_visibility'], data['stateid'], data['uid'], data['tagid']
        ]
        args['conditions'] = [{
            'field': 'stateid',
            'logic': 'And'
        }, {
            'field': 'uid',
            'logic': 'And'
        }, {
            'field': 'tagid',
            'logic': 'And'
        }]
        SQLExecuter().doUpdateData(args)
        '''
        Filter.objects.filter(stateid=data['stateid'], uid=data['uid'], tagid=data['tagid']).update(
            f_start_time=data['f_start_time'], f_stop_time=data['f_stop_time'], f_repeat=data['f_repeat'],
            f_visibility=data['f_visibility'])
            '''
        return self.createResultSet(data)
예제 #2
0
파일: models.py 프로젝트: johnccchang/Jingo
 def getPendingsInfoList(self, data):
     strSQL = 'Select uid, invitationid From friend Where is_friendship = 2 And f_uid= %s And uid Not In (Select f_uid From friend Where is_friendship = 1 And uid = %s)'
     args = [data['uid'], data['uid']]
     plist = SQLExecuter().doRawSQL(strSQL, args)
     flist = []
     for friend in plist:
         fuser = User.objects.filter(uid=friend['uid']).values()[0]
         fuser['invitationid'] = friend['invitationid']
         flist.append(fuser)
     return flist
예제 #3
0
파일: models.py 프로젝트: johnccchang/Jingo
    def addNote(self, data):
        newNoteid = self.getNewNoteid()
        '''
        notetime = Note()
        notetime.note = data['note']
        #notetime.n_timestamp = timezone.now()
        print "======Insert time======"
        notetime.n_timestamp = JingoTimezone().getLocalTime()
        notetime.link = ''
        notetime.noteid = newNoteid
        notetime.uid = User(uid=data['uid'])

        if 'radius' in data:
            notetime.radius = data['radius']
            notetime.n_visibility = data['n_visibility']
            if 'is_comment' in data:
                notetime.is_comment = data['is_comment']
            else:
                notetime.is_comment = 0
        else:
            notetime.radius = N_DEFAULT_RADIUS     # default 200 yards
            notetime.n_visibility = 0                    # default 0: public
            notetime.is_comment = IS_COMMENT

        notetime.n_latitude = data['n_latitude']
        notetime.n_longitude = data['n_longitude']
        notetime.n_like = N_LIKES
        notetime.save()
        '''
        if 'radius' in data:
            radius = data['radius']
            n_visibility = data['n_visibility']
            if 'is_comment' in data:
                is_comment = data['is_comment']
            else:
                is_comment = 0
        else:
            radius = N_DEFAULT_RADIUS  # default 200 yards
            n_visibility = 0  # default 0: public
            is_comment = IS_COMMENT

        data['n_longitude'] = "%.6f" % float(data['n_longitude'])
        data['n_latitude'] = "%.6f" % float(data['n_latitude'])
        timestamp = JingoTimezone().getLocalTime()
        values = [
            data['note'], timestamp, data['link'], newNoteid, data['uid'],
            radius, n_visibility, data['n_latitude'], data['n_longitude'],
            is_comment, N_LIKES
        ]
        #print values
        args = dict([('table', 'note'), ('values', values)])
        SQLExecuter().doInsertData(args)

        data['noteid'] = newNoteid
        return data
예제 #4
0
파일: models.py 프로젝트: johnccchang/Jingo
    def addInvitation(self, data):
        newInvitationid = self.getNewInvitationid()
        # 0:denied, 1:accepted, 2:pending, 3:cancel
        values = [int(data['uid']), int(data['f_uid']), 2, newInvitationid]
        args = dict([('table', 'friend'), ('values', values)])
        SQLExecuter().doInsertData(args)

        data['friendship'] = 'pending'
        data['invitationid'] = newInvitationid

        return data
예제 #5
0
파일: models.py 프로젝트: johnccchang/Jingo
 def logUserKeywords(self, data, keywords):
     #currenttime         = timezone.now()
     currenttime = JingoTimezone().getLocalTime()
     data['u_longitude'] = "%.6f" % float(data['u_longitude'])
     data['u_latitude'] = "%.6f" % float(data['u_latitude'])
     for keyword in keywords:
         values = [
             self.getNewLogid(),
             int(data['uid']), keyword, data['u_longitude'],
             data['u_latitude'], currenttime
         ]
         args = dict([('table', 'log_keywords'), ('values', values)])
         SQLExecuter().doInsertData(args)
예제 #6
0
파일: models.py 프로젝트: johnccchang/Jingo
 def deleteTag(self, request):
     data = self.readData(request)
     args = {}
     args['table'] = 'Tag'
     args['attributes'] = [{
         'field': 'stateid',
         'logic': 'And'
     }, {
         'field': 'uid',
         'logic': 'And'
     }]
     args['values'] = [data['tagid'], data['uid']]
     SQLExecuter().doDeleteData(args)
     return self.createResultSet(data)
예제 #7
0
파일: models.py 프로젝트: johnccchang/Jingo
 def addComment(self, data):
     newCommentid = self.getNewCommentid()
     data['c_longitude'] = "%.6f" % float(data['c_longitude'])
     data['c_latitude'] = "%.6f" % float(data['c_latitude'])
     values = [
         newCommentid,
         int(data['noteid']),
         JingoTimezone().getLocalTime(),
         int(data['uid']),
         float(data['c_latitude']),
         float(data['c_longitude']), data['comment']
     ]
     #print values
     args = dict([('table', 'comments'), ('values', values)])
     SQLExecuter().doInsertData(args)
     return newCommentid
예제 #8
0
파일: models.py 프로젝트: johnccchang/Jingo
 def getUserSysTags(self, data):
     result = []
     args = {}
     args['columns'] = ['b.*, a.tag_name, a.sys_tagid']
     args['tables'] = ['tag as a', 'filter as b']
     args['joins'] = ['a.tagid = b.tagid', 'a.tagid>=%s And a.tagid<=%s']
     args['conditions'] = [{
         'criteria': 'b.uid=',
         'logic': 'And'
     }, {
         'criteria': 'b.stateid=',
         'logic': 'And'
     }]
     args['values'] = [0, 10, data['uid_id'], data['stateid']]
     slist = SQLExecuter().doSelectData(args)
     for sys in slist:
         sys['is_checked'] = 0
         result.append(sys)
     return result
예제 #9
0
파일: models.py 프로젝트: johnccchang/Jingo
 def addNoteTag(self, data):
     args = dict([('table', 'note_tag'),
                  ('values', [data['noteid'], data['tagid']])])
     SQLExecuter().doInsertData(args)
     return data
예제 #10
0
파일: models.py 프로젝트: johnccchang/Jingo
 def insertState(self, uid, is_current, stateid):
     data = [stateid, STATE_NAME_DEFAULT, uid, is_current]
     args = dict([('table', 'State'), ('values', data)])
     SQLExecuter().doInsertData(args)
예제 #11
0
파일: models.py 프로젝트: johnccchang/Jingo
 def getAreasRanking(self):
     strSQL = 'Select n_longitude, n_latitude, n_notes, top_tag From v_areas_ranking'
     result = SQLExecuter().doRawSQL(strSQL)
     return result
예제 #12
0
파일: models.py 프로젝트: johnccchang/Jingo
 def getStatistic(self):
     strSQL = 'Select * From v_statistic'
     result = SQLExecuter().doRawSQL(strSQL)
     return result
예제 #13
0
파일: models.py 프로젝트: johnccchang/Jingo
 def __init__(self):
     self.sql = SQLExecuter()
예제 #14
0
파일: models.py 프로젝트: johnccchang/Jingo
 def addFilter(self, data):
     args = dict([('table', 'filter'), ('values', data)])
     SQLExecuter().doInsertData(args)
예제 #15
0
파일: models.py 프로젝트: jiankaidang/Jingo
class NoteFilter(HttpRequestResponser, Formatter):
    def __init__(self):
        self.sql = SQLExecuter()

    def getValuesBasedonKey(self, valueset, key):
        result = []
        for row in valueset:
            result.append(row[key])
        return result

    def getKeywordString(self, data):
        result, keywordset, sql = [{}, [], ""]
        keywords = string.split(data["keywords"], " ")

        # log keywords the user uses and his location
        Log_Keywords().logUserKeywords(data, keywords)

        # make query string
        for word in keywords:
            keywordset.append("%" + word + "%")
            keywordset.append("%" + word + "%")
            sql += "(a.note like %s Or c.tag_name like %s) And "

        result["sql"] = "(" + sql[: len(sql) - 5] + ")"
        result["keywords"] = keywordset
        result["n_keywords"] = len(keywordset)
        return result

    def getNoteInfoListByKewords(self, data, currenttime):
        # data['keywords'] = '%' + data['keywords'] + '%'
        keywordset = self.getKeywordString(data)
        # strSQL = "Select a.*, b.tagid, c.sys_tagid, c.tag_name, d.n_start_time, d.n_stop_time, n_repeat From note as a, note_tag as b, tag as c, (Select * From note_time Where %s between n_start_time And n_stop_time And n_repeat=0) as d Where a.noteid=b.noteid And b.tagid=c.tagid And a.noteid=d.noteid And (a.note like %s Or c.tag_name like %s) Union Select a.*, b.tagid, c.sys_tagid, c.tag_name, d.n_start_time, d.n_stop_time, n_repeat From note as a, note_tag as b, tag as c, (Select * From note_time Where %s between n_start_time And n_stop_time And n_repeat=1) as d Where a.noteid=b.noteid And b.tagid=c.tagid And a.noteid=d.noteid And (a.note like %s Or c.tag_name like %s)"
        strSQL = (
            "Select a.*, b.tagid, c.sys_tagid, c.tag_name, d.n_start_time, d.n_stop_time, n_repeat From note as a, note_tag as b, tag as c, (Select * From note_time Where %s between n_start_time And n_stop_time And n_repeat=0) as d Where a.noteid=b.noteid And b.tagid=c.tagid And a.noteid=d.noteid And %s Union Select a.*, b.tagid, c.sys_tagid, c.tag_name, d.n_start_time, d.n_stop_time, n_repeat From note as a, note_tag as b, tag as c, (Select * From note_time Where %s between n_start_time And n_stop_time And n_repeat=1) as d Where a.noteid=b.noteid And b.tagid=c.tagid And a.noteid=d.noteid And %s"
            % ("%s", keywordset["sql"], "%s", keywordset["sql"])
        )

        values = keywordset["keywords"] * 2
        values.insert(0, currenttime)
        values.insert(int(keywordset["n_keywords"]) + 1, currenttime)
        # print values
        noteslist = self.sql.doRawSQL(strSQL, values)
        return noteslist

    def getNoteInfoList(self, currenttime):
        # retrieve every detail of notes
        strSQL = "Select a.*, b.tagid, c.sys_tagid, d.n_start_time, d.n_stop_time, n_repeat From note as a, note_tag as b, tag as c, (Select * From note_time Where %s between n_start_time And n_stop_time And n_repeat=0) as d Where a.noteid=b.noteid And b.tagid=c.tagid And a.noteid=d.noteid Union Select a.*, b.tagid, c.sys_tagid, d.n_start_time, d.n_stop_time, n_repeat From note as a, note_tag as b, tag as c, (Select * From note_time Where %s between n_start_time And n_stop_time And n_repeat=1) as d Where a.noteid=b.noteid And b.tagid=c.tagid And a.noteid=d.noteid"
        noteslist = self.sql.doRawSQL(strSQL, [currenttime, currenttime])
        return noteslist

    def getUserCategoryTagsList(self, data):
        args = {}
        args["columns"] = ["b.*, c.sys_tagid"]
        args["tables"] = ["state as a", "filter as b", "tag as c"]
        args["joins"] = ["a.stateid=b.stateid And a.uid=b.uid And b.tagid=c.tagid And a.is_current=1 And is_checked=1"]
        args["conditions"] = [{"criteria": "b.uid=", "logic": "And"}]
        args["values"] = [data["uid"]]
        uCTags = self.sql.doSelectData(args)
        return uCTags

    def computeDistance(self, data, n_longitude, n_latitude):
        """
        Calculate the great circle distance between two points 
        on the earth (specified in decimal degrees)
        """
        # convert decimal degrees to radians
        lon1, lat1, lon2, lat2 = map(
            radians, [float(n_longitude), float(n_latitude), float(data["u_longitude"]), float(data["u_latitude"])]
        )

        # haversine formula
        dlon = lon2 - lon1
        dlat = lat2 - lat1
        a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2
        c = 2 * asin(sqrt(a))
        dist = (6367 * c) * 1093.61  # km to yard
        return dist

    def filterByTags(self, uProfile, noteslist):
        result = []
        passset = self.getValuesBasedonKey(uProfile, "sys_tagid")
        for note in noteslist:
            if note["sys_tagid"] in passset:
                result.append(note)
        return result

    def filterByTime(self, uProfile, noteslist, currenttime):
        result = []
        sys_tagset = []
        currenttime = datetime.datetime.strptime(currenttime, "%Y-%m-%d %H:%M:%S")

        for filter in uProfile:
            if filter["f_repeat"]:
                current = currenttime.strftime("%H:%M:%S")
                start = filter["f_start_time"].strftime("%H:%M:%S")
                end = filter["f_stop_time"].strftime("%H:%M:%S")
            else:
                current = currenttime.strftime("%Y-%m-%d %H:%M:%S")
                start = filter["f_start_time"]
                end = filter["f_stop_time"]
            # print "current %s" % current
            # print "start %s" % start
            # print "end %s" % end
            if current >= start and current <= end:
                sys_tagset.append(filter["sys_tagid"])

        # print "active sys_tagset"
        # print sys_tagset

        for note in noteslist:
            # print "note " + str(note['noteid']) + " has sys:" + str(note['sys_tagid'])
            if note["sys_tagid"] in sys_tagset:
                # print "note " + str(note['noteid']) + " passed"
                result.append(note)
        return result

    def filterByVisibility(self, data, uProfile, noteslist):
        friendslist = Friend().getFriendsList(data)

        # print "friendslist"
        # print friendslist
        # generalize visibility of user tags based on sys_tags
        sys_visset = {}
        result = []
        # print "current uProfile"
        # print uProfile
        for ufilter in uProfile:
            sys_tag = ufilter["sys_tagid"]
            visibility = ufilter["f_visibility"]
            if (sys_tag in sys_visset and sys_visset[sys_tag] < visibility) or (sys_tag not in sys_visset):
                sys_visset[sys_tag] = visibility
        # print "visibility of sys_tag"
        # print sys_visset

        for note in noteslist:
            # print "with sys:" + str(note['sys_tagid']) + ", note " + str(note['noteid']) + " has vis:" + str(note['n_visibility']) + " while user has vis:" + str(sys_visset[note['sys_tagid']])
            case_a, case_b, case_c, case_d, case_e, case_f, case_g = [False, False, False, False, False, False, False]
            if note["sys_tagid"] in sys_visset:
                # print str(note['uid']) in friendslist
                # if both of them have visibilities of public
                if sys_visset[note["sys_tagid"]] == 0 and note["n_visibility"] == 0:
                    case_a = True
                    # print "note " + str(note['noteid']) + " passed because of case_a"

                # if sys_visset[note['sys_tagid']] == 0 and note['n_visibility'] == 1 and (str(note['uid']) != str(data['uid']) and note['uid'] in friendslist):
                if (
                    sys_visset[note["sys_tagid"]] == 0
                    and note["n_visibility"] == 1
                    and not ((str(note["uid"]) != str(data["uid"]) and note["uid"] not in friendslist))
                ):
                    case_b = True
                    # print "note " + str(note['noteid']) + " passed because of case_b"

                if (
                    sys_visset[note["sys_tagid"]] == 0
                    and note["n_visibility"] == 2
                    and str(note["uid"]) == str(data["uid"])
                ):
                    case_c = True
                    # print "note " + str(note['noteid']) + " passed because of case_c"

                if (
                    sys_visset[note["sys_tagid"]] == 1
                    and note["n_visibility"] == 0
                    and not (str(note["uid"]) != str(data["uid"]) and note["uid"] not in friendslist)
                ):
                    case_d = True
                    # print "note " + str(note['noteid']) + " passed because of case_d"

                if (
                    sys_visset[note["sys_tagid"]] == 1
                    and note["n_visibility"] == 1
                    and not (str(note["uid"]) != str(data["uid"]) and note["uid"] not in friendslist)
                ):
                    case_e = True
                    # print "note " + str(note['noteid']) + " passed because of case_e"

                if (
                    sys_visset[note["sys_tagid"]] == 1
                    and note["n_visibility"] == 2
                    and str(note["uid"]) == str(data["uid"])
                ):
                    case_f = True
                    # print "note " + str(note['noteid']) + " passed because of case_f"

                # if reader has private and he is poster also
                # print data['uid']
                # print note['uid']
                if sys_visset[note["sys_tagid"]] == 2 and str(note["uid"]) == str(data["uid"]):
                    case_g = True
                    # print "note " + str(note['noteid']) + " passed because of case_g"

                if case_a or case_b or case_c or case_d or case_e or case_f or case_g:
                    result.append(note)

                    """
            if note['sys_tagid'] in sys_visset and sys_visset[note['sys_tagid']] == note['n_visibility']:
                if (note['n_visibility'] == 1 and note['uid'] in friendslist) or note['n_visibility'] == 0:
                    result.append(note)
                    """
        # print "after visibility"
        # print result
        return result

    def filterByLocation(self, data, noteslist):
        result = []
        for note in noteslist:
            dist = self.computeDistance(data, note["n_longitude"], note["n_latitude"])
            if dist <= note["radius"]:
                result.append(note)
        return result

    def filterNotes(self, data, mode="normal"):
        localtime = JingoTimezone().getLocalTime()
        currenttime = localtime.strftime("%Y-%m-%d %H:%M:%S")
        if mode == "normal":
            noteslist = self.getNoteInfoList(currenttime)
        else:
            noteslist = self.getNoteInfoListByKewords(data, currenttime)
        uProfile = self.getUserCategoryTagsList(data)

        # filter by user's tags
        noteslist = self.filterByTags(uProfile, noteslist)

        # filter by user's time range
        noteslist = self.filterByTime(uProfile, noteslist, currenttime)

        # filter by visibility and friendship
        noteslist = self.filterByVisibility(data, uProfile, noteslist)

        # filter by location
        noteslist = self.filterByLocation(data, noteslist)
        # print noteslist
        return noteslist

    def retrieveNotesByKeywords(self, data):
        return self.filterNotes(data, "keyword")
예제 #16
0
파일: models.py 프로젝트: johnccchang/Jingo
 def getTagsRanking(self):
     strSQL = 'Select tag_name, n_notes From v_tags_ranking'
     result = SQLExecuter().doRawSQL(strSQL)
     return result
예제 #17
0
파일: models.py 프로젝트: johnccchang/Jingo
 def getPosterRanking(self):
     strSQL = 'Select u_name, n_notes From v_poster_ranking'
     result = SQLExecuter().doRawSQL(strSQL)
     return result
예제 #18
0
파일: models.py 프로젝트: johnccchang/Jingo
 def getNotesRanking(self):
     strSQL = 'Select * From v_notes_ranking'
     result = SQLExecuter().doRawSQL(strSQL)
     return result
예제 #19
0
파일: models.py 프로젝트: johnccchang/Jingo
 def getKeywordsRanking(self):
     strSQL = 'Select * From v_keywords_ranking'
     result = SQLExecuter().doRawSQL(strSQL)
     return result
예제 #20
0
파일: models.py 프로젝트: jiankaidang/Jingo
 def __init__(self):
     self.sql = SQLExecuter()
예제 #21
0
파일: models.py 프로젝트: johnccchang/Jingo
class NoteFilter(HttpRequestResponser, Formatter):
    def __init__(self):
        self.sql = SQLExecuter()

    def getValuesBasedonKey(self, valueset, key):
        result = []
        for row in valueset:
            result.append(row[key])
        return result

    def getKeywordString(self, data):
        result, keywordset, sql = [{}, [], '']
        keywords = string.split(data['keywords'], ' ')

        # log keywords the user uses and his location
        Log_Keywords().logUserKeywords(data, keywords)

        # make query string
        for word in keywords:
            keywordset.append('%' + word + '%')
            keywordset.append('%' + word + '%')
            sql += '(a.note like %s Or c.tag_name like %s) And '

        result['sql'] = '(' + sql[:len(sql) - 5] + ')'
        result['keywords'] = keywordset
        result['n_keywords'] = len(keywordset)
        return result

    def getNoteInfoListByKewords(self, data, currenttime):
        #data['keywords'] = '%' + data['keywords'] + '%'
        keywordset = self.getKeywordString(data)
        #strSQL = "Select a.*, b.tagid, c.sys_tagid, c.tag_name, d.n_start_time, d.n_stop_time, n_repeat From note as a, note_tag as b, tag as c, (Select * From note_time Where %s between n_start_time And n_stop_time And n_repeat=0) as d Where a.noteid=b.noteid And b.tagid=c.tagid And a.noteid=d.noteid And (a.note like %s Or c.tag_name like %s) Union Select a.*, b.tagid, c.sys_tagid, c.tag_name, d.n_start_time, d.n_stop_time, n_repeat From note as a, note_tag as b, tag as c, (Select * From note_time Where %s between n_start_time And n_stop_time And n_repeat=1) as d Where a.noteid=b.noteid And b.tagid=c.tagid And a.noteid=d.noteid And (a.note like %s Or c.tag_name like %s)"
        strSQL = "Select a.*, b.tagid, c.sys_tagid, c.tag_name, d.n_start_time, d.n_stop_time, n_repeat From note as a, note_tag as b, tag as c, (Select * From note_time Where %s between n_start_time And n_stop_time And n_repeat=0) as d Where a.noteid=b.noteid And b.tagid=c.tagid And a.noteid=d.noteid And %s Union Select a.*, b.tagid, c.sys_tagid, c.tag_name, d.n_start_time, d.n_stop_time, n_repeat From note as a, note_tag as b, tag as c, (Select * From note_time Where %s between n_start_time And n_stop_time And n_repeat=1) as d Where a.noteid=b.noteid And b.tagid=c.tagid And a.noteid=d.noteid And %s" % (
            '%s', keywordset['sql'], '%s', keywordset['sql'])

        values = keywordset['keywords'] * 2
        values.insert(0, currenttime)
        values.insert(int(keywordset['n_keywords']) + 1, currenttime)
        #print values
        noteslist = self.sql.doRawSQL(strSQL, values)
        return noteslist

    def getNoteInfoList(self, currenttime):
        # retrieve every detail of notes
        strSQL = 'Select a.*, b.tagid, c.sys_tagid, d.n_start_time, d.n_stop_time, n_repeat From note as a, note_tag as b, tag as c, (Select * From note_time Where %s between n_start_time And n_stop_time And n_repeat=0) as d Where a.noteid=b.noteid And b.tagid=c.tagid And a.noteid=d.noteid Union Select a.*, b.tagid, c.sys_tagid, d.n_start_time, d.n_stop_time, n_repeat From note as a, note_tag as b, tag as c, (Select * From note_time Where %s between n_start_time And n_stop_time And n_repeat=1) as d Where a.noteid=b.noteid And b.tagid=c.tagid And a.noteid=d.noteid'
        noteslist = self.sql.doRawSQL(strSQL, [currenttime, currenttime])
        return noteslist

    def getUserCategoryTagsList(self, data):
        args = {}
        args['columns'] = ['b.*, c.sys_tagid']
        args['tables'] = ['state as a', 'filter as b', 'tag as c']
        args['joins'] = [
            'a.stateid=b.stateid And a.uid=b.uid And b.tagid=c.tagid And a.is_current=1 And is_checked=1'
        ]
        args['conditions'] = [{'criteria': 'b.uid=', 'logic': 'And'}]
        args['values'] = [data['uid']]
        uCTags = self.sql.doSelectData(args)
        return uCTags

    def computeDistance(self, data, n_longitude, n_latitude):
        """
        Calculate the great circle distance between two points 
        on the earth (specified in decimal degrees)
        """
        # convert decimal degrees to radians
        lon1, lat1, lon2, lat2 = map(radians, [
            float(n_longitude),
            float(n_latitude),
            float(data['u_longitude']),
            float(data['u_latitude'])
        ])

        # haversine formula
        dlon = lon2 - lon1
        dlat = lat2 - lat1
        a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
        c = 2 * asin(sqrt(a))
        dist = (6367 * c) * 1093.61  # km to yard
        return dist

    def filterByTags(self, uProfile, noteslist):
        result = []
        passset = self.getValuesBasedonKey(uProfile, 'sys_tagid')
        for note in noteslist:
            if note['sys_tagid'] in passset:
                result.append(note)
        return result

    def filterByTime(self, uProfile, noteslist, currenttime):
        result = []
        sys_tagset = []
        currenttime = datetime.datetime.strptime(currenttime,
                                                 '%Y-%m-%d %H:%M:%S')

        for filter in uProfile:
            if filter['f_repeat']:
                current = currenttime.strftime('%H:%M:%S')
                start = filter['f_start_time'].strftime('%H:%M:%S')
                end = filter['f_stop_time'].strftime('%H:%M:%S')
            else:
                current = currenttime.strftime('%Y-%m-%d %H:%M:%S')
                start = filter['f_start_time']
                end = filter['f_stop_time']
            #print "current %s" % current
            #print "start %s" % start
            #print "end %s" % end
            if current >= start and current <= end:
                sys_tagset.append(filter['sys_tagid'])

        #print "active sys_tagset"
        #print sys_tagset

        for note in noteslist:
            #print "note " + str(note['noteid']) + " has sys:" + str(note['sys_tagid'])
            if note['sys_tagid'] in sys_tagset:
                #print "note " + str(note['noteid']) + " passed"
                result.append(note)
        return result

    def filterByVisibility(self, data, uProfile, noteslist):
        friendslist = Friend().getFriendsList(data)

        #print "friendslist"
        #print friendslist
        # generalize visibility of user tags based on sys_tags
        sys_visset = {}
        result = []
        #print "current uProfile"
        #print uProfile
        for ufilter in uProfile:
            sys_tag = ufilter['sys_tagid']
            visibility = ufilter['f_visibility']
            if (sys_tag in sys_visset and
                    sys_visset[sys_tag] < visibility) or (sys_tag
                                                          not in sys_visset):
                sys_visset[sys_tag] = visibility
        #print "visibility of sys_tag"
        #print sys_visset

        for note in noteslist:
            #print "with sys:" + str(note['sys_tagid']) + ", note " + str(note['noteid']) + " has vis:" + str(note['n_visibility']) + " while user has vis:" + str(sys_visset[note['sys_tagid']])
            case_a, case_b, case_c, case_d, case_e, case_f, case_g = [
                False, False, False, False, False, False, False
            ]
            if note['sys_tagid'] in sys_visset:
                #print str(note['uid']) in friendslist
                # if both of them have visibilities of public
                if sys_visset[
                        note['sys_tagid']] == 0 and note['n_visibility'] == 0:
                    case_a = True
                    #print "note " + str(note['noteid']) + " passed because of case_a"

                #if sys_visset[note['sys_tagid']] == 0 and note['n_visibility'] == 1 and (str(note['uid']) != str(data['uid']) and note['uid'] in friendslist):
                if sys_visset[note['sys_tagid']] == 0 and note[
                        'n_visibility'] == 1 and not (
                            (str(note['uid']) != str(data['uid'])
                             and note['uid'] not in friendslist)):
                    case_b = True
                    #print "note " + str(note['noteid']) + " passed because of case_b"

                if sys_visset[note['sys_tagid']] == 0 and note[
                        'n_visibility'] == 2 and str(note['uid']) == str(
                            data['uid']):
                    case_c = True
                    #print "note " + str(note['noteid']) + " passed because of case_c"

                if sys_visset[note['sys_tagid']] == 1 and note[
                        'n_visibility'] == 0 and not (str(note['uid']) != str(
                            data['uid']) and note['uid'] not in friendslist):
                    case_d = True
                    #print "note " + str(note['noteid']) + " passed because of case_d"

                if sys_visset[note['sys_tagid']] == 1 and note[
                        'n_visibility'] == 1 and not (str(note['uid']) != str(
                            data['uid']) and note['uid'] not in friendslist):
                    case_e = True
                    #print "note " + str(note['noteid']) + " passed because of case_e"

                if sys_visset[note['sys_tagid']] == 1 and note[
                        'n_visibility'] == 2 and str(note['uid']) == str(
                            data['uid']):
                    case_f = True
                    #print "note " + str(note['noteid']) + " passed because of case_f"

                # if reader has private and he is poster also
                #print data['uid']
                #print note['uid']
                if sys_visset[note['sys_tagid']] == 2 and str(
                        note['uid']) == str(data['uid']):
                    case_g = True
                    #print "note " + str(note['noteid']) + " passed because of case_g"

                if case_a or case_b or case_c or case_d or case_e or case_f or case_g:
                    result.append(note)
                    '''
            if note['sys_tagid'] in sys_visset and sys_visset[note['sys_tagid']] == note['n_visibility']:
                if (note['n_visibility'] == 1 and note['uid'] in friendslist) or note['n_visibility'] == 0:
                    result.append(note)
                    '''
        #print "after visibility"
        #print result
        return result

    def filterByLocation(self, data, noteslist):
        result = []
        for note in noteslist:
            dist = self.computeDistance(data, note['n_longitude'],
                                        note['n_latitude'])
            if dist <= note['radius']:
                result.append(note)
        return result

    def filterNotes(self, data, mode='normal'):
        localtime = JingoTimezone().getLocalTime()
        currenttime = localtime.strftime('%Y-%m-%d %H:%M:%S')
        if mode == 'normal':
            noteslist = self.getNoteInfoList(currenttime)
        else:
            noteslist = self.getNoteInfoListByKewords(data, currenttime)
        uProfile = self.getUserCategoryTagsList(data)

        # filter by user's tags
        noteslist = self.filterByTags(uProfile, noteslist)

        # filter by user's time range
        noteslist = self.filterByTime(uProfile, noteslist, currenttime)

        # filter by visibility and friendship
        noteslist = self.filterByVisibility(data, uProfile, noteslist)

        # filter by location
        noteslist = self.filterByLocation(data, noteslist)
        #print noteslist
        return noteslist

    def retrieveNotesByKeywords(self, data):
        return self.filterNotes(data, 'keyword')