Example #1
0
def create_face_picture(url, facebook_id, x, y, width, height):
    global FacePictures
    raw = get_raw_picture_by(facebook_id)
    face_id = uuid.uuid4().hex
    doc = {_FACE_ID: face_id,
           'url': url,
           'face_x': x,
           'face_y': y,
           'face_width': width,
           'face_height': height,
           'source_width': raw.get('width'),
           'source_height': raw.get('height'),
           'facebook_id': facebook_id,
           'datetime': datetime.datetime.now(),
           'nb_votes': 0,
           'nb_votes_total': 0,
           'tag': NOTTAGGED,
           'tags': {},
           'views': 0,
           'views_total': 0,
           'score': 0,
           'score_total': 0,
           'nb_favorited': 0,
           'favorite_votes': 0,
           'favorite_votes_total': 0,
           'bonusmalus': [],
           'bonusmalus_total': [],
           'has_won': False}
    FacePictures.insert(doc)
    return get_face_from(face_id)
Example #2
0
def create_face_picture(url, facebook_id, x, y, width, height):
    global FacePictures
    raw = get_raw_picture_by(facebook_id)
    face_id = uuid.uuid4().hex
    doc = {
        _FACE_ID: face_id,
        'url': url,
        'face_x': x,
        'face_y': y,
        'face_width': width,
        'face_height': height,
        'source_width': raw.get('width'),
        'source_height': raw.get('height'),
        'facebook_id': facebook_id,
        'datetime': datetime.datetime.now(),
        'nb_votes': 0,
        'nb_votes_total': 0,
        'tag': NOTTAGGED,
        'tags': {},
        'views': 0,
        'views_total': 0,
        'score': 0,
        'score_total': 0,
        'nb_favorited': 0,
        'favorite_votes': 0,
        'favorite_votes_total': 0,
        'bonusmalus': [],
        'bonusmalus_total': [],
        'has_won': False
    }
    FacePictures.insert(doc)
    return get_face_from(face_id)
Example #3
0
def add_tag_to_face(face_id, tag_id, user):
    # check if user already tagged with same tag
    global TaggedFace
    global FacePictures
    if TaggedFace.find({'from': user.get('ident'),
                       _FACE_ID: face_id,
                       'tag': tag_id}).count():
        return False
    # create tag object
    TaggedFace.insert({'from': user.get('ident'),
                       'from_type': user.get('type'),
                       _FACE_ID: face_id,
                       'tag': tag_id,
                       'datetime': now()})

    face = get_face_from(face_id)

    # update face tags list
    face.update({'tags': face_tags_dict_for(face=face)})

    # maybe update main tag of face
    face.update({'tag': main_tag_for(face=face)})

    # save face
    FacePictures.save(face)

    # maybe update winner cache
    update_winner_cache_if_winner(face)
Example #4
0
def get_gallery_faces(sort_order='-chrono', with_tags=None,
                      limit=9, user_id=None, page=0):
    '''

        sort_order:
            'chrono', '-chrono': chronological order
            'score', '-score': sorted by score

        add if user_id has tagged each picture'''
    global FacePictures
    if sort_order == '-chrono':
        sort_query = {'datetime': -1}
    elif sort_order == 'chrono':
        sort_query = {'datetime': 1}
    elif sort_order == '-score':
        sort_query = {'score': -1}
    elif sort_order == 'score':
        sort_query = {'score': 1}
    else:
        # default soring order
        sort_query = {'datetime': -1}

    query = {'tag': {'$in': with_tags}}

    return FacePictures.find(query,
                             sort=sort_query,
                             limit=limit,
                             skip=page * limit)
Example #5
0
def get_face_from(face_id, update_views=False):
    ''' return a FacePictures obj from `face_id` '''
    global FacePictures
    face = FacePictures.find_one({_FACE_ID: face_id})
    if update_views:
        add_one_view_to(face)
    return face
Example #6
0
def get_random_face(extra_query=None, update_views=False):
    global FacePictures
    cursor = FacePictures.find(extra_query)
    nb_faces = cursor.count()
    rand = random.randint(0, nb_faces - 1)
    try:
        face = cursor[rand:rand][0]
        add_one_view_to(face)
        return face
    except KeyError:
        return None
Example #7
0
def get_faces_for_raw_picture(facebook_id):
    return FacePictures.find({_FACEBOOK_ID: facebook_id})
Example #8
0
def delete_face(picture_id):
    global FacePictures
    FacePictures.remove({_FACE_ID: picture_id})
    return True
Example #9
0
def get_faces_for_raw_picture(facebook_id):
    return FacePictures.find({_FACEBOOK_ID: facebook_id})
Example #10
0
def delete_face(picture_id):
    global FacePictures
    FacePictures.remove({_FACE_ID: picture_id})
    return True
Example #11
0
def get_current_winner(from_cache=True):
    if not from_cache:
        global FacePictures
        return FacePictures.find_one(sort={'score': -1}, limit=1)
    return get_from_cache('winner')
Example #12
0
def update_face(face, data):
    global FacePictures
    face.update(data)
    FacePictures.save(face)