Skip to content
Hayden Carpenter edited this page Dec 11, 2020 · 2 revisions

Decouple and Environmental Variables

We use a python module called decouple to hide values in the code which should be kept private. For example, the private OAuth key.

This way we can push the code to github, and have it work on the server, but our data can't be compromised because the hidden keys cannot be found in it, there's only a reference.

The actual keys are stored in a file called .env, which is in the same directory as the GitHub project. To make sure that file isn't uploaded to GitHub, we have a .gitignore file which removes it from the list of files committed.

If you ever need to find a private key, check that file.

If you ever need to hide a value add something along these lines to your python code.

from decouple import config
...

name_of_key = config('whatever key you have')

and add

name_of_key=whatever key you have

to the .env file.

Make sure you import config from decouple.

Models

Django Documentation: https://docs.djangoproject.com/en/3.1/topics/db/models/

The reason we initially used Django to create Scout Janssen is that we wanted an easy way to submit form data to a database without having to write PHP.
Django's model system functions as a way to create database tables and elements.
Django defines a model as:

the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table.

In models.py, we create classes for each of our models.
Here is an example of the Event model, which is used to create a table of Events in our database.

class Event(models.Model):
    name = models.CharField(max_length = 1000, primary_key = True)
    start_date = models.DateField()
    end_date = models.DateField()
    year = models.IntegerField()

    def __str__(self):
        return (str(self.name) + " " + str(self.year))

Fields

This model has a bunch of fields. Fields are what data we're going to add to our table. What columns, to be precise.
These fields have a type which is used to discern what value is supposed to be inputted into that field.

name = models.CharField(max_length = 1000, primary_key = True)

for example creates a text field with a max length of 1000. The property primary_key is set to False on default, but here we enable it as true. The primary key is the only thing that has to be unique in the model. It is what Django will use to index the data in the database. It is the identifier.

On default, the primary key is set to an id which is automatically created. However we force it to be assigned to a different value, in this case the name of the team, because there can only be one instance of that team. If another team of the same name is created, we want the first instance of the team to be overwritten.

Other field options, like primary_key, can be found on the Django documentation.

Foreign keys

Foreign keys are a way to include other models as a field of our model. Instead of requiring a value there, we require a link (a reference) to an instance of a model.

These relationships can be recursive.
That means a Report object might have a reference (through a ForeignKey) to certain Match, and that Match object might have a reference back to the Report. There's an infinite loop.

ManyToMany Relationships

I'm not sure if we're using any of these, but they're essentially a way to link multiple foreign keys to the same field. Like a list, if you will, except that list is handled as a Django QuerySet. More information on QuerySets can be found here.

__str__() function

In our admin panel we can view our models and the data assigned to them. These data values, when viewed from the admin panel, need to be of type 'string'. Because of this, we use the __str__() function in each of our models to tell the site how to preview that model on the page. For example:

def __str__(self):
        return str(self.scouter + ", match: " + str(self.match.number))

Will render something like "Hayden, match: 1" if the instance of the model has its scouter value as 'Hayden' and its linked match's number as 1.

Settings

Django Documentation: https://docs.djangoproject.com/en/3.1/topics/settings/

The settings.py file is where you change Django Project settings. This can be found in the main directory of the project, 'Scout-Janssen/scoutjanssen'.

If you ever want to install plugins to Django, you do so here.

This is also where we connect to the database and configure OAuth.

Forms

There are actually multiple ways to handle forms in Django, but we use ModelForms. These are essentially forms which are assigned to a specific model. All we have to do is pass the model to use and the fields to take from that model in order to create a form.

class ScoutingForm(forms.ModelForm):
    class Meta:
        model = Report
        fields = [
            'scouter', 
            'team',
            'match',
            'onePointMadeTele',
            'onePointMissedTele',
            'twoPointMadeTele',
            'twoPointMissedTele',
            'onePointMadeAuto',
            'onePointMissedAuto',
            'twoPointMadeAuto',
            'twoPointMissedAuto',
            'wheelTurn',
            'wheelColor',
            'climb',
            'park',
            'climbAssist',
            'balanceResponsibility',
            'initiationLine', 
            'timeOnDefense',
            'bot1Defense',
            'bot2Defense',
            'bot3Defense',
            'timeInoperable',
            'notes',
            'estimate3pt',
            'mechanicalIssues',
            'connectionIssues',
            ]
    def __init__(self, *args, **kwargs):
        super(ScoutingForm, self).__init__(*args, **kwargs)
        #eventObject = Event.objects.filter(name = "gagr2019")
        currentEvent = CurrentScouting.objects.filter(pk = 1).values_list('event_id')
        self.fields['match'].queryset = Match.objects.filter(event_id = currentEvent[0]).order_by('number')

We use the __init__() method to filter the dropdown box that gets put on the form.
We want to filter by event, and so we use a queryset to only get matches which are under the current event.\

We also use the .order_by() method to sort the matches by number.

Views

Templating

Admin

Clone this wiki locally