Beispiel #1
0
    def get(self, request, *args, **kw):

        if request.user.is_authenticated():
            list_viewed = []

            try:
                eprofile = EmifProfile.objects.get(user=request.user)
            except EmifProfile.DoesNotExist:
                print "-- ERROR: Couldn't get emif profile for user"

            most_hit = Hit.objects.filter(user=request.user).values(
                'user', 'hitcount__object_pk').annotate(
                    total_hits=Count('hitcount')).order_by('-total_hits')

            i = 0

            for hit in most_hit:
                try:
                    this_fingerprint = Fingerprint.valid().get(
                        id=hit['hitcount__object_pk'])

                    if eprofile.restricted:
                        try:
                            allowed = RestrictedUserDbs.objects.get(
                                user=request.user,
                                fingerprint=this_fingerprint)
                        except RestrictedUserDbs.DoesNotExist:
                            restricted = RestrictedGroup.hashes(request.user)

                            if this_fingerprint.fingerprint_hash not in RestrictedGroup.hashes(
                                    request.user):
                                continue

                    list_viewed.append({
                        'hash': this_fingerprint.fingerprint_hash,
                        'name': this_fingerprint.findName(),
                        'count': hit['total_hits']
                    })
                    i += 1
                    if i == 10:
                        break

                except Fingerprint.DoesNotExist:
                    print "-- Error on hitcount for fingerprint with id " + hit[
                        'hitcount__object_pk']

            response = Response({'mostviewed': list_viewed},
                                status=status.HTTP_200_OK)

        else:
            response = Response({}, status=status.HTTP_403_FORBIDDEN)
        return response
def restriction(user):
    dbs = RestrictedUserDbs.objects.filter(user=user)
    rest = None
    i = 0

    def add_condition(i, rest, hash):

        if rest == None:
            rest = " AND (id:"+hash
        else:
            rest += " OR id:"+hash

        i += 1

        return (i, rest)

    # The main principle is avoid iterations, since usually this number will be very restricted in comparison with the real value
    for db in dbs:
        (i, rest) = add_condition(i, rest, db.fingerprint.fingerprint_hash)

    dbs = RestrictedGroup.hashes(user)

    for hash in dbs:
        (i, rest) = add_condition(i, rest, hash)

    if i>0:
        rest += ")"

    return rest
Beispiel #3
0
    def get(self, request, *args, **kw):

        if request.user.is_authenticated():
            list_viewed = []

            try:
                eprofile = EmifProfile.objects.get(user=request.user)
            except EmifProfile.DoesNotExist:
                print "-- ERROR: Couldn't get emif profile for user"

            most_hit = Hit.objects.filter(user=request.user).values('user','hitcount__object_pk').annotate(total_hits=Count('hitcount')).order_by('-total_hits')

            i=0

            for hit in most_hit:
                try:
                    this_fingerprint = Fingerprint.valid().get(id=hit['hitcount__object_pk'])

                    if eprofile.restricted:
                        try:
                            allowed = RestrictedUserDbs.objects.get(user=request.user, fingerprint=this_fingerprint)
                        except RestrictedUserDbs.DoesNotExist:
                            restricted = RestrictedGroup.hashes(request.user)

                            if this_fingerprint.fingerprint_hash not in RestrictedGroup.hashes(request.user):
                                continue

                    list_viewed.append(
                        {
                            'hash': this_fingerprint.fingerprint_hash,
                            'name': this_fingerprint.findName(),
                            'count': hit['total_hits']
                        })
                    i+=1
                    if i == 10:
                        break

                except Fingerprint.DoesNotExist:
                    print "-- Error on hitcount for fingerprint with id "+hit['hitcount__object_pk']

            response = Response({'mostviewed': list_viewed}, status=status.HTTP_200_OK)

        else:
            response = Response({}, status=status.HTTP_403_FORBIDDEN)
        return response
def get_matrix_data(db_type, qset_post, user, flat=False):

    # generate a mumbo jumbo digest for this combination of parameters, to be used as key for caching purposes
    string_to_be_hashed = "dbtype" + str(db_type)

    for post in qset_post:
        string_to_be_hashed += "qs" + post

    string_to_be_hashed += "_hashed_" + str(flat)

    hashed = hashlib.sha256(string_to_be_hashed).hexdigest()

    titles = None
    answers = None

    cached = cache.get(hashed)

    if cached != None:
        #print "cache hit"
        (titles, answers) = cached

    else:
        #print "need for cache"
        qset_int = []
        for qs in qset_post:
            qset_int.append(int(qs))

        qset = QuestionSet.objects.filter(id__in=qset_int)

        fingerprints = Fingerprint.objects.filter(questionnaire__id=db_type)

        try:
            eprofile = EmifProfile.objects.get(user=user)

            if eprofile.restricted == True:
                hashes = RestrictedGroup.hashes(user)

                others = RestrictedUserDbs.objects.filter(user=user)

                for db in others:
                    hashes.add(db.fingerprint.fingerprint_hash)

                fingerprints = fingerprints.filter(fingerprint_hash__in=hashes)

        except EmifProfile.DoesNotExist:
            print "-- ERROR: Couldn't get emif profile for user"

        (titles, answers) = creatematrixqsets(db_type,
                                              fingerprints,
                                              qset,
                                              flat=flat)

        cache.set(hashed, (titles, answers), 720)  # 12 hours of cache

    return (hashed, titles, answers)
Beispiel #5
0
def get_matrix_data(db_type, qset_post, user, flat=False):

    # generate a mumbo jumbo digest for this combination of parameters, to be used as key for caching purposes
    string_to_be_hashed = "dbtype"+str(db_type)

    for post in qset_post:
        string_to_be_hashed+="qs"+post

    string_to_be_hashed += "_hashed_"+str(flat)

    hashed = hashlib.sha256(string_to_be_hashed).hexdigest()

    titles = None
    answers = None

    cached = cache.get(hashed)

    if cached != None:
        #print "cache hit"
        (titles, answers) = cached

    else :
        #print "need for cache"
        qset_int = []
        for qs in qset_post:
            qset_int.append(int(qs))


        qset = QuestionSet.objects.filter(id__in=qset_int)

        fingerprints = Fingerprint.objects.filter(questionnaire__id=db_type)

        try:
            eprofile = EmifProfile.objects.get(user=user)

            if eprofile.restricted == True:
                hashes = RestrictedGroup.hashes(user)


                others = RestrictedUserDbs.objects.filter(user=user)

                for db in others:
                    hashes.add(db.fingerprint.fingerprint_hash)

                fingerprints = fingerprints.filter(fingerprint_hash__in=hashes)

        except EmifProfile.DoesNotExist:
            print "-- ERROR: Couldn't get emif profile for user"

        (titles, answers) = creatematrixqsets(db_type, fingerprints, qset, flat=flat)

        cache.set(hashed, (titles, answers), 720) # 12 hours of cache

    return (hashed, titles, answers)