Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Creating functional classes in Django that provide deployment pipeline functionality.

I am learning Django, so please correct any issues with my way of thinking here. To start, I am using the Django Rest framework and I'm not interested in just creating a list of items, which is all that I can find examples of on the entire Internet...

I will have some pretty large functional classes that will POST and GET data to and from a pipeline server (TFS to be specific) and obviously, I'll need to insert this logic into a file within my app, I'm not just displaying things from my database here.

So, my question is, where does this go within my app? (let's say that I have a python script called getTFSbuild.py that does a bunch of stuff that I need) my understanding is that the "Django way" is to keep models thick.. does that mean that all code logic to make requests and interact with the pipeline server should end up in my models.py?

It's kinda frustrating that everyone else seems to just be listing furniture or some other sales item from their database. Which does not really help me very much... I need to know what a very large web app would look like (one that has a lot of logic and not just a stupid list of red and blue chairs).

I'm a little fuzzy on what you are trying to do. As I understand it you want to use a HTTP REST service authored in python/Django to talk to Team Foundation Server? What would be the client to the Django app? Why would the client not talk directly to TFS? Have you seen https://devopshq.github.io/tfs/ for the code to talk to TFS?

3 Answers

Myers Carpenter Here is what I have so far:

import requests
from requests_ntlm import HttpNtlmAuth
import json

class tfs:
    def __init__(self, host, project, apiVersion, user, passwd):
        self.host = host
        self.project = project
        self.apiVersion = apiVersion
        self.user = user
        self.passwd = passwd

    def projectsList(self):
        tfsApi = f'http://{self.host}/{self.project}/_apis/projects?api-version={self.apiVersion}' # Get API information
        tfsResponse = requests.get(tfsApi,auth=HttpNtlmAuth(self.user,self.passwd))
        if (tfsResponse.ok):
            tfsResponse = json.dumps(tfsResponse.json(), indent=4)
            print(tfsResponse)
        else:
            tfsResponse.raise_for_status()

tfsGet = tfs('IP_ADDRESS', 'PROJECT', '3.2-preview', 'USER', 'PASS')
tfsGet.projectsList()

The end goal is to have my frontend list info about my TFS project, using a differnt URL I can also list info about builds and releases.

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,717 Points

There is no rule that says you can't separate business rules out into a separate module. There is no rule that requires you to keep all your ORM models in models.py. It is just a good convention.

Circular imports can become a problem if you aren't careful. Creating tests on models and business rules require a little more work, but shouldn't be too much of a challenge if you've keep things factored in a logical fashion and kept up with your notes/documentation.

My question was more so along the line of, "how would I get my backend (Django REST API) to actually run this code so that I can call it and have it list the contents of JSON output from my frontend application". I will figure out the frontend requirements later (because its angularJS), but what are the Django requirements to make this happen? Let's say that I have a working script called tfsout.py in the tfs app directory. I need to have all the json output display on the from the root API URL for now (until I get the frontend requirements).

Jeff Muday
MOD
Jeff Muday
Treehouse Moderator 28,717 Points

That sounds kind of fun, but I'd be a little out of my depth to comment. I have almost no experience with Angular.