예제 #1
0
파일: views.py 프로젝트: dlukes/kudlanka
def list():
    uid = session["user_id"]
    user = User.objects(id=uid).first()
    segs = []
    for i, sid in enumerate(user.segs):
        seg = Seg.objects(sid=sid).first()
        utt = []
        flag_seg = False
        for pos in seg.utt:
            utt.append(pos["word"])
            if pos.get("flags", {}).get(uid, False):
                flag_seg = True
        utt = " ".join(utt)
        segs.append(dict(i=i + 1, sid=seg.sid, oral=seg.oral, utt=utt,
                         flag_seg=flag_seg))
    total = 0
    batches = []
    for batch, batch_size in enumerate(user.batches):
        # all batches except the last one are by definition done
        batches.append(dict(batch=batch + 1, assigned=batch_size,
                            done=batch_size, remaining=0))
        total += batch_size
    # correct stats for the last batch
    penalty = 1 if user.assigned else 0
    remaining = total - len(segs) + penalty
    done = user.batches[-1] - remaining
    batches[-1].update(done=done, remaining=remaining)
    return render_template("list.html", segs=segs, batches=batches)
예제 #2
0
파일: views.py 프로젝트: dlukes/kudlanka
def admin():
    users = []
    for user in User.objects():
        if user.batches:
            penalty = 1 if user.assigned else 0
            done = len(user.segs) - penalty
            users.append(dict(assigned=sum(user.batches),
                              done=done, id=user.email))
    form = AssignBatchForm(request.form)
    form.user.choices = [(str(u.id), u.email) for u
                         in User.objects(email__nin=["admin"])]
    if form.validate_on_submit():
        User.objects(
            id=form.user.data).first().modify(
            push__batches=form.batch_size.data)
        flash("Nová várka úspěšně přidána.", "success")
        return redirect(url_for("admin"))
    return render_template("admin.html", users=users, form=form)
예제 #3
0
파일: api.py 프로젝트: dlukes/kudlanka
def get_user_info():
    uid = session["user_id"]
    user = User.objects(id=uid).first()
    # there's a difference between a user with len(user.segs) == X and no seg
    # assigned (that one's done) and a user with len(user.segs) == X and a
    # pending assignment (that one's still got one seg to go)
    penalty = 1 if user.assigned else 0
    done = len(user.segs) - sum(user.batches[:-1]) - penalty
    return dict(done=done, max=user.batches[-1])
예제 #4
0
파일: api.py 프로젝트: dlukes/kudlanka
 def post(self, sid):
     request.get_data()
     utt = request.json
     uid = session["user_id"]
     user = User.objects(id=uid).first()
     seg = Seg.objects(sid=sid).first()
     if uid not in seg.users:
         abort(403, messages=[["danger", SegSid.edit_err]])
     if not len(seg["utt"]) == len(utt):
         abort(400, messages=[["danger", SegSid.len_err.format(sid)]])
     for i, dbpos, postpos in zip(range(1, len(seg["utt"]) + 1), seg["utt"], utt):
         if not postpos.get("lemma", False):
             abort(400, messages=[["warning", SegSid.miss_l_err.format(i)]])
         if not postpos.get("tag", False):
             abort(400, messages=[["warning", SegSid.miss_t_err.format(i)]])
         if dbpos["word"] == postpos["word"]:
             # only save flag if it's True (it might be present, but set to
             # False)
             if postpos.get("flag"):
                 dbpos.setdefault("flags", {})[uid] = True
                 # only save note if flag was True (a note might be present
                 # along with a False flag)
                 if "note" in postpos:
                     dbpos.setdefault("notes", {})[uid] = postpos["note"]
             else:
                 # the flag may also have been removed after having been
                 # added in an earlier POST; make sure it's gone by deleting
                 # it
                 dbpos.setdefault("flags", {}).pop(uid, None)
                 dbpos.setdefault("notes", {}).pop(uid, None)
             if "pool" not in dbpos:
                 continue
             elif postpos["lemma"] in dbpos["pool"]:
                 dbpos.setdefault("lemmas", {})[uid] = postpos["lemma"]
             else:
                 abort(400, messages=[["danger", SegSid.err]])
             if postpos["tag"] in dbpos["pool"].get(postpos["lemma"], {}):
                 dbpos.setdefault("tags", {})[uid] = postpos["tag"]
             else:
                 abort(400, messages=[["danger", SegSid.err]])
         else:
             abort(400, messages=[["danger", SegSid.word_err.format(postpos["word"], dbpos["word"], i, sid)]])
     seg.modify(utt=seg["utt"])
     # only remove segment assignment if the user is currently posting their
     # most recently asssigned segment, which means they're ready to be
     # assigned a new one (otherwise they're just re-editing a segment from
     # their history, in which case we want them to keep the segment they
     # have been assigned)
     if user.assigned == seg.sid:
         user.modify(assigned=None)
     return {}
예제 #5
0
파일: api.py 프로젝트: dlukes/kudlanka
 def get(self):
     max_done = app.config["MAX_DISAMB_PASSES"]
     uid = session["user_id"]
     user = User.objects(id=uid).first()
     if user.assigned:
         seg = Seg.objects(sid=user.assigned).first().to_mongo()
         return seg
     # if user.assigned == "", then the user has completed all segs in
     # user.segs and might be done with their batch
     elif len(user.segs) >= sum(user.batches):
         abort(404, messages=[["success", SegAssign.done]])
     else:
         # assign the already rated (i.e. the most rated → order_by) segs
         # first
         seg = Seg.objects(ambiguous=True, users_size__lt=max_done, users__nin=[uid]).order_by("-users_size").first()
         try:
             users = set(seg.users + [uid])
             users_size = len(users)
             seg.modify(users=users, users_size=users_size)
             user.modify(assigned=seg.sid, add_to_set__segs=seg.sid)
         except AttributeError:
             # seg is None
             abort(400, messages=[["danger", SegAssign.noseg_err]])
         return seg.to_mongo()
예제 #6
0
파일: views.py 프로젝트: dlukes/kudlanka
 def validate_user(self, field):
     user = User.objects(id=field.data).first()
     if len(user.segs) < sum(user.batches):
         raise ValidationError("Uživatel má již várku přidělenou.")