'TypeError: unhashable type: 'list' in flask
New to flask and trying out POST and GET methods. The error comes from lines 20 and 21. I've changed it up a bit but I keep on getting more errors. Anyone got any ideas?
from flask import Flask, redirect, url_for, request
app = Flask(__name__)
@app.route('/')
def routing():
return redirect(url_for("login"))
@app.route('/login', methods=['POST', 'GET'])
def login():
if request.method == "POST":
user = request.form["username"]
password = request.form["password"]
if user == "John" and password == "Password123!":
return f"Welcome {user}"
else:
return "Invalid credentials"
else:
user = request.args.get(["username"])
password = request.args.get(["password"])
if user == "John" and password == "Password123!":
return f"Welcome {user}"
else:
return "Invalid credentials"
if __name__ == "__main__":
app.run(debug=True)
Solution 1:[1]
The commit history of a single file is not exposed through the GitLab API directly. Therefore, there is no direct functionality for this in the python-gitlab gitlab module.
However, you can obtain, effectively, the same information by using available APIs. Specifically, you can either use the repository commits API and diff APIs or the files blame API.
Using the commits API
For example, using the commits API, you can list all commits and their diffs, then associate file changes for each commit.
import gitlab
from collections import defaultdict
TOKEN = 'Your API Token'
gl = gitlab.Gitlab('https://gitlab.example.com', private_token=TOKEN)
project = gl.projects.get(1234)
commits = project.commits.list(all=True)
# file paths and a list of commits which create/modify/delete the file
file_map = defaultdict(list)
for c in commits:
diff = c.diff()
files_changed = set()
for change in diff:
files_changed.add(change['old_path'])
files_changed.add(change['new_path'])
for path in files_changed:
file_map[path].append(c)
# show list of commits which modified README.md
print(file_map['README.md'])
Using the blame API
Using the commits API requires obtaining the diff for every commit, which can take a long time on large repositories.
If you're only interested in the commits which change a single file, traversing the blame tree can be more efficient. However, note that you may also miss commits (for example, commits in other branches or diverged trees) using this method.
def search_blame(project, filename, base_ref=None):
if base_ref is None:
base_ref = project.default_branch
commits = set()
refs_to_check = [base_ref,]
seen = set()
while refs_to_check:
ref = refs_to_check.pop()
if ref in seen:
continue
seen.add(ref)
blame = project.files.blame(filename, ref)
for change in blame:
commit_id = change['commit']['id']
if commit_id not in seen:
refs_to_check.append(commit_id)
refs_to_check.extend(change['commit']['parent_ids'])
for c in change['commit']['parent_ids']:
commits.add(c)
commits.add(commit_id)
return commits
# show commits in blame tree for README.md
# only includes commits in the default branch
print(search_blame(project, 'README.md'))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | sytech |
