Skip to content

Commit

Permalink
Update app structure
Browse files Browse the repository at this point in the history
  • Loading branch information
metaodi committed Mar 15, 2024
1 parent 63abfbe commit 7187d80
Show file tree
Hide file tree
Showing 7 changed files with 1,036 additions and 168 deletions.
113 changes: 29 additions & 84 deletions app.py
@@ -1,8 +1,5 @@
import datetime
import csv
import tempfile
import os
import uuid
import json
from flask import Flask, jsonify, request, render_template
from flask_cors import CORS
from github import Github
Expand All @@ -26,95 +23,43 @@

@app.route('/')
def home():
return render_template('index.html')
args = request.args

@app.route('/submit_calendar', methods=['POST'])
con_id = args.get("consultation_id")

# read json
content = {}
if con_id:
json_path = os.path.join("fedlex", f"{con_id}.json")
with open(json_path) as f:
content = json.loads(f.read())

return render_template('index.html', consultation_id=con_id, content=content)

@app.route('/submit_consultation', methods=['POST'])
def submit_calendar():
response = {"status": "success"}
data = request.get_json()
print(data)


#[{'id': 1679647593951, 'type': 'Karton', 'isDateSeries': True, 'value': ['Montag'], 'repetition': '0', 'radioGroup': 'days', 'datePicker': '', 'timePicker': ''}]

now = datetime.datetime.now()
next_year = now.year + 1
start_date = datetime.datetime(next_year, 1, 1)
end_date = datetime.datetime(next_year, 12, 31)


cal = Calendar()
for entry in data['entries']:

if entry['isDateSeries']:
for weekday in entry['value']:
event = Event()
event.add('summary', entry['type'])

first_date = next_weekday(start_date, weekday)
event.add('dtstart', first_date)
event.add(
'rrule',
{
'FREQ': entry['radioGroup'],
'INTERVAL': entry['repetition'],
'UNTIL': end_date
}
)
cal.add_component(event)

else:
event = Event()
event.add('summary', entry['type'])
start_date = datetime.datetime.fromisoformat(entry['datePicker'])
event.add('dtstart', start_date)
cal.add_component(event)

print(cal.to_ical().decode("utf-8"))


events = recurring_ical_events.of(cal).between(start_date, end_date)
from pprint import pprint
pprint(events)

header = [
'region',
'waste_type',
'col_date',
]
with tempfile.TemporaryFile('w+') as fp:
writer = csv.DictWriter(fp, fieldnames=header, quoting=csv.QUOTE_NONNUMERIC)
writer.writeheader()
for event in events:
props = {v[0]: v[1] for v in event.property_items()}
row = {
'region': data['municipality'],
'waste_type': props['SUMMARY'],
'col_date': props['DTSTART'].dt.date().isoformat(),
}
print(row)
writer.writerow(row)
fp.seek(0)


filename = data['municipality'].replace(' ', '_').replace('.', '').lower()
repo_path = f"csv/{filename}.csv"
# filename = data['municipality'].replace(' ', '_').replace('.', '').lower()
# repo_path = f"csv/{filename}.csv"

br_id = str(uuid.uuid4())
branch = f"{filename}-{br_id}"
# br_id = str(uuid.uuid4())
# branch = f"{filename}-{br_id}"

create_branch(branch)
add_csv_to_branch(branch, fp, repo_path)
create_pull_request(branch, f"New data for {data['municipality']}", "Please check")
# create_branch(branch)
# add_csv_to_branch(branch, fp, repo_path)
# create_pull_request(branch, f"New data for {data['municipality']}", "Please check")

return jsonify(response)


# function to create new branch (wiring to app still missing)
def create_branch(new_branch_name):

REPO_NAME = 'BinBuddy'
OWNER_NAME = 'opendatazurich'
REPO_NAME = 'govtech24_kommentator2000'
OWNER_NAME = 'metaodi'

# Authenticate with GitHub using PyGitHub
g = Github(ACCESS_TOKEN)
Expand All @@ -138,8 +83,8 @@ def create_branch(new_branch_name):
# function to add file to branch
def add_csv_to_branch(branch_name, csv_file, repo_path):
# Replace with the name of your repository and the owner's username
REPO_NAME = 'BinBuddy'
OWNER_NAME = 'opendatazurich'
REPO_NAME = 'govtech24_kommentator2000'
OWNER_NAME = 'metaodi'

# Authenticate with GitHub using PyGitHub
g = Github(ACCESS_TOKEN)
Expand All @@ -157,19 +102,19 @@ def add_csv_to_branch(branch_name, csv_file, repo_path):
# Create the new file in the repository
repo.create_file(
path=repo_path,
message='Add new CSV file',
message='Add consultation version',
content=contents,
branch=branch_name
)

# Print a success message
print('CSV file added to branch.')
print('File added to branch.')

# function to create pull request
def create_pull_request(branch_name, title, description):
# Replace with the name of your repository and the owner's username
REPO_NAME = 'BinBuddy'
OWNER_NAME = 'opendatazurich'
REPO_NAME = 'govtech24_kommentator2000'
OWNER_NAME = 'metaodi'

# Authenticate with GitHub using PyGitHub
g = Github(ACCESS_TOKEN)
Expand Down
2 changes: 1 addition & 1 deletion fedlex.xml
Expand Up @@ -6697,4 +6697,4 @@ der Eignerrechte</p>
</component>
</components>
</act>
</akomaNtoso>
</akomaNtoso>

0 comments on commit 7187d80

Please sign in to comment.