-
Notifications
You must be signed in to change notification settings - Fork 2.2k
PCC38 GeoffRiley — HacktoberFest Checker #851
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| from datetime import date | ||
|
|
||
| from bottle import run, template, request, post, get | ||
| from github import Github, GithubException | ||
|
|
||
|
|
||
| @get('/') | ||
| def login(): | ||
| return ''' | ||
| <h1>HacktoberFest Checker</h1> | ||
| <h2>Login</h2> | ||
| <p>Please enter your Github username</p> | ||
| <form action="/" method="post"> | ||
| Github username: <input name="username" type="text" /> | ||
| </form> | ||
| ''' | ||
|
|
||
|
|
||
| @post('/') | ||
| def search_prs(): | ||
| # Statements taken from https://github.com/pybites/hacktoberfest-checker/blob/master/controllers/index.js | ||
| statements = [ | ||
| "It's not too late to start!", | ||
| "Off to a great start, keep going!", | ||
| "Half way there, keep it up!", | ||
| "So close!", | ||
| "Way to go!", | ||
| "Now you're just showing off!" | ||
| ] | ||
| # Extract the username entered in the web form | ||
| username = request.forms.get('username') | ||
|
|
||
| # check with GitHub that the entered username actually exists… if not show the unknown template | ||
| gh = Github() | ||
| try: | ||
| user = gh.get_user(username).name | ||
| except GithubException: | ||
| return template('unknown') | ||
|
|
||
| # Now we're going to grab a list of all the PRs put through by the user… there should be more checking though, ie: | ||
| # for invalid and spam labels at the very least. | ||
| try: | ||
| # Query based upon: | ||
| # const options = { | ||
| # q: `-label:invalid | ||
| # +created:2017-09-30T00:00:00-12:00..2017-10-31T23:59:59-12:00 | ||
| # +type:pr | ||
| # +is:public | ||
| # +author:${username}` | ||
| # }; | ||
| # from https://github.com/pybites/hacktoberfest-checker/blob/master/controllers/index.js | ||
| # The was written for 2017, so it needs to be updated for *this* year, | ||
| # we can automagically pick the current year: | ||
| year = str(date.today().year) | ||
| # Request a list of PRs within the specific date range… | ||
| query = f'{year}-09-30T00:00:00-12:00..{year}-10-31T23:59:59-12:00' | ||
| pr_list = gh.search_issues('', author=username, type='pr', created=query) | ||
| except GithubException: | ||
| # any errors from GitHub at this stage we just assumes means no PRs found | ||
| pr_list = [] | ||
| pr_list.totalCount = 0 | ||
|
|
||
| # We're going to pick out the details for each PR that we want to display to the user | ||
| pr_detail = {"url": [], "title": [], "date": []} | ||
| # Provided there are PRs, loop through them extracting the url, title and date into the pr_detail arrays | ||
| if pr_list.totalCount > 0: | ||
| for pr in pr_list: | ||
| pr_detail["url"].append(pr.pull_request.html_url) | ||
| pr_detail["title"].append(pr.title) | ||
| pr_detail["date"].append(pr.created_at) | ||
|
|
||
| pr_count = len(pr_detail["title"]) | ||
| return template('pr_list', pr_data=pr_detail, name=pr_list[0].user.name, | ||
| prs=pr_detail["title"], dates=pr_detail["date"], urls=pr_detail["url"], | ||
| pr_count=pr_count, statement=statements[pr_count]) | ||
|
|
||
| return template('pr_list', pr_count=0, name=user, statement=statements[0]) | ||
|
|
||
|
|
||
| run(host='localhost', port=8080) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <h1>HacktoberFest Checker</h1> | ||
| <h2>{{name}}</h2> | ||
| <p>You have {{pr_count}} out of 4 PRs completed this October.</p> | ||
| <p><strong>{{statement}}</strong></p> | ||
| %if 'prs' in locals(): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need locals(), seems you're passing all data into the template?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've probably misunderstood how the templates work… I must admit I faffed around with lots of different things before trying this (that I found on stackoverflow). I was trying to stop it attempting to print the table when there were no PRs to print… and now I'm looking at it again I should have just tested for Thanks Bob. |
||
| <ul> | ||
| %for pr,date,url in zip(prs,dates,urls): | ||
| <li><a href={{url}}>{{pr}}</a> created on {{date}}</li> | ||
| %end | ||
| </ul> | ||
| %end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| <h1>HacktoberFest Checker</h1> | ||
| <p>Sorry, could not find a user on Github with that name, please check and <a href="/">try again</a>.</p> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about a list of named tuples?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried with a
namedtupleand got a bunch of errors in the template—the template seems to be very fussy on some things, I put it down to it being a beta version for the Py3 compatibility.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok gotcha, all good then, merged quickly as last day of Hacktoberfest :)