Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search for a range of dates #51

Merged
merged 7 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ body:
`'query'` (required): string representing the string to search for in the 'search_field'
`'return_fields'` (required): list of strings representing aspects of the notes you want returned back to you
- must be in ('modified_date', 'title', 'created_date', 'tag')
`'start'` (option): start date for range to search by date within
`'end'` (option): end date for range to search by date within

## Additional Feature 2: Search by title
Run this with `'search_field': 'title'`
Expand Down
17 changes: 12 additions & 5 deletions apiCalls.py
augustus-thomas marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import requests
from datetime import date

# Http integration managment
# generic verion of a http request, formats to request well.
Expand All @@ -17,18 +18,24 @@ def listNotes(listBy):
url = "http://127.0.0.1:5000/notes/list"
return requests.get(url,headers=header, json={'list_field': listBy})

def searchNotes(field, searchQuery, returnField):
def searchNotes(field, searchQuery, returnField, start='1970-01-01', stop=today.strftime("%Y-%m-%d")):
#by content, title, tags, date
url = "http://127.0.0.1:5000/notes/search"
return requests.get(url,headers=header, json={'search_field': field, 'query': searchQuery, 'return_fields': returnField})

# conditional check on whether to pass start and stop
if field in set('modified_date', 'created_date'):
json = {'search_field': field, 'query': searchQuery, 'return_fields': returnField, 'start': start, 'stop': stop}
else:
json = {'search_field': field, 'query': searchQuery, 'return_fields': returnField}
return requests.get(url,headers=header, json=json)

def searchNotesByTag(tag, returnFields):
# Search notes by tag
url = "http://127.0.0.1:5000/tags/search"
return requests.get(url, headers=header, json={'query': tag, 'return_fields': returnFields})






# Note Classification Suite
def addTag(noteTitle, tagName):
# Makes a list of tags that should be added to the title, does not need to check if tags exist
Expand Down
29 changes: 19 additions & 10 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,30 +304,39 @@ def search_notes():
FROM notes
WHERE notes.content == '{query}'
"""

# modified_date looks like 'YYYY-MM-DD'
if search_field == 'modified_date':
start = datetime.strptime(query, '%Y-%m-%d')
start_query = payload.get('start')
end_query = payload.get('end')
start = datetime.strptime(start_query, '%Y-%m-%d')
end = datetime.strptime(end_query, '%Y-%m-%d')
# Construct SQL query
sql_query = f"""
SELECT {select_fields_string} FROM notes
WHERE modified_date >= '{start.strftime('%Y-%m-%d')}' AND modified_date <= '{end.strftime('%Y-%m-%d')}'
"""

# created_date looks like 'YYYY-MM-DD'
if search_field == 'created_date':
start_query = payload.get('start')
end_query = payload.get('end')
start = datetime.strptime(start_query, '%Y-%m-%d')
end = datetime.strptime(end_query, '%Y-%m-%d')
start = datetime.strptime(query, '%Y-%m-%d')
# Construct SQL query
sql_query = f"""
SELECT {select_fields_string} FROM notes
WHERE created_date == '{start.strftime('%Y-%m-%d')}'
WHERE created_date >= '{start.strftime('%Y-%m-%d')}' AND created_date <= '{end.strftime('%Y-%m-%d')}
"""

# title
if search_field == 'title':
sql_query = f"""
SELECT {select_fields_string}
FROM notes
WHERE notes.title == '{query}'
"""
# created_date looks like 'YYYY-MM-DD'
if search_field == 'created_date':
start = datetime.strptime(query, '%Y-%m-%d')
# Construct SQL query
sql_query = f"""
SELECT {select_fields_string} FROM notes
WHERE created_date >= '{start.strftime('%Y-%m-%d')}'
"""

# tag (super hard ?)
if search_field == 'tag':
Expand Down
20 changes: 14 additions & 6 deletions frontend.py
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I am sure the empty field in the line below serves a purpose. But could you briefly explain the empty field at the start of class because I am not sure what that purpose is.
api_response = apiCalls.searchNotes('created_date', "", desired_response, start, end)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, didn't consider this

Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,15 @@ def runtime(state):
#created_date
case 132:
lineBreak(columns, getConfig(4))
printColor("Enter date created to search.",getConfig(2))
printColor("Enter the beginning of the date range to search by.",getConfig(2))
lineBreak(columns, getConfig(4))
search_by = input(": ")
start = input(": ")
lineBreak(columns, getConfig(4))
printColor("Enter the end of the date range to search by.",getConfig(2))
lineBreak(columns, getConfig(4))
end = input(": ")
desired_response = ['title','modified_date','created_date']
api_response = apiCalls.searchNotes('created_date', search_by, desired_response)
api_response = apiCalls.searchNotes('created_date', "", desired_response, start, end)
entries = translation(api_response.content)
parsing = entries.split("\n")
repeats = len(parsing)
Expand All @@ -325,11 +329,15 @@ def runtime(state):
#modified_date
case 133:
lineBreak(columns, getConfig(4))
printColor("Enter date modified to search.",getConfig(2))
printColor("Enter the beginning of the date range to search by.",getConfig(2))
lineBreak(columns, getConfig(4))
search_by = input(": ")
start = input(": ")
lineBreak(columns, getConfig(4))
printColor("Enter the end of the date range to search by.",getConfig(2))
lineBreak(columns, getConfig(4))
end = input(": ")
desired_response = ['title','modified_date','created_date']
api_response = apiCalls.searchNotes('modified_date', search_by, desired_response)
api_response = apiCalls.searchNotes('modified_date', "", desired_response, start, end)
entries = translation(api_response.content)
parsing = entries.split('\n')
repeats = len(parsing)
Expand Down