Skip to content

Commit

Permalink
[pymongo-todo] Todo in python with mongoDB
Browse files Browse the repository at this point in the history
  • Loading branch information
ikoala21 authored and sakethramanujam committed Mar 9, 2020
1 parent 05fe057 commit a19f31c
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pymongo/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vscode/
basic/
33 changes: 33 additions & 0 deletions pymongo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# :pen: PyMongo Todo
A simple command line todo app built in :snake: python with :heart: on MongoDB

### :paperclip: Usage
```python
$ python todo.py
```

### :package: Pre-requisites
- [MongoDB](https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/)
- Python Packages
- pymongo
- datetime

> #### Note:
- Make sure your Mongo Daemon is running on http://127.0.0.1:27017
- It's up to you whether or not to create a **db** named _todo_ and **collection** named _todos_ in it.

### :camera: Samples

![basic](./media/basic.png)
---
![add](./media/add.png)
---
![show](./media/show.png)
---
![update](./media/update.png)
---
![delete](./media/delete.png)
---

author: @manasakalaga

Binary file added pymongo/media/add.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pymongo/media/basic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pymongo/media/delete.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pymongo/media/show.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pymongo/media/update.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions pymongo/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
astroid==2.3.3
DateTime==4.3
isort==4.3.21
lazy-object-proxy==1.4.3
mccabe==0.6.1
pkg-resources==0.0.0
pylint==2.4.4
pymongo==3.10.1
pytz==2019.3
six==1.14.0
typed-ast==1.4.1
wrapt==1.11.2
zope.interface==4.7.1
139 changes: 139 additions & 0 deletions pymongo/todo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
from pymongo import MongoClient
import sys
from datetime import datetime as dt

client = MongoClient()
db = client['todo']
collection = db['todos']


def _task_exists(task_name: str) -> bool:
if collection.find_one({'name': task_name}) is None:
return False
else:
return True


def _add_task(task: dict):
'''
Takes in a task
---
Arguments
- task
Example:
task = {'created':<time>,
'name':<name of the task>,
'status': <done/undone>,
'completed':<time> }
'''
if not _task_exists(task_name=task['name']):
collection.insert_one(task)


def _delete_all():
'''
Deletes all tasks in the database
'''
collection.delete_many({})


def _del_task(task_name: str):
'''
Takes in task name and deletes the task
---
Args:
- task_name
Returns:
None
'''
if _task_exists(task_name=task_name):
collection.delete_one({'name': task_name})


def _update_task(task_name: str):
'''
Takes in task name and updates the task status to done
---
Args:
- task_name
Returns:
None
'''
if _task_exists(task_name=task_name):
collection.find_one_and_update(
{'name': task_name}, {'$set': {'status': 'done', 'completed': dt.now()}})


def _show(what: str):
'''
Takes in 'what' and displays list of tasks
---
Args:
- which
- all
- done
- undone
Returns:
None
'''
if what == 'all':
for i, task in enumerate(collection.find()):
print(f"{i+1} - {task['name']}, created at {task['created']}")
elif(what == 'done'):
for i, task in enumerate(collection.find({'status': 'done'})):
print(f"{i+1} - {task['name']}, completed at {task['completed']}")
elif(what == 'undone'):
for i, task in enumerate(collection.find({'status': 'not done'})):
print(f"{i+1} - {task['name']}, created at {task['created']}")


def main():
while True:
c = input(
'Do what (add, delete/delete all, update, show <all/undone/done>, exit): ')
if c == 'add':
task = {}
task['name'] = input('Task: ')
task['created'] = dt.now()
task['status'] = 'not done'
task['completed'] = 'na'
_add_task(task)
print('---- All Tasks ----')
_show(what='all')
elif c == 'show all':
print('---- All Tasks ----')
_show(what='all')
elif c == 'show undone':
print('---- Undone Tasks ----')
_show(what='undone')
elif c == 'show done':
print('---- Completed Tasks ----')
_show(what='done')
elif c == 'update':
print('---- All Tasks ----')
_show(what='all')
which = input('Which Task: ')
_update_task(task_name=which)
print('---- Completed Tasks ----')
_show(what='done')
elif c == 'delete':
print('---- All Tasks ----')
_show(what='all')
which = input('Which Task: ')
_del_task(task_name=which)
print('---- Completed Tasks ----')
_show(what='all')
elif c == 'delete all':
_delete_all()
elif c == 'exit':
sys.exit("Ok Bye! :)")


if __name__ == "__main__":
main()

0 comments on commit a19f31c

Please sign in to comment.