mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-06-04 06:38:04 +02:00
feat(edit): add the edit feature
This commit is contained in:
@@ -11,10 +11,18 @@ import requests
|
|||||||
import github
|
import github
|
||||||
|
|
||||||
|
|
||||||
|
def clean_github_repository(repo):
|
||||||
|
if repo is None:
|
||||||
|
return None
|
||||||
|
repo = repo.replace("http://github.com/", "") \
|
||||||
|
.replace("https://github.com/", "")
|
||||||
|
if repo[-1] == '/':
|
||||||
|
repo = repo[:-1]
|
||||||
|
return repo
|
||||||
|
|
||||||
|
|
||||||
class Anonymous_Github:
|
class Anonymous_Github:
|
||||||
def __init__(self, github_token="0e8f5af6801d89e533f5c045920e928b4535d41e", host="127.0.0.1", port=5000,
|
def __init__(self, github_token, host="127.0.0.1", port=5000,
|
||||||
config_dir='./repositories'):
|
config_dir='./repositories'):
|
||||||
self.github_token = github_token if github_token != "" else os.environ["GITHUB_AUTH_TOKEN"]
|
self.github_token = github_token if github_token != "" else os.environ["GITHUB_AUTH_TOKEN"]
|
||||||
self.host = host
|
self.host = host
|
||||||
@@ -34,8 +42,6 @@ class Anonymous_Github:
|
|||||||
else:
|
else:
|
||||||
self.public_url = "http://" + self.host + ":" + str(self.port)
|
self.public_url = "http://" + self.host + ":" + str(self.port)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def create_flask_application(self):
|
def create_flask_application(self):
|
||||||
application = Flask(__name__)
|
application = Flask(__name__)
|
||||||
application.log = {}
|
application.log = {}
|
||||||
@@ -48,10 +54,13 @@ class Anonymous_Github:
|
|||||||
for term in terms:
|
for term in terms:
|
||||||
content = content.replace(term, "XXX")
|
content = content.replace(term, "XXX")
|
||||||
return content
|
return content
|
||||||
|
|
||||||
if file.size > 1000000:
|
if file.size > 1000000:
|
||||||
return Markup("The file %s is too big please download it: <a href='%s'>Download %s</a>" % (file.name, file.url, file.name))
|
return Markup("The file %s is too big please download it: <a href='%s'>Download %s</a>" % (
|
||||||
|
file.name, file.url, file.name))
|
||||||
if ".md" in file.name:
|
if ".md" in file.name:
|
||||||
return Markup("<div class='markdown-body'>%s</div>" % removeTerms(self.github.render_markdown(file.decoded_content), terms))
|
return Markup("<div class='markdown-body'>%s</div>" % removeTerms(
|
||||||
|
self.github.render_markdown(file.decoded_content), terms))
|
||||||
if ".jpg" in file.name or ".png" in file.name or ".png" in file.name or ".gif" in file.name:
|
if ".jpg" in file.name or ".png" in file.name or ".png" in file.name or ".gif" in file.name:
|
||||||
return Markup("<img src='%s' alt='%s'>" % (file.url, file.name))
|
return Markup("<img src='%s' alt='%s'>" % (file.url, file.name))
|
||||||
if ".html" in file.name:
|
if ".html" in file.name:
|
||||||
@@ -75,11 +84,7 @@ class Anonymous_Github:
|
|||||||
return render_template('404.html'), 404
|
return render_template('404.html'), 404
|
||||||
with open(config_path) as f:
|
with open(config_path) as f:
|
||||||
data = json.load(f)
|
data = json.load(f)
|
||||||
repo = data['repository']\
|
repo = clean_github_repository(data['repository'])
|
||||||
.replace("http://github.com/", "")\
|
|
||||||
.replace("https://github.com/", "")
|
|
||||||
if repo[-1] == '/':
|
|
||||||
repo = repo[:-1]
|
|
||||||
g_repo = self.github.get_repo(repo)
|
g_repo = self.github.get_repo(repo)
|
||||||
current_folder = g_repo.get_contents(urllib.quote(path))
|
current_folder = g_repo.get_contents(urllib.quote(path))
|
||||||
current_file = None
|
current_file = None
|
||||||
@@ -102,23 +107,36 @@ class Anonymous_Github:
|
|||||||
|
|
||||||
@application.route('/', methods=['GET'])
|
@application.route('/', methods=['GET'])
|
||||||
def index():
|
def index():
|
||||||
return render_template('index.html')
|
id = request.args.get('id', None)
|
||||||
|
repo_name = clean_github_repository(request.args.get('githubRepository', None))
|
||||||
|
repo = None
|
||||||
|
if id is not None:
|
||||||
|
config_path = self.config_dir + "/" + id + "/config.json"
|
||||||
|
if os.path.exists(config_path):
|
||||||
|
with open(config_path) as f:
|
||||||
|
data = json.load(f)
|
||||||
|
repo_data = clean_github_repository(data['repository'])
|
||||||
|
if repo_name == repo_data:
|
||||||
|
repo = data
|
||||||
|
|
||||||
|
return render_template('index.html', repo=repo)
|
||||||
|
|
||||||
@application.route('/', methods=['POST'])
|
@application.route('/', methods=['POST'])
|
||||||
def add_repository():
|
def add_repository():
|
||||||
id = str(uuid.uuid4())
|
id = request.args.get('id', str(uuid.uuid4()))
|
||||||
repo = request.form['githubRepository']
|
repo = request.form['githubRepository']
|
||||||
terms = request.form['terms'],
|
terms = request.form['terms']
|
||||||
repo_name = request.form['name']
|
repo_name = request.form['name']
|
||||||
|
|
||||||
config_path = self.config_dir + "/" + str(id)
|
config_path = self.config_dir + "/" + str(id)
|
||||||
os.mkdir(config_path)
|
if not os.path.exists(config_path):
|
||||||
|
os.mkdir(config_path)
|
||||||
with open(config_path + "/config.json", 'w') as outfile:
|
with open(config_path + "/config.json", 'w') as outfile:
|
||||||
json.dump({
|
json.dump({
|
||||||
"id": id,
|
"id": id,
|
||||||
"name": repo_name,
|
"name": repo_name,
|
||||||
"repository": repo,
|
"repository": repo,
|
||||||
"terms": terms.split()
|
"terms": terms.splitlines()
|
||||||
}, outfile)
|
}, outfile)
|
||||||
return redirect(url_for('repository', id=id))
|
return redirect(url_for('repository', id=id))
|
||||||
|
|
||||||
|
|||||||
+23
-6
@@ -14,24 +14,41 @@
|
|||||||
<div class="main">
|
<div class="main">
|
||||||
<div class="container-fluid">
|
<div class="container-fluid">
|
||||||
<h1>GihHub Anonymous</h1>
|
<h1>GihHub Anonymous</h1>
|
||||||
|
<h2>Anonymous a GitHub repository</h2>
|
||||||
<form action="" method="post">
|
<form action="" method="post">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="name">GitHub Repository</label>
|
<label for="name">GitHub Repository</label>
|
||||||
<input type="url" class="form-control" name="name" id="name" aria-describedby="name" placeholder="Anonymous Name">
|
<input class="form-control" value="{{ repo.name }}" name="name" id="name" aria-describedby="name" placeholder="Anonymous Name">
|
||||||
<small id="nameHelp" class="form-text text-muted">The anonymous name of the repository.</small>
|
<small id="nameHelp" class="form-text text-muted">The anonymous name of the repository.</small>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="githubRepository">GitHub Repository</label>
|
||||||
|
<input type="url" value="{{ repo.repository }}" class="form-control" name="githubRepository" id="githubRepository" aria-describedby="githubRepository" placeholder="GitHub Repository">
|
||||||
|
<small id="githubRepositoryHelp" class="form-text text-muted">The github url to the repository that you want to anonymous.</small>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="ignoredTerms">The text to remove from the repository</label>
|
||||||
|
<textarea class="form-control" name="terms" id="ignoredTerms" rows="5">{{ repo.terms|join('\n') }}</textarea>
|
||||||
|
<small id="ignoredTermsHelp" class="form-text text-muted">One term per line.</small>
|
||||||
|
</div>
|
||||||
|
<button type="submit" class="btn btn-primary">Submit</button>
|
||||||
|
</form>
|
||||||
|
{% if not repo %}
|
||||||
|
<h2>Edit an Anonymous GitHub repository</h2>
|
||||||
|
<form action="" method="get">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="name">Anonymous GitHub Repository</label>
|
||||||
|
<input class="form-control" name="id" id="name" aria-describedby="name" placeholder="Id Anonymous GitHub Repository">
|
||||||
|
<small id="nameHelp" class="form-text text-muted">The id of the Anonymous GitHub repository.</small>
|
||||||
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label for="githubRepository">GitHub Repository</label>
|
<label for="githubRepository">GitHub Repository</label>
|
||||||
<input type="url" class="form-control" name="githubRepository" id="githubRepository" aria-describedby="githubRepository" placeholder="GitHub Repository">
|
<input type="url" class="form-control" name="githubRepository" id="githubRepository" aria-describedby="githubRepository" placeholder="GitHub Repository">
|
||||||
<small id="githubRepositoryHelp" class="form-text text-muted">The github url to the repository that you want to anonymous.</small>
|
<small id="githubRepositoryHelp" class="form-text text-muted">The github url to the repository that you want to anonymous.</small>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
|
||||||
<label for="ignoredTerms">The text to remove from the repository</label>
|
|
||||||
<textarea class="form-control" name="terms" id="ignoredTerms" rows="5"></textarea>
|
|
||||||
<small id="ignoredTermsHelp" class="form-text text-muted">One term per line.</small>
|
|
||||||
</div>
|
|
||||||
<button type="submit" class="btn btn-primary">Submit</button>
|
<button type="submit" class="btn btn-primary">Submit</button>
|
||||||
</form>
|
</form>
|
||||||
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user