Skip to content

Commit

Permalink
Populate GSheet with all the columns present in JSON (#14488)
Browse files Browse the repository at this point in the history
* adding comment as columns

* add checkboxes as json in sheet

* fix: update sheet range based on environment for production or staging

* feat: enhance cred_sign_up to dynamically extract and append comments to Google Sheets

* feat: add timestamp field to cred_sign_up form submission

* fix: update checkbox value from 'Self Teaching' to 'Other' in signup forms

* refactor: remove redundant fields from cred_sign_up sheet and reorder headers

* remove redundant checkbox
  • Loading branch information
usamabinnadeem-10 authored Nov 26, 2024
1 parent 0c0a86f commit 780be4c
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 27 deletions.
14 changes: 3 additions & 11 deletions templates/shared/_credentials-signup-sme-form.html
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,6 @@ <h3>About you</h3>
type="checkbox" />
<span class="p-checkbox__label" id="ContainersVMsLXD">Containers, VMs, LXD</span>
</label>
<label class="p-checkbox">
<input class="p-checkbox__input"
value="Ubuntu Desktop"
aria-labelledby="UbuntuDesktop"
name="cred_area_of_expertise"
type="checkbox" />
<span class="p-checkbox__label" id="UbuntuDesktop">Ubuntu Desktop</span>
</label>
<label class="p-checkbox">
<input class="p-checkbox__input"
value="Storage"
Expand All @@ -283,8 +275,6 @@ <h3>About you</h3>
type="checkbox" />
<span class="p-checkbox__label" id="Storage">Storage</span>
</label>
</div>
<div class="col-4">
<label class="p-checkbox">
<input class="p-checkbox__input"
value="Networking"
Expand All @@ -293,6 +283,8 @@ <h3>About you</h3>
type="checkbox" />
<span class="p-checkbox__label" id="Networking">Networking</span>
</label>
</div>
<div class="col-4">
<label class="p-checkbox">
<input class="p-checkbox__input"
value="Charms"
Expand Down Expand Up @@ -453,7 +445,7 @@ <h3>Knowledge</h3>
</label>
<label class="p-checkbox">
<input class="p-checkbox__input"
value="Self Teaching"
value="Other"
aria-labelledby="Other"
name=""
type="checkbox" />
Expand Down
2 changes: 1 addition & 1 deletion templates/shared/_credentials-signup-tester-form.html
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ <h3>Knowledge</h3>
</label>
<label class="p-checkbox">
<input class="p-checkbox__input"
value="Self Teaching"
value="Other"
aria-labelledby="Other"
name=""
type="checkbox" />
Expand Down
118 changes: 103 additions & 15 deletions webapp/shop/cred/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,24 +219,112 @@ def cred_sign_up(**_):

service = build("sheets", "v4", credentials=credentials)

def extract_json_comment(obj):
fields = [
"NativeLanguage",
"Country",
"areaOfExpertise",
"HasFormalTechnicalDegree",
"HighestLevelOfFormalEducation",
"UbuntuLastProfessionalExperience",
"CUEMotivation",
"whyNotOtherCertifications",
"UbuntuLastAcademicExperience",
"whyOtherCertifications",
"trainingExperiences",
"otherCertifications",
"UbuntuOverallExperience",
"YearsTechnicalRole",
]
row = []
for key in fields:
cell = obj.get(key, None)
if cell is not None:
if isinstance(cell, dict):
json_dict = json.dumps(cell)
row.append(json_dict)
else:
row.append(cell)
else:
row.append("")

return row

SHEET_ID = "1i9dT558_YYxxdPpDTG5VYewezb5gRUziMG77BtdUZGU"
range = (
"Production"
if "staging"
not in os.getenv(
"CONTRACTS_API_URL", "https://contracts.staging.canonical.com/"
)
else "Staging"
)

sheet = service.spreadsheets()
# add the header to the sheet if the sheet is empty initially
result = (
sheet.values()
.get(spreadsheetId=SHEET_ID, range=f"{range}!1:1")
.execute()
)
first_row = result.get("values", [])
if len(first_row) == 0:
header = [
"First Name",
"Last Name",
"Email",
"Job Role",
"Timestamp",
"Title",
"Comments",
"Canonical Updates Opt In",
"Exam Contributor Type",
"NativeLanguage",
"Country",
"Area Of Expertise",
"Has Formal Technical Degree",
"Highest Level Of Formal Education",
"Ubuntu Last Professional Experience",
"CUE Motivation",
"Why Not Other Certifications",
"Ubuntu Last Academic Experience",
"Why Other Certifications",
"Training Experiences",
"Other Certifications",
"Ubuntu Overall Experience",
"Years Technical Role",
]
body = {"values": [header]}
sheet.values().append(
spreadsheetId=SHEET_ID,
range=f"{range}!A:A",
valueInputOption="RAW",
body=body,
).execute()

body = {
"values": [
[
form_fields.get("firstName"),
form_fields.get("lastName"),
form_fields.get("email"),
form_fields.get("Job_Role__c"),
datetime.now(pytz.UTC).strftime("%Y-%m-%d %H:%M:%S"),
form_fields.get("title"),
form_fields.get("Comments_from_lead__c"),
form_fields.get("canonicalUpdatesOptIn"),
form_fields.get("exam_contributor_type"),
*extract_json_comment(
json.loads(form_fields["Comments_from_lead__c"])
),
]
]
}
sheet.values().append(
spreadsheetId="1i9dT558_YYxxdPpDTG5VYewezb5gRUziMG77BtdUZGU",
range="Sheet1",
spreadsheetId=SHEET_ID,
range=f"{range}!A:A",
valueInputOption="RAW",
body={
"values": [
[
form_fields.get("firstName"),
form_fields.get("lastName"),
form_fields.get("email"),
form_fields.get("Job_Role__c"),
form_fields.get("title"),
form_fields.get("Comments_from_lead__c"),
form_fields.get("canonicalUpdatesOptIn"),
]
]
},
body=body,
).execute()

if return_url:
Expand Down

0 comments on commit 780be4c

Please sign in to comment.