diff options
| author | Bobby <[email protected]> | 2023-09-23 01:42:29 -0400 |
|---|---|---|
| committer | Bobby <[email protected]> | 2023-09-23 01:42:29 -0400 |
| commit | 8b6812816f0610f0942cfcd4eb70333b1a8e8a3f (patch) | |
| tree | 184c97135df67d202fe24681423ffa25ba642d73 /dev_status/views.py | |
| parent | ee777e041132206014b6be397d7c8d9a1d015265 (diff) | |
| download | thatcomputerscientist-8b6812816f0610f0942cfcd4eb70333b1a8e8a3f.tar.xz thatcomputerscientist-8b6812816f0610f0942cfcd4eb70333b1a8e8a3f.zip | |
Removed Solitude + Refactor
Diffstat (limited to 'dev_status/views.py')
| -rw-r--r-- | dev_status/views.py | 112 |
1 files changed, 61 insertions, 51 deletions
diff --git a/dev_status/views.py b/dev_status/views.py index 0a7adf7f..c656d2b2 100644 --- a/dev_status/views.py +++ b/dev_status/views.py @@ -7,32 +7,30 @@ from dotenv import load_dotenv from github import Github load_dotenv() -g = Github(os.getenv('GH_TOKEN')) +g = Github(os.getenv("GH_TOKEN")) + # Create your views here. def home(request): - page = request.GET.get('page') or 1 - items = request.GET.get('items') or 10 - sort = request.GET.get('sort') or 'updated' - direction = request.GET.get('direction') or 'desc' - search = request.GET.get('search') or '' + page = request.GET.get("page") or 1 + items = request.GET.get("items") or 10 + sort = request.GET.get("sort") or "updated" + direction = request.GET.get("direction") or "desc" + search = request.GET.get("search") or "" context = {} sort_map = { - 'updated': 'UPDATED_AT', - 'stars': 'STARGAZERS', - 'pushed': 'PUSHED_AT', - 'created': 'CREATED_AT', - 'name': 'NAME' - } - direction_map = { - 'desc': 'DESC', - 'asc': 'ASC' + "updated": "UPDATED_AT", + "stars": "STARGAZERS", + "pushed": "PUSHED_AT", + "created": "CREATED_AT", + "name": "NAME", } - + direction_map = {"desc": "DESC", "asc": "ASC"} + # make request to github api to get page of repos and total count of repos - url = 'https://api.github.com/graphql' - headers = {'Authorization': 'token ' + os.getenv('GH_TOKEN')} - user = 'luciferreeves' + url = "https://api.github.com/graphql" + headers = {"Authorization": "token " + os.getenv("GH_TOKEN")} + user = "luciferreeves" query = """ query {{ @@ -53,51 +51,63 @@ def home(request): }} }} }} - """.format(user=user, sort=sort_map[sort], direction=direction_map[direction]) - data = requests.post(url, json={'query': query}, headers=headers).json() + """.format( + user=user, sort=sort_map[sort], direction=direction_map[direction] + ) + data = requests.post(url, json={"query": query}, headers=headers).json() - repos = [{'name': repo['node']['name'], 'description': repo['node']['description']} for repo in data['data']['user']['repositories']['edges']] - total_count = data['data']['user']['repositories']['totalCount'] + repos = [ + {"name": repo["node"]["name"], "description": repo["node"]["description"]} + for repo in data["data"]["user"]["repositories"]["edges"] + ] + total_count = data["data"]["user"]["repositories"]["totalCount"] - - context['search'] = search + context["search"] = search if search: - context['repos'] = [repo for repo in repos if search.lower() in repo['name'].lower() or search.lower() in repo['description'].lower()] - context['total_count'] = len(context['repos']) + context["repos"] = [ + repo + for repo in repos + if search.lower() in repo["name"].lower() + or search.lower() in repo["description"].lower() + ] + context["total_count"] = len(context["repos"]) else: - context['repos'] = repos - context['total_count'] = total_count + context["repos"] = repos + context["total_count"] = total_count # calculate pagination - context['page'] = int(page) - context['items'] = int(items) - context['sort'] = sort - context['direction'] = direction - context['num_pages'] =math.ceil(context['total_count'] / context['items']) - context['repos'] = context['repos'][(context['page'] - 1) * context['items']:context['page'] * context['items']] - - return render(request, 'dev_status/home.html', context) + context["page"] = int(page) + context["items"] = int(items) + context["sort"] = sort + context["direction"] = direction + context["num_pages"] = math.ceil(context["total_count"] / context["items"]) + context["repos"] = context["repos"][ + (context["page"] - 1) * context["items"] : context["page"] * context["items"] + ] + + return render(request, "dev_status/home.html", context) + -def get_repo(request, r='thatcomputerscientist', p=None): - repository = 'luciferreeves/{}'.format(r) +def get_repo(request, r="thatcomputerscientist", p=None): + repository = "luciferreeves/{}".format(r) parent = None - if p and not len(p.split('/')) == 0: - parent = '/'.join(p.split('/')[:-1]) - contents = g.get_repo(repository).get_contents(p or '') + if p and not len(p.split("/")) == 0: + parent = "/".join(p.split("/")[:-1]) + contents = g.get_repo(repository).get_contents(p or "") context = {} try: files = [] while contents: file_content = contents.pop(0) files.append(file_content) - context['title'] = 'Tree - {}'.format(p) - context['files'] = files - context['parent'] = parent - context['repo'] = r + context["title"] = "Tree - {}".format(p) + context["files"] = files + context["parent"] = parent + context["repo"] = r except: - context['title'] = 'File - {}'.format(p) - context['file'] = contents.html_url - context['parent'] = parent - context['repo'] = r + context["title"] = "File - {}".format(p) + context["file"] = contents.html_url + context["parent"] = parent + context["repo"] = r finally: - return render(request, 'dev_status/repo.html', context)
\ No newline at end of file + return render(request, "dev_status/repo.html", context) |
