How to use the typer.forms.MonkeyTyperForm 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 imageInput(request, fieldId, error=None, fieldData=None):
    """
    Helper function for the indexView - responsible for creating the page
    that features the input interface & possible error messages.
    """
    # Recover the requested die image and its corresponding die
    dieField = get_object_or_404(TypedDie, id=fieldId)
    di = dieField.dieImage
    d = di.die

    # Populate the form with the raw data from the previous submit if there was an error
    if error and fieldData:
        form = MonkeyTyperForm(instance=dieField, initial={'typedField': fieldData})
    else:
        form = MonkeyTyperForm(instance=dieField)

    # Prune off just the filename from the dieImage url
    dieImageBasename = os.path.basename(dieField.dieImage.image.url)

    # Display the input page
    context = {
                  'die': d,
                  'dieImage': di,
                  'dieImageBasename': dieImageBasename,
                  'typedDie': dieField,
                  'form' : form,
                  'error' : error,
              }
    return render(request, 'typer/imageInput.html', context)
github andrew-gardner / django-monkeys / typer / views.py View on Github external
if not form.is_valid():
                # TODO: Do something interesting here (really the admin should know better).
                #       As for now, it just reverts your changes.
                pass
            else:
                workingField.submitter = request.user
                workingField.submitDate = timezone.now()
                workingField.save()


    # Build the arrays that we want to display
    submitterArray = list()
    populatedForms = list()
    submitTimeArray = list()
    for aaf in allAvailableFields:
        populatedForms.append(MonkeyTyperForm(instance=aaf, initial={'typedField': aaf.typedField}))
        submitterArray.append(aaf.submitter)
        if aaf.submitDate:
            submitTimeArray.append(str(aaf.submitDate)[2:10] + "<br>" + str(aaf.submitDate)[12:16])
        else:
            submitTimeArray.append("N/A<br>N/A<br>")

    # Prune off just the filename from the dieImage url
    dieImageBasename = os.path.basename(dieImage.image.url)

    context = {
                  'die': dieObject,
                  'dieImage': dieImage,
                  'dieImageBasename': dieImageBasename,
                  'dieInfoArray': zip(populatedForms, submitterArray, submitTimeArray, range(len(submitTimeArray)))
              }
github andrew-gardner / django-monkeys / typer / views.py View on Github external
def imageInput(request, fieldId, error=None, fieldData=None):
    """
    Helper function for the indexView - responsible for creating the page
    that features the input interface & possible error messages.
    """
    # Recover the requested die image and its corresponding die
    dieField = get_object_or_404(TypedDie, id=fieldId)
    di = dieField.dieImage
    d = di.die

    # Populate the form with the raw data from the previous submit if there was an error
    if error and fieldData:
        form = MonkeyTyperForm(instance=dieField, initial={'typedField': fieldData})
    else:
        form = MonkeyTyperForm(instance=dieField)

    # Prune off just the filename from the dieImage url
    dieImageBasename = os.path.basename(dieField.dieImage.image.url)

    # Display the input page
    context = {
                  'die': d,
                  'dieImage': di,
                  'dieImageBasename': dieImageBasename,
                  'typedDie': dieField,
                  'form' : form,
                  'error' : error,
              }
    return render(request, 'typer/imageInput.html', context)
github andrew-gardner / django-monkeys / typer / views.py View on Github external
# Clear the form
            workingField.submitter = None
            workingField.submitDate = None
            workingField.typedField = ""
            workingField.save()
            allAvailableFields[clearNumber].refresh_from_db()

        # Pull which save button was pressed
        saveButtonsPressed = [k for k,v in request.POST.items() if k.startswith('saveButton')]
        if len(saveButtonsPressed) > 0:
            firstSaveButtonPressed = saveButtonsPressed[0]
            saveNumberRe = re.search(r'(\d+)$', firstSaveButtonPressed)
            saveNumber = int(saveNumberRe.group(0))
            workingField = allAvailableFields[saveNumber]

            form = MonkeyTyperForm(request.POST, instance=workingField)
            if not form.is_valid():
                # TODO: Do something interesting here (really the admin should know better).
                #       As for now, it just reverts your changes.
                pass
            else:
                workingField.submitter = request.user
                workingField.submitDate = timezone.now()
                workingField.save()


    # Build the arrays that we want to display
    submitterArray = list()
    populatedForms = list()
    submitTimeArray = list()
    for aaf in allAvailableFields:
        populatedForms.append(MonkeyTyperForm(instance=aaf, initial={'typedField': aaf.typedField}))
github andrew-gardner / django-monkeys / typer / forms.py View on Github external
def __init__(self, *args, **kwargs):
        """
        Standard initialization routine, but with an explicit definition of
        the typedField's width and height based on database info.
        """
        super(MonkeyTyperForm, self).__init__(*args, **kwargs)
        self.fields['typedField'].widget = forms.Textarea(attrs={'cols': self.instance.dieImage.bitWidth+2,
                                                                 'rows': self.instance.dieImage.bitHeight+2,
                                                                 'style': 'font-family:monospace;'})
github andrew-gardner / django-monkeys / typer / views.py View on Github external
# Choose a random field to display
        randomField = random.randint(0, len(usableFields)-1)
        randomId = usableFields[randomField].id

        # Display the random page
        return imageInput(request, randomId)

    else:
        # Data has been POSTed
        # Pull the previous die field out of the form's hidden data
        dieId = int(request.POST['dieField'])
        dieField = TypedDie.objects.filter(id=dieId)[0]
        someoneCompletedTheFieldBeforeYou = dieField.completed()

        # Create a form object from the post data and knowledge of which dieField we're dealing with
        form = MonkeyTyperForm(request.POST, instance=dieField)

        # Insure input is valid (if it is, data is stored in dieField, but not saved to the database)
        # TODO: There is a very good chance exceptions aren't being used correctly here
        if not form.is_valid():
            # Redisplay the same page but with an error message
            error = form.errors.as_data()['typedField'][0].message
            return imageInput(request, dieField.id, error, form.data['typedField'])

        # If the strange situation occurred where someone snuck in and completed the field before you
        if someoneCompletedTheFieldBeforeYou:
            # TODO: Convert to django/Python logging
            dieObject = dieField.dieImage.die
            dieImageObject = dieField.dieImage
            print("User %s attempted to submit %s die image %s typed id %d, but someone else did first" %
                  (request.user,
                   dieObject,