Skip to content

Commit 7a000b3

Browse files
committed
save and edit home.md, but for some reason \n multiplies?
1 parent e7442b5 commit 7a000b3

File tree

7 files changed

+75
-21
lines changed

7 files changed

+75
-21
lines changed
602 Bytes
Binary file not shown.

app.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
from flask import Flask, render_template, request, redirect, url_for
2-
from app_utilities import create_folder, create_empty_comp, folder_occupied, list_all_comps
2+
from app_utilities import *
3+
import os
34

45
app = Flask(__name__)
56

6-
SELECTED_COMP = None
7+
if (not folder_occupied("competitions")):
8+
SELECTED_COMP = None
9+
else:
10+
SELECTED_COMP = retrieve_first_comp()
711

812
@app.route("/")
913
@app.route("/home")
@@ -16,16 +20,35 @@ def create():
1620
if (not folder_occupied("competitions")):
1721
return redirect(url_for('createcomp'))
1822
else:
19-
return render_template("create.html")
23+
return redirect(url_for('edithome'))
2024

2125
@app.route("/edithome")
2226
def edithome():
23-
return render_template("edithome.html")
27+
global SELECTED_COMP
28+
if (SELECTED_COMP == None):
29+
return redirect(url_for('createcomp'))
30+
else:
31+
return render_template("edithome.html")
32+
33+
@app.route("/savehome", methods=('GET', 'POST'))
34+
def savehome():
35+
if request.method == 'POST':
36+
saved_text = request.form['hometext']
37+
# sanitized_text = saved_text.replace('','\n')
38+
# overwrite_file(str(SELECTED_COMP)+'/Home.md',saved_text)
39+
print(saved_text)
40+
print(saved_text.count('\n'))
41+
overwrite_file(os.path.join('competitions',str(SELECTED_COMP),'Home.md'),saved_text)
42+
return redirect(url_for('edithome'))
2443

2544
@app.route("/selectcomp", methods=('GET', 'POST'))
2645
def selectcomp():
2746
global SELECTED_COMP
2847
SELECTED_COMP = request.form['competitions']
48+
49+
if (SELECTED_COMP == 'Select competition'):
50+
SELECTED_COMP = retrieve_first_comp()
51+
2952
return redirect(url_for('create'))
3053

3154
@app.route("/initcomp", methods=('GET', 'POST'))
@@ -61,5 +84,11 @@ def get_selected_comp():
6184
return SELECTED_COMP
6285
return dict(get_selected_comp=get_selected_comp)
6386

87+
@app.context_processor
88+
def utility_processor():
89+
def get_home():
90+
return extract_text('competitions/'+str(SELECTED_COMP),'Home.md')
91+
return dict(get_home=get_home)
92+
6493
if __name__ == '__main__':
6594
app.run(debug=True)

app_utilities.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,21 @@ def list_all_comps():
6868
dir = os.listdir(os.path.join(current_directory,"competitions"))
6969
return dir
7070

71-
print(list_all_comps())
71+
def retrieve_first_comp():
72+
return list_all_comps()[0]
73+
74+
def extract_text(dir,filename):
75+
current_directory = os.getcwd()
76+
with open(os.path.join(current_directory,dir,filename),'r') as file:
77+
contents = file.read()
78+
return contents
79+
80+
def overwrite_file(dir,text):
81+
current_directory = os.getcwd()
82+
if (os.path.exists(os.path.join(current_directory,dir))):
83+
os.remove(os.path.join(current_directory,dir))
84+
else:
85+
pass
86+
create_file(os.path.join(current_directory,dir),text)
87+
88+
# print(extract_text('competitions'+'/KhetarKoinCompetition','Home.md'))

competitions/KhetarKoinCompetition/Home.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
# Welcome to CTFg!
2-
3-
## KhetarKoinCompetition
4-
5-
Author: Agneya, Competition difficulty: Cyber Criminal
6-
7-
## Challenge Description:
8-
Students will have to investigate who and when Calvin's KhetarKoin was stolen
9-
10-
Competition generated using CHALGEN-Online - Created by Agneya Tharun
1+
hi
2+
3+
4+
5+
6+
7+
8+
9+
hi

competitions/mcpshsf2024/Home.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test
2+
3+
test

competitions/mcpshsf2024/config.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
entrypoint:
2+
- Fesbuc
3+
admin_email: [email protected]
4+
admin_password: ctfgadministrator
5+
homepage: Home.md

templates/edithome.html

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@
2323
<section class="bg-white dark:bg-gray-900">
2424
<div class="py-8 px-4 mx-auto max-w-2xl lg:py-16">
2525
<h2 class="mb-4 text-xl font-bold text-gray-900 dark:text-white">Edit Home.md</h2>
26-
<form method="post" action="/initcomp">
27-
<div class="grid gap-4 sm:grid-cols-2 sm:gap-6">
26+
<!-- finish making edit home.md -->
27+
<form method="post" action="/savehome">
28+
<div class="">
2829
<div class="sm:col-span-2">
29-
<label for="compdescription" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Provide a brief description</label>
30-
<textarea id="compdescription" name="compdescription" rows="8" class="block p-2.5 w-full text-sm text-gray-900 bg-gray-50 rounded-lg border border-gray-300 focus:ring-primary-500 focus:border-primary-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" placeholder="You can change this later."></textarea>
30+
<label for="hometext" class="block mb-2 text-sm font-medium text-gray-900 dark:text-white">Provide a brief description</label>
31+
<textarea id="hometext" name="hometext" rows="8" class="block p-2.5 w-full text-sm text-gray-900 bg-gray-50 rounded-lg border border-gray-300 focus:ring-primary-500 focus:border-primary-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-primary-500 dark:focus:border-primary-500" placeholder="You can change this later.">{{ get_home() }}</textarea>
3132
</div>
3233
</div>
33-
<button type="submit" class="text-white bg-indigo-500 hover:bg-primary-800 focus:ring-4 focus:outline-none focus:ring-primary-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-primary-600 dark:hover:bg-primary-700 dark:focus:ring-primary-800">
34-
Create
34+
<button type="submit" class="mt-2 text-white bg-indigo-500 hover:bg-primary-800 focus:ring-4 focus:outline-none focus:ring-primary-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center dark:bg-primary-600 dark:hover:bg-primary-700 dark:focus:ring-primary-800">
35+
Save
3536
</button>
3637
</form>
3738
</div>

0 commit comments

Comments
 (0)