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

lab1 #1248

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

lab1 #1248

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 .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
app.log
__pycache__
62 changes: 0 additions & 62 deletions README.md

This file was deleted.

17 changes: 17 additions & 0 deletions app_python/PYTHON.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## Choice of framework


I chose ``Flask`` for this project due to following reasons:
- It is well-suited for small projects:)
- It is easy to insert content into HTML templates using it.
- It has build-in development server for testing.

## Best practices

- Logging: use ``logging`` library for printing logs into ``app.log`` file
- ``.gitignore``: use it to stash logs.
- Error handling: add ``error.html`` template to render it in case of errors.
- Code formatting: use ``Pylint`` for it.
- Testing: wrote test for validating if rendered time is correct using ``unittest`` library.
- Created ``requirements.txt`` file using <code>pip freeze > requirements.txt</code> command.
- Store all HTML templates into separate folder ``templates``
29 changes: 29 additions & 0 deletions app_python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## Description of application

### 1. Purpose

This simple app used to display current Moscow (UTC-3) time.

### 2. Stack
- Framework: ``flask``
- Templates of pages: ``Django HTML``
- Time calcualtion: ``datetime`` library
- Testing: ``unittest`` library
- Logging: ``logging`` library


### 3. Structure

- ``app.py`` performs routing at ``'/'`` and calculates Moscow time.
- ``test_app.py`` performs unit test to validate the correctness of time.
- ``app.log`` contains logs of application during its work.

#### 3.1. Tempelates
- ``index.html`` is the main page where time is shown.
- ``error.html`` contains a message that error has occured.

#### 3.2. Documentation
- ``PYTHON.md`` describes the reasons of choosing ``flask`` and best practices applied to the project.
- ``README.md`` describes the overall structure of project.


35 changes: 35 additions & 0 deletions app_python/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import datetime
import logging
from flask import Flask, render_template

app = Flask(__name__)

# Configure logging
logging.basicConfig(filename='app.log', level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s')


@app.route('/')
def display_time():
try:
# Correct the usage of datetime.utcnow()
moscow_time = datetime.datetime.utcnow() + datetime.timedelta(hours=3)
formatted_time = moscow_time.strftime("%Y-%m-%d %H:%M:%S")

# Log the current time
logging.info(f"Displayed Moscow time: {formatted_time}")

# Log a custom message
logging.info("Custom log message")

return render_template('index.html', time=formatted_time)
except Exception as e:
# Log any exceptions that occur
logging.error(f"An error occurred: {str(e)}")

# You can create an error template for users
return render_template('error.html')


if __name__ == '__main__':
app.run()
Binary file added app_python/requirements.txt
Binary file not shown.
14 changes: 14 additions & 0 deletions app_python/templates/error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Error - Lab 1</title>
</head>
<body>
<main>
<h1>An error occurred</h1>
</main>
</body>
</html>
14 changes: 14 additions & 0 deletions app_python/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>lab 1</title>
</head>
<body>
<main>
<h1>Current time in Moscow: {{time}}</h1>
</main>
</body>
</html>
20 changes: 20 additions & 0 deletions app_python/test_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import unittest
from app import app
import datetime

class FlaskAppTests(unittest.TestCase):

def setUp(self):
self.app = app.test_client()
self.app.testing = True

def test_moscow_time_calculation(self):
utc_now = datetime.datetime.utcnow()
expected_moscow_time = utc_now + datetime.timedelta(hours=3)
expected_time_str = expected_moscow_time.strftime("%Y-%m-%d %H:%M:%S")
response = self.app.get('/')
displayed_time_str = response.data.decode('utf-8').split(': ')[1].split('</h1>')[0].strip()
self.assertEqual(displayed_time_str, expected_time_str)

if __name__ == '__main__':
unittest.main()
54 changes: 0 additions & 54 deletions lab1.md

This file was deleted.

97 changes: 0 additions & 97 deletions lab10.md

This file was deleted.

Loading