How to use the typer.models.Die.objects.filter function in typer

To help you get started, we’ve selected a few typer examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github andrew-gardner / django-monkeys / typer / views.py View on Github external
def dieInstructionsView(request, dieName):
    """
    A view that simply displays the instructions image and instruction text
    for the given Die.
    """
    dieObject = Die.objects.filter(name=dieName)[0]
    instructions = dieObject.instructions

    # Find all the instances of images in our special markup [[[IMAGE_NAME (WIDTH HEIGHT)]]]
    m = re.finditer(r'\[\[\[(.*?)\]\]\]', instructions)
    for foundMatch in m:
        noBrackets = foundMatch.group(0).replace("[", "").replace("]", "")
        splitData = noBrackets.split()
        imageName = splitData[0]
        if len(splitData) > 1:
            imageWidth = int(splitData[1])
            imageHeight = int(splitData[2])
        imageObject = dieObject.instructionsimage_set.filter(Q(name=imageName))
        imageUrl = staticfiles.static(imageObject[0].image.url)
        if len(splitData) > 1:
            refText = """<img height="%d" width="%d" src="%s">""" % (imageUrl, imageWidth, imageHeight)
        else:
github andrew-gardner / django-monkeys / typer / views.py View on Github external
def adminSummaryView(request, dieName, imageId):
    """
    This administrative view displays a summary of all the entered information
    for a given Die and DieImage.  One can also do rudimentary changes to the
    entered data and compare results.
    """
    dieObject = Die.objects.filter(name=dieName)[0]
    dieImage = DieImage.objects.filter(id=imageId)[0]
    allAvailableFields = TypedDie.objects.filter(Q(dieImage__die=dieObject) & Q(dieImage__id=imageId))

    if request.method == "POST":
        # Pull which clear button was pressed
        clearButtonsPressed = [k for k,v in request.POST.items() if k.startswith('clearButton')]
        if len(clearButtonsPressed) > 0:
            firstClearButtonPressed = clearButtonsPressed[0]
            clearNumberRe = re.search(r'(\d+)$', firstClearButtonPressed)
            clearNumber = int(clearNumberRe.group(0))
            workingField = allAvailableFields[clearNumber]

            # Clear the form
            workingField.submitter = None
            workingField.submitDate = None
            workingField.typedField = ""
github andrew-gardner / django-monkeys / typer / views.py View on Github external
def adminStatisticsView(request, dieName):
    """
    """
    dieObject = Die.objects.filter(name=dieName)[0]

    # Get how many fields and how many have been typed
    allFields = TypedDie.objects.filter(Q(dieImage__die=dieObject))
    typedFields = TypedDie.objects.filter(Q(dieImage__die=dieObject) & ~Q(typedField=""))

    # Get a list of who's on first
    scoreboard = list()
    for user in User.objects.all():
        userTyped = TypedDie.objects.filter(~Q(typedField="") & Q(submitter=user) & Q(dieImage__die=dieObject))
        scoreboard.append( (user, len(userTyped)) )
    sortedScores = sorted(scoreboard, key=lambda tup: tup[1], reverse=True)

    context = {
                  'die' : dieObject,
                  'allFieldCount' : len(allFields),
                  'allTypedCount' : len(typedFields),
github andrew-gardner / django-monkeys / typer / views.py View on Github external
def dieSpecificUserStatisticsView(request, dieName, userName):
    """
    A view that shows interesting statistics for the user specified in the url.
    """

    # If the given username isn't the logged-in user, see if it's staff.  If not, go no further.
    if userName != request.user.username and not request.user.is_staff:
        return HttpResponse("Only administrators can access user statistics directly")

    # Get the User object from the username
    dieObject = Die.objects.filter(name=dieName)[0]
    specifiedUser = None
    for user in User.objects.all():
        if userName == user.username:
            specifiedUser = user
    if specifiedUser is None:
        return HttpResponse("The user %s does not exist." % userName)

    # Carry on
    userTypedTheseFields = TypedDie.objects.filter(Q(dieImage__die=dieObject) &amp; Q(submitter=specifiedUser))

    # How does this user's entry count compare to the others?
    userEntryTupleList = list()
    for user in User.objects.all():
        if user == specifiedUser:
            continue
        otherUserTypedFields = TypedDie.objects.filter(Q(dieImage__die=dieObject) &amp; Q(submitter=user))
github andrew-gardner / django-monkeys / typer / views.py View on Github external
def adminSummaryHomeView(request, dieName):
    """
    An administrative view that displays a list of images and info about
    the typed information for each for the given Die.  The administrator
    can see how much data has been entered for each image and click on
    the image to open a new view showing more details.
    """
    dieObject = Die.objects.filter(name=dieName)[0]
    allAvailableDieImages = DieImage.objects.filter(Q(die=dieObject))
    allApplicableTypedDies = TypedDie.objects.filter(Q(dieImage__die=dieObject))

    # Count all the entered fields for this die image (TODO: There must be a more Pythonic way to do this)
    totalFields = 0
    totalCompletedFields = 0
    dieIsCompleted = list()
    dieImageEntryCounts = list()
    # TODO: This is very slow right now - seems to be 2/3 python and 1/3 html - could be sped up with a Paginator
    for di in allAvailableDieImages:
        typedFields = allApplicableTypedDies.filter(Q(dieImage=di))
        completedFieldCount = 0
        for tf in typedFields:
            if tf.completed():
                completedFieldCount += 1
        totalFields += len(typedFields)
github andrew-gardner / django-monkeys / typer / views.py View on Github external
def indexView(request, dieName):
    """
    This view displays a randomly choosen DieImage for a given Die.  Secret
    data about which die has been randomly chosen passes through to the POST
    method since this view decides it dynamically.

    Note:
    allAvailableFields = allAvailableFields.exclude(Q(dieImage=tuht.dieImage))
    Resulted in large AND NOT query after adding many entries
    Instead two separate queries and subtract out manually
    """
    userIsStaff = request.user.is_staff

    if request.method == 'GET':
        # Standard page display
        dieObject = Die.objects.filter(name=dieName)[0]
        allAvailableFields = TypedDie.objects.filter(Q(typedField="") &amp; Q(dieImage__die=dieObject))

        thingsUserHasTyped = TypedDie.objects.filter(~Q(typedField="") &amp; Q(submitter=request.user) &amp; Q(dieImage__die=dieObject))
        setTyped = [td.dieImage_id for td in thingsUserHasTyped]
        usableFields = list(filter(lambda x: x.dieImage_id not in setTyped, allAvailableFields))
        #print 'avail', len(allAvailableFields)
        #print 'typed', len(setTyped)
        #print 'usable', len(usableFields)

        if not usableFields:
            return HttpResponse("All fields have been typed for this die. Check back later to see if there are other dies to type.")

        # Choose a random field to display
        randomField = random.randint(0, len(usableFields)-1)
        randomId = usableFields[randomField].id