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

Conversions Improvement & validations #82

Merged
merged 8 commits into from
Apr 6, 2024
Merged
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
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -39,9 +39,6 @@ create .env in root directory and add
```
SECRET_KEY=anystring
client_secret_captcha=anystring
GRAMMAR_API_URL=anystring
GRAMMAR_API_KEY=anystring
GRAMMAR_API_HOST=anystring
```
as this app consist few database schemas that need to be migrated.so,run
```
38 changes: 32 additions & 6 deletions basicsite/settings/base.py
Original file line number Diff line number Diff line change
@@ -11,10 +11,10 @@
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

@@ -35,7 +35,6 @@
'social_django',
'import_export',


]
SITE_ID = 1
MIDDLEWARE = [
@@ -78,8 +77,6 @@
# Needed to login by username in Django admin, regardless of `allauth`
'django.contrib.auth.backends.ModelBackend',



'social_core.backends.google.GoogleOAuth2',
]
# Database
@@ -92,7 +89,6 @@
}
}


# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators

@@ -111,7 +107,6 @@
},
]


# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/

@@ -132,3 +127,34 @@
LOGIN_URL = '/auth/login/google-oauth2/'
LOGOUT_URL = '/'
LOGIN_REDIRECT_URL = "/Sitemaps/"


def get_cache():
import os
try:
servers = os.environ['MEMCACHIER_SERVERS']
username = os.environ['MEMCACHIER_USERNAME']
password = os.environ['MEMCACHIER_PASSWORD']
return {
'default': {
'BACKEND': 'django_bmemcached.memcached.BMemcached',
# TIMEOUT is not the connection timeout! It's the default expiration
# timeout that should be applied to keys! Setting it to `None`
# disables expiration.
'TIMEOUT': None,
'LOCATION': servers,
'OPTIONS': {
'username': username,
'password': password,
}
}
}
except KeyError:
return {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'
}
}


CACHES = get_cache()
28 changes: 27 additions & 1 deletion mysite/all_views/views_converters.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
from .. import MyFunctions
from django.shortcuts import render
from django.http import HttpResponse
import json
import base64
import requests
import random
from django.views.decorators.csrf import csrf_exempt
from django.core.cache import cache
from .. import MyFunctions
from ..constants import converters as constants
from ..handlers.requestHandler.get import GetHandler

SideMap = MyFunctions.ArrangeSideMapForWebpage()
CACHE_TIMEOUT = 60 * 60 * 24


def generate_currency_endpoint(from_currency):
endpoint_key = random.choice([constants.ENDPOINT_KEY1, constants.ENDPOINT_KEY2])
currencies_queryparam = ','.join(constants.CURRENCIES)
endpoint = f"{constants.CURRENCY_ENDPOINT}/?apikey={endpoint_key}&base_currency={from_currency}&currencies={currencies_queryparam}"
return endpoint


def Binaryconversion(request):
@@ -30,6 +42,20 @@ def Hexadecimalconversion(request):
def Currencyconversion(request):
link_string1, link_string2 = SideMap.arrange(3, 2, 'CC')
param = {'link_string1': link_string1, 'link_string2': link_string2}
if request.method == "POST":
from_currency = request.POST.get('from_currency')
to_currency = request.POST.get('to_currency')
amount = request.POST.get('amount')
if cache.get(from_currency) is None:
endpoint = generate_currency_endpoint(from_currency)
get_request = GetHandler(endpoint)
response = get_request.send()
cache.set(from_currency, response, CACHE_TIMEOUT)
data = cache.get(from_currency).json()
conversion_rate = data['data'][to_currency]['value']
converted_amount = float(amount) * conversion_rate
json_response = json.dumps({'converted_amount': converted_amount}, default=str)
return HttpResponse(json_response)
return render(request, '../templates/converter/CurrencyCon.html', param)


19 changes: 1 addition & 18 deletions mysite/all_views/views_textanalyzer.py
Original file line number Diff line number Diff line change
@@ -4,18 +4,16 @@
import requests
import json
from rake_nltk import Rake
import base64
import os
from PIL import Image
from googletrans import Translator
import googletrans
import pytesseract
from io import BytesIO
from django.views.decorators.csrf import csrf_exempt

SideMap = MyFunctions.ArrangeSideMapForWebpage()
if os.getcwd() != '/app': # for windows
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files/Tesseract-OCR/tesseract' # do install tesseact in this path only and add it to your environment-variables
pytesseract.pytesseract.tesseract_cmd = 'C:/Program Files/Tesseract-OCR/tesseract' # do install tesseract in this path only and add it to your environment-variables
else:
pytesseract.pytesseract.tesseract_cmd = '/app/.apt/usr/bin/tesseract'

@@ -114,12 +112,6 @@ def KeywordsExtraction(request):

def texttobase64(request):
link_string1, link_string2 = SideMap.arrange(4, 1, 'AT')
if request.method == "POST":
text = request.POST['text']
encoded_string = base64.b64encode(text.encode())
encoded_string = encoded_string.decode()
response = json.dumps({'Encoded': encoded_string}, default=str)
return HttpResponse(response)
param = {'link_string1': link_string1, 'link_string2': link_string2}
return render(request, '../templates/textAnalyzer/text_to_base64.html', param)

@@ -128,15 +120,6 @@ def texttobase64(request):

def base64totext(request):
link_string1, link_string2 = SideMap.arrange(5, 1, 'AT')
if request.method == "POST":
try:
text = request.POST['text']
decoded_string = base64.b64decode(text.encode())
decoded_string = decoded_string.decode()
response = json.dumps({'Decoded': decoded_string}, default=str)
except Exception:
response = json.dumps({'Decoded': "There is some Error while processing"}, default=str)
return HttpResponse(response)
param = {'link_string1': link_string1, 'link_string2': link_string2}
return render(request, '../templates/textAnalyzer/base64_to_text.html', param)

Empty file added mysite/constants/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions mysite/constants/converters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import os

CURRENCY_ENDPOINT = f"{os.environ.get('CURRENCY_EXCHANGE_API')}"
ENDPOINT_KEY1 = f"{os.environ.get('CURRENCY_EXCHANGE_API_KEY1')}"
ENDPOINT_KEY2 = f"{os.environ.get('CURRENCY_EXCHANGE_API_KEY2')}"
CURRENCIES = ['EUR', 'USD', 'INR', 'GBP', 'AUD', 'JPY', 'NZD']
Empty file added mysite/handlers/__init__.py
Empty file.
Empty file.
18 changes: 18 additions & 0 deletions mysite/handlers/requestHandler/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.http import HttpResponse
from abc import ABC, abstractmethod


class Request(ABC):
def __init__(self, url, data=None, headers=None):
self.url = url
self.data = data
self.headers = headers

@abstractmethod
def send(self):
pass

def handleResponse(self, response):
if response.status_code != 200:
return HttpResponse(f"An error occurred: {response.status_code}")
return response
9 changes: 9 additions & 0 deletions mysite/handlers/requestHandler/get.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from . import base
import requests


class GetHandler(base.Request):

def send(self):
response = requests.get(self.url, headers=self.headers)
return self.handleResponse(response)
9 changes: 9 additions & 0 deletions mysite/handlers/requestHandler/post.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from . import base
import requests


class PostHandler(base.Request):

def send(self):
response = requests.post(self.url, data=self.data, headers=self.headers)
return self.handleResponse(response)
1 change: 1 addition & 0 deletions mysite/static/js/calculator/Postfix_calculator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
$(".cal").click(function (e) {
var postfix = document.getElementById("Postfix").value;
postfix.replace(/\s/g, "");
var stack = [],
top = -1;

Loading