diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..adf4f43 --- /dev/null +++ b/.coveragerc @@ -0,0 +1,2 @@ +[run] +omit = ./venv/*, ms_business_central_api/*, *forms.py, *apps.py,*manage.py,*__init__.py,apps/*/migrations/*.py,*asgi*,*wsgi*,*admin.py,*urls.py,*settings.py, *gunicorn*, \ No newline at end of file diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..b45b8bc --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +setup.sh +cache.db \ No newline at end of file diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..7a7d62f --- /dev/null +++ b/.flake8 @@ -0,0 +1,31 @@ +[flake8] +extend-ignore = + # Comparison to true should be 'if cond is true:' or 'if cond:' + E712, + # Comparison to None should be 'cond is None:' (E711) + E711, + # Line break occurred before a binary operator (W503) + W503, + # Missing whitespace after ',', ';', or ':' (E231) + E231, + # Line too long (82 > 79 characters) (E501) + E501, + # E251 unexpected spaces around keyword / parameter equals + E251, + # E502 the backslash is redundant between brackets + E502, + # E128 continuation line under-indented for visual indent + E128, + # E125 continuation line with same indent as next logical line + E125, + # E131 continuation line unaligned for hanging indent + E131, + # E129 visually indented line with same indent as next logical line + E129, + # Multiple spaces after ',' (E241) + E241 +max-line-length = 99 +max-complexity = 19 +ban-relative-imports = true +select = B,C,E,F,N,W,I25 +exclude=*env \ No newline at end of file diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..e7d2c1b --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,30 @@ +name: Continuous Integration + +on: + pull_request: + types: [assigned, opened, synchronize, reopened] + +jobs: + pytest: + runs-on: ubuntu-latest + environment: CI Environment + steps: + - uses: actions/checkout@v2 + - name: Bring up Services and Run Tests + run: | + docker-compose -f docker-compose-pipeline.yml build + docker-compose -f docker-compose-pipeline.yml up -d + docker-compose -f docker-compose-pipeline.yml exec -T api pytest tests/ --cov --junit-xml=test-reports/report.xml --cov-report=xml --cov-fail-under=70 + echo "STATUS=$(cat pytest-coverage.txt | grep 'Required test' | awk '{ print $1 }')" >> $GITHUB_ENV + echo "FAILED=$(cat test-reports/report.xml | awk -F'=' '{print $5}' | awk -F' ' '{gsub(/"/, "", $1); print $1}')" >> $GITHUB_ENV + - name: Upload coverage reports to Codecov with GitHub Action + uses: codecov/codecov-action@v3 + - name: Pytest coverage comment + uses: MishaKav/pytest-coverage-comment@main + if: ${{ always() && github.ref != 'refs/heads/master' }} + with: + create-new-comment: true + junitxml-path: ./test-reports/report.xml + - name: Evaluate Coverage + if: ${{ (env.STATUS == 'FAIL') || (env.FAILED > 0) }} + run: exit 1 diff --git a/.gitignore b/.gitignore index 68bc17f..da1348e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,33 @@ +# DjangoQ debugger folder +debugger/ + # Byte-compiled / optimized / DLL files __pycache__/ +cache.db-journal *.py[cod] *$py.class +.DS_Store/ # C extensions *.so +# IDEs +.idea +.vscode + + +# Test files +test.py + +# Cache +demo_cache.sqlite +__pycache__ +.DS_Store + +# Setup File +setup.sh +local_run.sh + # Distribution / packaging .Python build/ @@ -20,6 +42,7 @@ parts/ sdist/ var/ wheels/ +pip-wheel-metadata/ share/python-wheels/ *.egg-info/ .installed.cfg @@ -49,7 +72,9 @@ coverage.xml *.py,cover .hypothesis/ .pytest_cache/ -cover/ +test-reports/ +pytest-coverage.txt +.pytest_cache/ # Translations *.mo @@ -72,7 +97,6 @@ instance/ docs/_build/ # PyBuilder -.pybuilder/ target/ # Jupyter Notebook @@ -83,9 +107,7 @@ profile_default/ ipython_config.py # pyenv -# For a library or package, you might want to ignore these files since the code is -# intended to run in multiple environments; otherwise, check them in: -# .python-version +.python-version # pipenv # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. @@ -94,30 +116,15 @@ ipython_config.py # install all needed dependencies. #Pipfile.lock -# poetry -# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. -# This is especially recommended for binary packages to ensure reproducibility, and is more -# commonly ignored for libraries. -# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control -#poetry.lock - -# pdm -# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. -#pdm.lock -# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it -# in version control. -# https://pdm.fming.dev/#use-with-ide -.pdm.toml - -# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +# PEP 582; used by e.g. github.com/David-OConnor/pyflow __pypackages__/ # Celery stuff celerybeat-schedule celerybeat.pid -# SageMath parsed files -*.sage.py +# businessCentralMath parsed files +*.businessCentral.py # Environments .env @@ -146,15 +153,13 @@ dmypy.json # Pyre type checker .pyre/ -# pytype static type analyzer -.pytype/ +# Local server +local_run.sh -# Cython debug symbols -cython_debug/ +#docker compose +docker-compose.yml -# PyCharm -# JetBrains specific template is maintained in a separate JetBrains.gitignore that can -# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore -# and can be added to the global gitignore or merged into this file. For a more nuclear -# option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ +# Cache db +cache.db +fyle_integrations_platform_connector/ +fyle_accounting_mappings/ \ No newline at end of file diff --git a/.pylintrc b/.pylintrc new file mode 100644 index 0000000..42094fb --- /dev/null +++ b/.pylintrc @@ -0,0 +1,586 @@ +[MASTER] + +# A comma-separated list of package or module names from where C extensions may +# be loaded. Extensions are loading into the active Python interpreter and may +# run arbitrary code. +extension-pkg-whitelist= + +# Add files or directories to the blacklist. They should be base names, not +# paths. +ignore=CVS + +# Add files or directories matching the regex patterns to the blacklist. The +# regex matches against base names, not paths. +ignore-patterns= + +# Python code to execute, usually for sys.path manipulation such as +# pygtk.require(). +#init-hook= + +# Use multiple processes to speed up Pylint. Specifying 0 will auto-detect the +# number of processors available to use. +jobs=1 + +# Control the amount of potential inferred values when inferring a single +# object. This can help the performance when dealing with large functions or +# complex, nested conditions. +limit-inference-results=100 + +# List of plugins (as comma separated values of python modules names) to load, +# usually to register additional checkers. +load-plugins= + +# Pickle collected data for later comparisons. +persistent=yes + +# Specify a configuration file. +#rcfile= + +# When enabled, pylint would attempt to guess common misconfiguration and emit +# user-friendly hints instead of false-positive error messages. +suggestion-mode=yes + +# Allow loading of arbitrary C extensions. Extensions are imported into the +# active Python interpreter and may run arbitrary code. +unsafe-load-any-extension=no + + +[MESSAGES CONTROL] + +# Only show warnings with the listed confidence levels. Leave empty to show +# all. Valid levels: HIGH, INFERENCE, INFERENCE_FAILURE, UNDEFINED. +confidence= + +# Disable the message, report, category or checker with the given id(s). You +# can either give multiple identifiers separated by comma (,) or put this +# option multiple times (only on the command line, not in the configuration +# file where it should appear only once). You can also use "--disable=all" to +# disable everything first and then reenable specific checks. For example, if +# you want to run only the similarities checker, you can use "--disable=all +# --enable=similarities". If you want to run only the classes checker, but have +# no Warning level messages displayed, use "--disable=all --enable=classes +# --disable=W". +disable=print-statement, + parameter-unpacking, + unpacking-in-except, + old-raise-syntax, + backtick, + long-suffix, + old-ne-operator, + old-octal-literal, + import-star-module-level, + non-ascii-bytes-literal, + raw-checker-failed, + bad-inline-option, + locally-disabled, + file-ignored, + suppressed-message, + useless-suppression, + deprecated-pragma, + use-symbolic-message-instead, + apply-builtin, + basestring-builtin, + buffer-builtin, + cmp-builtin, + coerce-builtin, + execfile-builtin, + file-builtin, + long-builtin, + raw_input-builtin, + reduce-builtin, + standarderror-builtin, + unicode-builtin, + xrange-builtin, + coerce-method, + delslice-method, + getslice-method, + setslice-method, + no-absolute-import, + old-division, + dict-iter-method, + dict-view-method, + next-method-called, + metaclass-assignment, + indexing-exception, + raising-string, + reload-builtin, + oct-method, + hex-method, + nonzero-method, + cmp-method, + input-builtin, + round-builtin, + intern-builtin, + unichr-builtin, + map-builtin-not-iterating, + zip-builtin-not-iterating, + range-builtin-not-iterating, + filter-builtin-not-iterating, + using-cmp-argument, + eq-without-hash, + div-method, + idiv-method, + rdiv-method, + exception-message-attribute, + invalid-str-codec, + sys-max-int, + bad-python3-import, + deprecated-string-function, + deprecated-str-translate-call, + deprecated-itertools-function, + deprecated-types-field, + next-method-defined, + dict-items-not-iterating, + dict-keys-not-iterating, + dict-values-not-iterating, + deprecated-operator-function, + deprecated-urllib-function, + xreadlines-attribute, + deprecated-sys-function, + exception-escape, + comprehension-escape, + no-self-use, + unused-argument, + invalid-name, + missing-docstring, + too-many-return-statements, + no-else-raise, + inconsistent-return-statements, + duplicate-code, + no-else-return, + no-member, + simplifiable-if-expression, + broad-except, + too-many-arguments, + too-many-locals, + too-few-public-methods, + super-with-arguments + +# Enable the message, report, category or checker with the given id(s). You can +# either give multiple identifier separated by comma (,) or put this option +# multiple time (only on the command line, not in the configuration file where +# it should appear only once). See also the "--disable" option for examples. +enable=c-extension-no-member + + +[REPORTS] + +# Python expression which should return a note less than 10 (10 is the highest +# note). You have access to the variables errors warning, statement which +# respectively contain the number of errors / warnings messages and the total +# number of statements analyzed. This is used by the global evaluation report +# (RP0004). +evaluation=10.0 - ((float(5 * error + warning + refactor + convention) / statement) * 10) + +# Template used to display messages. This is a python new-style format string +# used to format the message information. See doc for all details. +#msg-template= + +# Set the output format. Available formats are text, parseable, colorized, json +# and msvs (visual studio). You can also give a reporter class, e.g. +# mypackage.mymodule.MyReporterClass. +output-format=text + +# Tells whether to display a full report or only the messages. +reports=no + +# Activate the evaluation score. +score=yes + + +[REFACTORING] + +# Maximum number of nested blocks for function / method body +max-nested-blocks=5 + +# Complete name of functions that never returns. When checking for +# inconsistent-return-statements if a never returning function is called then +# it will be considered as an explicit return statement and no message will be +# printed. +never-returning-functions=sys.exit + + +[LOGGING] + +# Format style used to check logging format string. `old` means using % +# formatting, while `new` is for `{}` formatting. +logging-format-style=old + +# Logging modules to check that the string format arguments are in logging +# function parameter format. +logging-modules=logging + + +[SPELLING] + +# Limits count of emitted suggestions for spelling mistakes. +max-spelling-suggestions=4 + +# Spelling dictionary name. Available dictionaries: none. To make it working +# install python-enchant package.. +spelling-dict= + +# List of comma separated words that should not be checked. +spelling-ignore-words= + +# A path to a file that contains private dictionary; one word per line. +spelling-private-dict-file= + +# Tells whether to store unknown words to indicated private dictionary in +# --spelling-private-dict-file option instead of raising a message. +spelling-store-unknown-words=no + + +[MISCELLANEOUS] + +# List of note tags to take in consideration, separated by a comma. +notes=FIXME, + XXX, + TODO + + +[TYPECHECK] + +# List of decorators that produce context managers, such as +# contextlib.contextmanager. Add to this list to register other decorators that +# produce valid context managers. +contextmanager-decorators=contextlib.contextmanager + +# List of members which are set dynamically and missed by pylint inference +# system, and so shouldn't trigger E1101 when accessed. Python regular +# expressions are accepted. +generated-members= + +# Tells whether missing members accessed in mixin class should be ignored. A +# mixin class is detected if its name ends with "mixin" (case insensitive). +ignore-mixin-members=yes + +# Tells whether to warn about missing members when the owner of the attribute +# is inferred to be None. +ignore-none=yes + +# This flag controls whether pylint should warn about no-member and similar +# checks whenever an opaque object is returned when inferring. The inference +# can return multiple potential results while evaluating a Python object, but +# some branches might not be evaluated, which results in partial inference. In +# that case, it might be useful to still emit no-member and other checks for +# the rest of the inferred objects. +ignore-on-opaque-inference=yes + +# List of class names for which member attributes should not be checked (useful +# for classes with dynamically set attributes). This supports the use of +# qualified names. +ignored-classes=optparse.Values,thread._local,_thread._local + +# List of module names for which member attributes should not be checked +# (useful for modules/projects where namespaces are manipulated during runtime +# and thus existing member attributes cannot be deduced by static analysis. It +# supports qualified module names, as well as Unix pattern matching. +ignored-modules= + +# Show a hint with possible names when a member name was not found. The aspect +# of finding the hint is based on edit distance. +missing-member-hint=yes + +# The minimum edit distance a name should have in order to be considered a +# similar match for a missing member name. +missing-member-hint-distance=1 + +# The total number of similar names that should be taken in consideration when +# showing a hint for a missing member. +missing-member-max-choices=1 + + +[VARIABLES] + +# List of additional names supposed to be defined in builtins. Remember that +# you should avoid defining new builtins when possible. +additional-builtins= + +# Tells whether unused global variables should be treated as a violation. +allow-global-unused-variables=yes + +# List of strings which can identify a callback function by name. A callback +# name must start or end with one of those strings. +callbacks=cb_, + _cb + +# A regular expression matching the name of dummy variables (i.e. expected to +# not be used). +dummy-variables-rgx=_+$|(_[a-zA-Z0-9_]*[a-zA-Z0-9]+?$)|dummy|^ignored_|^unused_ + +# Argument names that match this expression will be ignored. Default to name +# with leading underscore. +ignored-argument-names=_.*|^ignored_|^unused_ + +# Tells whether we should check for unused import in __init__ files. +init-import=no + +# List of qualified module names which can have objects that can redefine +# builtins. +redefining-builtins-modules=six.moves,past.builtins,future.builtins,builtins,io + + +[FORMAT] + +# Expected format of line ending, e.g. empty (any line ending), LF or CRLF. +expected-line-ending-format= + +# Regexp for a line that is allowed to be longer than the limit. +ignore-long-lines=^\s*(# )??$ + +# Number of spaces of indent required inside a hanging or continued line. +indent-after-paren=4 + +# String used as indentation unit. This is usually " " (4 spaces) or "\t" (1 +# tab). +indent-string=' ' + +# Maximum number of characters on a single line. +max-line-length=120 + +# Maximum number of lines in a module. +max-module-lines=1000 + +# List of optional constructs for which whitespace checking is disabled. `dict- +# separator` is used to allow tabulation in dicts, etc.: {1 : 1,\n222: 2}. +# `trailing-comma` allows a space between comma and closing bracket: (a, ). +# `empty-line` allows space-only lines. +no-space-check=trailing-comma, + dict-separator + +# Allow the body of a class to be on the same line as the declaration if body +# contains single statement. +single-line-class-stmt=no + +# Allow the body of an if to be on the same line as the test if there is no +# else. +single-line-if-stmt=no + + +[SIMILARITIES] + +# Ignore comments when computing similarities. +ignore-comments=yes + +# Ignore docstrings when computing similarities. +ignore-docstrings=yes + +# Ignore imports when computing similarities. +ignore-imports=no + +# Minimum lines number of a similarity. +min-similarity-lines=4 + + +[BASIC] + +# Naming style matching correct argument names. +argument-naming-style=snake_case + +# Regular expression matching correct argument names. Overrides argument- +# naming-style. +#argument-rgx= + +# Naming style matching correct attribute names. +attr-naming-style=snake_case + +# Regular expression matching correct attribute names. Overrides attr-naming- +# style. +#attr-rgx= + +# Bad variable names which should always be refused, separated by a comma. +bad-names=foo, + bar, + baz, + toto, + tutu, + tata + +# Naming style matching correct class attribute names. +class-attribute-naming-style=any + +# Regular expression matching correct class attribute names. Overrides class- +# attribute-naming-style. +#class-attribute-rgx= + +# Naming style matching correct class names. +class-naming-style=PascalCase + +# Regular expression matching correct class names. Overrides class-naming- +# style. +#class-rgx= + +# Naming style matching correct constant names. +const-naming-style=UPPER_CASE + +# Regular expression matching correct constant names. Overrides const-naming- +# style. +#const-rgx= + +# Minimum line length for functions/classes that require docstrings, shorter +# ones are exempt. +docstring-min-length=-1 + +# Naming style matching correct function names. +function-naming-style=snake_case + +# Regular expression matching correct function names. Overrides function- +# naming-style. +#function-rgx= + +# Good variable names which should always be accepted, separated by a comma. +good-names=i, + j, + k, + ex, + Run, + _ + +# Include a hint for the correct naming format with invalid-name. +include-naming-hint=no + +# Naming style matching correct inline iteration names. +inlinevar-naming-style=any + +# Regular expression matching correct inline iteration names. Overrides +# inlinevar-naming-style. +#inlinevar-rgx= + +# Naming style matching correct method names. +method-naming-style=snake_case + +# Regular expression matching correct method names. Overrides method-naming- +# style. +#method-rgx= + +# Naming style matching correct module names. +module-naming-style=snake_case + +# Regular expression matching correct module names. Overrides module-naming- +# style. +#module-rgx= + +# Colon-delimited sets of names that determine each other's naming style when +# the name regexes allow several styles. +name-group= + +# Regular expression which should only match function or class names that do +# not require a docstring. +no-docstring-rgx=^_ + +# List of decorators that produce properties, such as abc.abstractproperty. Add +# to this list to register other decorators that produce valid properties. +# These decorators are taken in consideration only for invalid-name. +property-classes=abc.abstractproperty + +# Naming style matching correct variable names. +variable-naming-style=snake_case + +# Regular expression matching correct variable names. Overrides variable- +# naming-style. +#variable-rgx= + + +[STRING] + +# This flag controls whether the implicit-str-concat-in-sequence should +# generate a warning on implicit string concatenation in sequences defined over +# several lines. +check-str-concat-over-line-jumps=no + + +[IMPORTS] + +# Allow wildcard imports from modules that define __all__. +allow-wildcard-with-all=no + +# Analyse import fallback blocks. This can be used to support both Python 2 and +# 3 compatible code, which means that the block might have code that exists +# only in one or another interpreter, leading to false positives when analysed. +analyse-fallback-blocks=no + +# Deprecated modules which should not be used, separated by a comma. +deprecated-modules=optparse,tkinter.tix + +# Create a graph of external dependencies in the given file (report RP0402 must +# not be disabled). +ext-import-graph= + +# Create a graph of every (i.e. internal and external) dependencies in the +# given file (report RP0402 must not be disabled). +import-graph= + +# Create a graph of internal dependencies in the given file (report RP0402 must +# not be disabled). +int-import-graph= + +# Force import order to recognize a module as part of the standard +# compatibility libraries. +known-standard-library= + +# Force import order to recognize a module as part of a third party library. +known-third-party=enchant + + +[CLASSES] + +# List of method names used to declare (i.e. assign) instance attributes. +defining-attr-methods=__init__, + __new__, + setUp + +# List of member names, which should be excluded from the protected access +# warning. +exclude-protected=_asdict, + _fields, + _replace, + _source, + _make + +# List of valid names for the first argument in a class method. +valid-classmethod-first-arg=cls + +# List of valid names for the first argument in a metaclass class method. +valid-metaclass-classmethod-first-arg=cls + + +[DESIGN] + +# Maximum number of arguments for function / method. +max-args=5 + +# Maximum number of attributes for a class (see R0902). +max-attributes=7 + +# Maximum number of boolean expressions in an if statement. +max-bool-expr=5 + +# Maximum number of branch for function / method body. +max-branches=12 + +# Maximum number of locals for function / method body. +max-locals=20 + +# Maximum number of parents for a class (see R0901). +max-parents=7 + +# Maximum number of public methods for a class (see R0904). +max-public-methods=20 + +# Maximum number of return / yield for function / method body. +max-returns=6 + +# Maximum number of statements in function / method body. +max-statements=50 + +# Minimum number of public methods for a class (see R0903). +min-public-methods=2 + + +[EXCEPTIONS] + +# Exceptions that will emit a warning when being caught. Defaults to +# "BaseException, Exception". +overgeneral-exceptions=BaseException, + Exception diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..693bb2b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,38 @@ +# Pull python base image +FROM python:3.11-slim + +# set environment variables +ENV PYTHONDONTWRITEBYTECODE 1 +ENV PYTHONUNBUFFERED 1 + +RUN apt-get update && apt-get -y install libpq-dev gcc && apt-get install git -y --no-install-recommends + +ARG CI +RUN if [ "$CI" = "ENABLED" ]; then \ + apt-get update; \ + apt-get install lsb-release gnupg2 wget -y --no-install-recommends; \ + apt-cache search postgresql | grep postgresql; \ + sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'; \ + wget --no-check-certificate --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - ; \ + apt -y update; \ + apt-get install postgresql-15 -y --no-install-recommends; \ + fi + +# Installing requirements +COPY requirements.txt /tmp/requirements.txt +RUN pip install --upgrade pip && pip install -r /tmp/requirements.txt && pip install flake8 + + +# Copy Project to the container +RUN mkdir -p /fyle-ms-business-central-api +COPY . /fyle-ms-business-central-api/ +WORKDIR /fyle-ms-business-central-api + +# Do linting checks +RUN flake8 . + +# Expose development port +EXPOSE 8000 + +# Run development server +CMD /bin/bash run.sh diff --git a/apps/accounting_exports/__init__.py b/apps/accounting_exports/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/accounting_exports/apps.py b/apps/accounting_exports/apps.py new file mode 100644 index 0000000..01c7d6d --- /dev/null +++ b/apps/accounting_exports/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class AccountingExportsConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "apps.accounting_exports" diff --git a/apps/accounting_exports/migrations/__init__.py b/apps/accounting_exports/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/accounting_exports/models.py b/apps/accounting_exports/models.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/accounting_exports/serializers.py.py b/apps/accounting_exports/serializers.py.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/accounting_exports/urls.py b/apps/accounting_exports/urls.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/accounting_exports/views.py b/apps/accounting_exports/views.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/fyle/__init__.py b/apps/fyle/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/fyle/apps.py b/apps/fyle/apps.py new file mode 100644 index 0000000..febfd49 --- /dev/null +++ b/apps/fyle/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class FyleConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "apps.fyle" diff --git a/apps/fyle/helpers.py b/apps/fyle/helpers.py new file mode 100644 index 0000000..6cfb45d --- /dev/null +++ b/apps/fyle/helpers.py @@ -0,0 +1,52 @@ +import json +import requests +from django.conf import settings + + +def post_request(url, body, refresh_token=None): + """ + Create a HTTP post request. + """ + access_token = None + api_headers = { + 'Content-Type': 'application/json', + } + if refresh_token: + access_token = get_access_token(refresh_token) + api_headers['Authorization'] = 'Bearer {0}'.format(access_token) + + response = requests.post( + url, + headers=api_headers, + data=body + ) + + if response.status_code == 200: + return json.loads(response.text) + else: + raise Exception(response.text) + + +def get_access_token(refresh_token: str) -> str: + """ + Get access token from fyle + """ + api_data = { + 'grant_type': 'refresh_token', + 'refresh_token': refresh_token, + 'client_id': settings.FYLE_CLIENT_ID, + 'client_secret': settings.FYLE_CLIENT_SECRET + } + + return post_request(settings.FYLE_TOKEN_URI, body=json.dumps(api_data))['access_token'] + + +def get_cluster_domain(refresh_token: str) -> str: + """ + Get cluster domain name from fyle + :param refresh_token: (str) + :return: cluster_domain (str) + """ + cluster_api_url = '{0}/oauth/cluster/'.format(settings.FYLE_BASE_URL) + + return post_request(cluster_api_url, {}, refresh_token)['cluster_domain'] diff --git a/apps/fyle/migrations/__init__.py b/apps/fyle/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/fyle/models.py b/apps/fyle/models.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/fyle/serializers.py.py b/apps/fyle/serializers.py.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/fyle/urls.py b/apps/fyle/urls.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/fyle/views.py b/apps/fyle/views.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/mappings/__init__.py b/apps/mappings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/mappings/apps.py b/apps/mappings/apps.py new file mode 100644 index 0000000..7ef2ccf --- /dev/null +++ b/apps/mappings/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class MappingsConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "apps.mappings" diff --git a/apps/mappings/migrations/__init__.py b/apps/mappings/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/mappings/models.py b/apps/mappings/models.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/mappings/serializers.py.py b/apps/mappings/serializers.py.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/mappings/urls.py b/apps/mappings/urls.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/mappings/views.py b/apps/mappings/views.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/ms_business_central/__init__.py b/apps/ms_business_central/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/ms_business_central/apps.py b/apps/ms_business_central/apps.py new file mode 100644 index 0000000..3567bd1 --- /dev/null +++ b/apps/ms_business_central/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class MsBusinessCentralConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "apps.ms_business_central" diff --git a/apps/ms_business_central/migrations/__init__.py b/apps/ms_business_central/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/ms_business_central/models.py b/apps/ms_business_central/models.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/ms_business_central/serializers.py.py b/apps/ms_business_central/serializers.py.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/ms_business_central/urls.py b/apps/ms_business_central/urls.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/ms_business_central/views.py b/apps/ms_business_central/views.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/users/__init__.py b/apps/users/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/users/apps.py b/apps/users/apps.py new file mode 100644 index 0000000..37ba421 --- /dev/null +++ b/apps/users/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class UsersConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "apps.users" diff --git a/apps/users/migrations/0001_initial.py b/apps/users/migrations/0001_initial.py new file mode 100644 index 0000000..cabf21e --- /dev/null +++ b/apps/users/migrations/0001_initial.py @@ -0,0 +1,31 @@ +# Generated by Django 4.1.2 on 2023-10-29 17:56 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='User', + fields=[ + ('password', models.CharField(max_length=128, verbose_name='password')), + ('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')), + ('id', models.AutoField(primary_key=True, serialize=False)), + ('email', models.EmailField(max_length=255, verbose_name='email address')), + ('user_id', models.CharField(max_length=255, unique=True, verbose_name='Fyle user id')), + ('full_name', models.CharField(max_length=255, verbose_name='full name')), + ('active', models.BooleanField(default=True)), + ('staff', models.BooleanField(default=False)), + ('admin', models.BooleanField(default=False)), + ], + options={ + 'db_table': 'users', + }, + ), + ] diff --git a/apps/users/migrations/__init__.py b/apps/users/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/users/models.py b/apps/users/models.py new file mode 100644 index 0000000..5397506 --- /dev/null +++ b/apps/users/models.py @@ -0,0 +1,21 @@ +from django.db import models +from django.contrib.auth.models import AbstractBaseUser + + +class User(AbstractBaseUser): + id = models.AutoField(primary_key=True) + email = models.EmailField( + verbose_name='email address', + max_length=255 + ) + user_id = models.CharField(verbose_name='Fyle user id', max_length=255, unique=True) + full_name = models.CharField(verbose_name='full name', max_length=255) + active = models.BooleanField(default=True) + staff = models.BooleanField(default=False) + admin = models.BooleanField(default=False) + + USERNAME_FIELD = 'user_id' + REQUIRED_FIELDS = ['full_name', 'email'] + + class Meta: + db_table = 'users' diff --git a/apps/users/serializers.py.py b/apps/users/serializers.py.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/users/urls.py b/apps/users/urls.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/users/views.py b/apps/users/views.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/workspaces/__init__.py b/apps/workspaces/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/workspaces/apps.py b/apps/workspaces/apps.py new file mode 100644 index 0000000..bfc3b16 --- /dev/null +++ b/apps/workspaces/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class WorkspacesConfig(AppConfig): + default_auto_field = "django.db.models.BigAutoField" + name = "apps.workspaces" diff --git a/apps/workspaces/migrations/__init__.py b/apps/workspaces/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/apps/workspaces/models.py b/apps/workspaces/models.py new file mode 100644 index 0000000..f1852c6 --- /dev/null +++ b/apps/workspaces/models.py @@ -0,0 +1,171 @@ +from django.db import models +from django.contrib.auth import get_user_model + +from ms_business_central_api.models.fields import ( + StringNotNullField, + CustomDateTimeField, + StringOptionsField, + TextNotNullField, + StringNullField, + BooleanTrueField +) + +User = get_user_model() + +ONBOARDING_STATE_CHOICES = ( + ('CONNECTION', 'CONNECTION'), + ('EXPORT_SETTINGS', 'EXPORT_SETTINGS'), + ('IMPORT_SETTINGS', 'IMPORT_SETTINGS'), + ('ADVANCED_CONFIGURATION', 'ADVANCED_CONFIGURATION'), + ('COMPLETE', 'COMPLETE') +) + + +def get_default_onboarding_state(): + return 'EXPORT_SETTINGS' + + +class Workspace(models.Model): + """ + Workspace model + """ + id = models.AutoField(primary_key=True) + name = StringNotNullField(help_text='Name of the workspace') + user = models.ManyToManyField(User, help_text='Reference to users table') + org_id = models.CharField(max_length=255, help_text='org id', unique=True) + last_synced_at = CustomDateTimeField(help_text='Datetime when expenses were pulled last') + ccc_last_synced_at = CustomDateTimeField(help_text='Datetime when ccc expenses were pulled last') + source_synced_at = CustomDateTimeField(help_text='Datetime when source dimensions were pulled') + destination_synced_at = CustomDateTimeField(help_text='Datetime when destination dimensions were pulled') + onboarding_state = StringOptionsField( + max_length=50, choices=ONBOARDING_STATE_CHOICES, default=get_default_onboarding_state, + help_text='Onboarding status of the workspace' + ) + ms_business_central_accounts_last_synced_at = CustomDateTimeField(help_text='ms business central accounts last synced at time') + created_at = models.DateTimeField(auto_now_add=True, help_text='Created at datetime') + updated_at = models.DateTimeField(auto_now=True, help_text='Updated at datetime') + + class Meta: + db_table = 'workspaces' + + +class BaseModel(models.Model): + workspace = models.OneToOneField(Workspace, on_delete=models.PROTECT, help_text='Reference to Workspace model') + created_at = models.DateTimeField(auto_now_add=True, help_text='Created at datetime') + updated_at = models.DateTimeField(auto_now=True, help_text='Updated at datetime') + + class Meta: + abstract = True + + +class BaseForeignWorkspaceModel(models.Model): + workspace = models.ForeignKey(Workspace, on_delete=models.PROTECT, help_text='Reference to Workspace model') + created_at = models.DateTimeField(auto_now_add=True, help_text='Created at datetime') + updated_at = models.DateTimeField(auto_now=True, help_text='Updated at datetime') + + class Meta: + abstract = True + + +class FyleCredential(BaseModel): + """ + Table to store Fyle credentials + """ + id = models.AutoField(auto_created=True, primary_key=True, verbose_name='ID', serialize=False) + refresh_token = TextNotNullField(help_text='Fyle refresh token') + cluster_domain = StringNullField(help_text='Fyle cluster domain') + + class Meta: + db_table = 'fyle_credentials' + + +# Reimbursable Expense Choices +REIMBURSABLE_EXPENSE_EXPORT_TYPE_CHOICES = ( + ('PURCHASE_INVOICE', 'PURCHASE_INVOICE'), + ('JOURNAL_ENTRY', 'JOURNAL_ENTRY') +) + +REIMBURSABLE_EXPENSE_STATE_CHOICES = ( + ('PAYMENT_PROCESSING', 'PAYMENT_PROCESSING'), + ('CLOSED', 'CLOSED') +) + +REIMBURSABLE_EXPENSES_GROUPED_BY_CHOICES = ( + ('REPORT', 'report_id'), + ('EXPENSE', 'expense_id') +) + +REIMBURSABLE_EXPENSES_DATE_TYPE_CHOICES = ( + ('LAST_SPENT_AT', 'last_spent_at'), + ('CREATED_AT', 'created_at'), + ('SPENT_AT', 'spent_at') +) + +# Credit Card Expense Choices +CREDIT_CARD_EXPENSE_EXPORT_TYPE_CHOICES = ( + ('JOURNAL_ENTRY', 'JOURNAL_ENTRY'), +) + +CREDIT_CARD_EXPENSE_STATE_CHOICES = ( + ('APPROVED', 'APPROVED'), + ('PAYMENT_PROCESSING', 'PAYMENT_PROCESSING'), + ('PAID', 'PAID') +) + +CREDIT_CARD_EXPENSES_GROUPED_BY_CHOICES = ( + ('REPORT', 'report_id'), + ('EXPENSE', 'expense_id') +) + +CREDIT_CARD_EXPENSES_DATE_TYPE_CHOICES = ( + ('LAST_SPENT_AT', 'last_spent_at'), + ('POSTED_AT', 'posted_at'), + ('CREATED_AT', 'created_at') +) + + +class ExportSetting(BaseModel): + """ + Table to store export settings + """ + # Reimbursable Expenses Export Settings + id = models.AutoField(auto_created=True, primary_key=True, verbose_name='ID', serialize=False) + reimbursable_expenses_export_type = StringOptionsField( + choices=REIMBURSABLE_EXPENSE_EXPORT_TYPE_CHOICES, + ) + default_bank_account_name = StringNullField(help_text='Bank account name') + default_back_account_id = StringNullField(help_text='Bank Account ID') + reimbursable_expense_state = StringOptionsField( + choices=REIMBURSABLE_EXPENSE_STATE_CHOICES + ) + reimbursable_expense_date = StringOptionsField( + choices=REIMBURSABLE_EXPENSES_DATE_TYPE_CHOICES + ) + reimbursable_expense_grouped_by = StringOptionsField( + choices=REIMBURSABLE_EXPENSES_GROUPED_BY_CHOICES + ) + # Credit Card Expenses Export Settings + credit_card_expense_export_type = StringOptionsField( + choices=CREDIT_CARD_EXPENSE_EXPORT_TYPE_CHOICES + ) + credit_card_expense_state = StringOptionsField( + choices=CREDIT_CARD_EXPENSE_STATE_CHOICES + ) + default_reimbursable_account_name = StringNullField(help_text='Reimbursable account name') + default_reimbursable_account_id = StringNullField(help_text='Reimbursable Account ID') + default_ccc_credit_card_account_name = StringNullField(help_text='CCC Credit card account name') + default_ccc_credit_card_account_id = StringNullField(help_text='CCC Credit Card Account ID') + default_reimbursable_credit_card_account_name = StringNullField(help_text='Reimbursable Credit card account name') + default_reimbursable_credit_card_account_id = StringNullField(help_text='Reimbursable Credit card account name') + credit_card_expense_grouped_by = StringOptionsField( + choices=CREDIT_CARD_EXPENSES_GROUPED_BY_CHOICES + ) + credit_card_expense_date = StringOptionsField( + choices=CREDIT_CARD_EXPENSES_DATE_TYPE_CHOICES + ) + default_vendor_name = StringNullField(help_text='default Vendor Name') + default_vendor_id = StringNullField(help_text='default Vendor Id') + auto_map_employees = BooleanTrueField(help_text='Auto map employees') + + class Meta: + db_table = 'export_settings' diff --git a/apps/workspaces/serializers.py b/apps/workspaces/serializers.py new file mode 100644 index 0000000..744c8dc --- /dev/null +++ b/apps/workspaces/serializers.py @@ -0,0 +1,98 @@ +""" +Workspace Serializers +""" +from django.core.cache import cache +from rest_framework import serializers +from fyle_rest_auth.helpers import get_fyle_admin +from fyle_rest_auth.models import AuthToken + +from ms_business_central_api.utils import assert_valid +from apps.workspaces.models import ( + Workspace, + FyleCredential, + ExportSetting +) +from apps.users.models import User +from apps.fyle.helpers import get_cluster_domain + + +class WorkspaceSerializer(serializers.ModelSerializer): + """ + Workspace serializer + """ + class Meta: + model = Workspace + fields = '__all__' + read_only_fields = ('id', 'name', 'org_id', 'created_at', 'updated_at', 'user') + + def create(self, validated_data): + """ + Update workspace + """ + access_token = self.context['request'].META.get('HTTP_AUTHORIZATION') + user = self.context['request'].user + + # Getting user profile using the access token + fyle_user = get_fyle_admin(access_token.split(' ')[1], None) + + # getting name, org_id, currency of Fyle User + name = fyle_user['data']['org']['name'] + org_id = fyle_user['data']['org']['id'] + + # Checking if workspace already exists + workspace = Workspace.objects.filter(org_id=org_id).first() + + if workspace: + # Adding user relation to workspace + workspace.user.add(User.objects.get(user_id=user)) + cache.delete(str(workspace.id)) + else: + workspace = Workspace.objects.create( + name=name, + org_id=org_id, + ) + + workspace.user.add(User.objects.get(user_id=user)) + + auth_tokens = AuthToken.objects.get(user__user_id=user) + + cluster_domain = get_cluster_domain(auth_tokens.refresh_token) + + FyleCredential.objects.update_or_create( + refresh_token=auth_tokens.refresh_token, + workspace_id=workspace.id, + cluster_domain=cluster_domain + ) + + return workspace + + +class ExportSettingsSerializer(serializers.ModelSerializer): + """ + Export Settings serializer + """ + class Meta: + model = ExportSetting + fields = '__all__' + read_only_fields = ('id', 'workspace', 'created_at', 'updated_at') + + def create(self, validated_data): + """ + Create Export Settings + """ + assert_valid(validated_data, 'Body cannot be null') + workspace_id = self.context['request'].parser_context.get('kwargs').get('workspace_id') + + export_settings, _ = ExportSetting.objects.update_or_create( + workspace_id=workspace_id, + defaults=validated_data + ) + + # Update workspace onboarding state + workspace = export_settings.workspace + + if workspace.onboarding_state == 'EXPORT_SETTINGS': + workspace.onboarding_state = 'IMPORT_SETTINGS' + workspace.save() + + return export_settings diff --git a/apps/workspaces/urls.py b/apps/workspaces/urls.py new file mode 100644 index 0000000..d7a216b --- /dev/null +++ b/apps/workspaces/urls.py @@ -0,0 +1,21 @@ +from django.urls import path + +from apps.workspaces.views import ( + ReadyView, + WorkspaceView, + ExportSettingView +) + + +workspace_app_paths = [ + path('', WorkspaceView.as_view(), name='workspaces'), + path('ready/', ReadyView.as_view(), name='ready'), + path('/export_settings/', ExportSettingView.as_view(), name='export-settings'), + +] + +other_app_paths = [] + +urlpatterns = [] +urlpatterns.extend(workspace_app_paths) +urlpatterns.extend(other_app_paths) diff --git a/apps/workspaces/views.py b/apps/workspaces/views.py new file mode 100644 index 0000000..4117e55 --- /dev/null +++ b/apps/workspaces/views.py @@ -0,0 +1,82 @@ +import logging +from django.contrib.auth import get_user_model + +from rest_framework import generics +from rest_framework.views import Response, status + +from fyle_rest_auth.utils import AuthUtils + + +from ms_business_central_api.utils import assert_valid +from apps.workspaces.models import ( + Workspace, + ExportSetting +) +from apps.workspaces.serializers import ( + WorkspaceSerializer, + ExportSettingsSerializer +) + + +logger = logging.getLogger(__name__) +logger.level = logging.INFO + +User = get_user_model() +auth_utils = AuthUtils() + + +class WorkspaceView(generics.CreateAPIView, generics.RetrieveAPIView): + """ + Create Retrieve Workspaces + """ + serializer_class = WorkspaceSerializer + + def get_object(self): + """ + return workspace object for the given org_id + """ + user_id = self.request.user + + org_id = self.request.query_params.get('org_id') + + assert_valid(org_id is not None, 'org_id is missing') + + workspace = Workspace.objects.filter(org_id=org_id, user__user_id=user_id).first() + + assert_valid( + workspace is not None, + 'Workspace not found or the user does not have access to workspaces' + ) + + return workspace + + +class ReadyView(generics.RetrieveAPIView): + """ + Ready call to check if the api is ready + """ + authentication_classes = [] + permission_classes = [] + + def get(self, request, *args, **kwargs): + """ + Ready call + """ + Workspace.objects.first() + + return Response( + data={ + 'message': 'Ready' + }, + status=status.HTTP_200_OK + ) + + +class ExportSettingView(generics.CreateAPIView, generics.RetrieveAPIView): + """ + Retrieve or Create Export Settings + """ + serializer_class = ExportSettingsSerializer + lookup_field = 'workspace_id' + + queryset = ExportSetting.objects.all() diff --git a/centralized_run.sh b/centralized_run.sh new file mode 100644 index 0000000..426403a --- /dev/null +++ b/centralized_run.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +# Run db migrations +python manage.py migrate + +# Creating the cache table +python manage.py createcachetable --database cache_db + +# Running development server +gunicorn -c gunicorn_config.py ms_business_central_api.wsgi -b 0.0.0.0:8009 \ No newline at end of file diff --git a/docker-compose-pipeline.yml b/docker-compose-pipeline.yml new file mode 100644 index 0000000..7246776 --- /dev/null +++ b/docker-compose-pipeline.yml @@ -0,0 +1,49 @@ +version: '3.7' + +services: + api: + build: + context: ./ + args: + CI: ENABLED + entrypoint: bash run.sh + restart: unless-stopped + volumes: + - ./:/fyle-ms-business-central-api + depends_on: + - db + environment: + SECRET_KEY: thisisthedjangosecretkey + ALLOWED_HOSTS: "*" + DEBUG: "False" + NO_WORKERS: 1 + API_URL: ${API_URL} + DATABASE_URL: postgres://postgres:postgres@db:5432/ms_business_central_db + FYLE_BASE_URL: 'https://sample.fyle.tech' + FYLE_CLIENT_ID: 'sample' + FYLE_CLIENT_SECRET: 'sample' + FYLE_REFRESH_TOKEN: 'sample.sample.sample' + MS_BUSINESS_CENTRAL_USER_PASSWORD: 'sample' + MS_BUSINESS_CENTRAL_USER_SENDER_ID: 'sample' + ENCRYPTION_KEY: ${ENCRYPTION_KEY} + FYLE_TOKEN_URI: 'https://sample.fyle.tech' + FYLE_SERVER_URL: 'https://sample.fyle.tech' + FYLE_JOBS_URL: 'https://sample.fyle.tech' + DB_NAME: ms_business_central_db + DB_USER: postgres + DB_PASSWORD: postgres + DB_HOST: db + DB_PORT: 5432 + db: + image: "postgres:15" + environment: + POSTGRES_DB: dummy + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + volumes: + - data:/var/lib/postgresql/data/ + - ./init.sql:/docker-entrypoint-initdb.d/init.sql + +volumes: + api: + data: \ No newline at end of file diff --git a/gunicorn_config.py b/gunicorn_config.py new file mode 100644 index 0000000..e84bb09 --- /dev/null +++ b/gunicorn_config.py @@ -0,0 +1,122 @@ +import os +from psycogreen.gevent import patch_psycopg + +# https://docs.gunicorn.org/en/stable/settings.html + +port_number = 8000 +bind = '0.0.0.0:{0}'.format(port_number) +proc_name = 'ms_business_central' + +# The maximum number of pending connections. +backlog = int(os.environ.get('GUNICORN_BACKLOG', 2048)) + +# The number of worker processes for handling requests. +workers = int(os.environ.get('GUNICORN_NUMBER_WORKERS', 2)) + +# Workers silent for more than this many seconds are killed and restarted. +timeout = int(os.environ.get('GUNICORN_WORKER_TIMEOUT', 60)) + +# The number of seconds to wait for requests on a Keep-Alive connection. +keepalive = int(os.environ.get('GUNICORN_KEEPALIVE', 2)) + +# The maximum number of simultaneous clients. +worker_connections = int(os.environ.get('GUNICORN_WORKER_CONNECTIONS', 1000)) + +# The granularity of Error log outputs. +loglevel = os.environ.get('GUNICORN_LOG _LEVEL', 'debug') + +# The type of workers to use. +worker_class = os.environ.get('GUNICORN_WORKER_CLASS', 'gevent') + +# The number of worker threads for handling requests. +threads = int(os.environ.get('GUNICORN_NUMBER_WORKER_THREADS', 1)) + +# The maximum number of requests a worker will process before restarting. +max_requests = int(os.environ.get('GUNICORN_MAX_REQUESTS', 20)) + +# The jitter causes the restart per worker to be randomized by randint(0, max_requests_jitter). +max_requests_jitter = int(os.environ.get('GUNICORN_MAX_REQUESTS_JITTER', 20)) + +# Timeout for graceful workers restart. +graceful_timeout = int(os.environ.get('GUNICORN_WORKER_GRACEFUL_TIMEOUT', 5)) + +# Restart workers when code changes. +reload = True + +# The maximum size of HTTP request line in bytes. +limit_request_line = 0 + +# Install a trace function that spews every line executed by the server. +spew = False + +# Detaches the server from the controlling terminal and enters the background. +daemon = False + +pidfile = None +umask = 0 +user = None +group = None +tmp_upload_dir = None + +errorlog = '-' +accesslog = '-' +access_log_format = '%({X-Real-IP}i)s - - - %(t)s "%(r)s" "%(f)s" "%(a)s" %({X-Request-Id}i)s %(L)s %(b)s %(s)s' + + +def post_fork(server, worker): + patch_psycopg() + server.log.info("Worker spawned (pid: %s)", worker.pid) + + +def pre_fork(server, worker): # noqa + pass + + +def pre_exec(server): + server.log.info("Forked child, re-executing.") + + +def when_ready(server): + server.log.info("Server is ready. Spawning workers") + + +def worker_int(worker): + worker.log.info("worker received INT or QUIT signal") + + # get traceback info + import threading + import sys + import traceback + id2name = dict([(th.ident, th.name) for th in threading.enumerate()]) + code = [] + for thread_id, stack in sys._current_frames().items(): + code.append("\n# Thread: %s(%d)" % (id2name.get(thread_id, ""), + thread_id)) + for filename, line_no, name, line in traceback.extract_stack(stack): + code.append('File: "%s", line %d, in %s' % (filename, + line_no, name)) + if line: + code.append(" %s" % (line.strip())) + worker.log.debug("\n".join(code)) + + +def worker_abort(worker): + worker.log.info("worker received SIGABRT signal") + + +def child_exit(server, worker): + server.log.info("server: child_exit is called") + worker.log.info("worker: child_exit is called") + + +def worker_exit(server, worker): + server.log.info("server: worker_exit is called") + worker.log.info("worker: worker_exit is called") + + +def nworkers_changed(server, new_value, old_value): + server.log.info("server: nworkers_changed is called with new_value: %s old_value: %s", new_value, old_value) + + +def on_exit(server): + server.log.info("server: on_exit is called") diff --git a/init.sql b/init.sql new file mode 100644 index 0000000..7501be2 --- /dev/null +++ b/init.sql @@ -0,0 +1 @@ +CREATE DATABASE ms_business_central_db; \ No newline at end of file diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..6fbef40 --- /dev/null +++ b/manage.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" +import os +import sys + + +def main(): + """Run administrative tasks.""" + os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ms_business_central_api.settings") + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) + + +if __name__ == "__main__": + main() diff --git a/ms_business_central_api/__init__.py b/ms_business_central_api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ms_business_central_api/asgi.py b/ms_business_central_api/asgi.py new file mode 100644 index 0000000..2b3ae87 --- /dev/null +++ b/ms_business_central_api/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for ms_business_central_api project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.2/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ms_business_central_api.settings") + +application = get_asgi_application() diff --git a/ms_business_central_api/cache_router.py b/ms_business_central_api/cache_router.py new file mode 100644 index 0000000..be58083 --- /dev/null +++ b/ms_business_central_api/cache_router.py @@ -0,0 +1,20 @@ +app_to_database = { + 'django_cache': 'cache_db', +} + + +class CacheRouter: + + def db_for_read(self, model, **hints): + return app_to_database.get(model._meta.app_label, None) + + def db_for_write(self, model, **hints): + return app_to_database.get(model._meta.app_label, None) + + def allow_syncdb(self, db, model): + _db = app_to_database.get(model._meta.app_label, None) + return db == _db if _db else None + + def allow_migrate(self, db, app_label, model_name=None, **hints): + _db = app_to_database.get(app_label, None) + return db == _db if _db else None diff --git a/ms_business_central_api/models/__init__.py b/ms_business_central_api/models/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ms_business_central_api/models/fields.py b/ms_business_central_api/models/fields.py new file mode 100644 index 0000000..5fc1da4 --- /dev/null +++ b/ms_business_central_api/models/fields.py @@ -0,0 +1,133 @@ +from django.db import models +from django.core.validators import EmailValidator + + +class StringNotNullField(models.CharField): + description = "Custom String with Not Null" + + def __init__(self, *args, **kwargs): + kwargs['max_length'] = kwargs.get('max_length', 255) + kwargs['null'] = False # Ensure the field is not nullable + kwargs['help_text'] = kwargs.get('help_text', 'string field with null false') + super(StringNotNullField, self).__init__(*args, **kwargs) + + +class StringNullField(models.CharField): + description = "Custom String with Null" + + def __init__(self, *args, **kwargs): + kwargs['max_length'] = kwargs.get('max_length', 255) + kwargs['null'] = True # Allow the field to be nullable + kwargs['help_text'] = kwargs.get('help_text', 'string field with null True') + super(StringNullField, self).__init__(*args, **kwargs) + + +class IntegerNullField(models.IntegerField): + description = "Custom Integer with Null" + + def __init__(self, *args, **kwargs): + kwargs['null'] = True # Allow the field to be nullable + kwargs['help_text'] = kwargs.get('help_text', 'Integer field with null True') + super(IntegerNullField, self).__init__(*args, **kwargs) + + +class IntegerNotNullField(models.IntegerField): + description = "Custom Integer with Not Null" + + def __init__(self, *args, **kwargs): + kwargs['null'] = False # Ensure the field is not nullable + kwargs['help_text'] = kwargs.get('help_text', 'Integer field with null false') + super(IntegerNotNullField, self).__init__(*args, **kwargs) + + +class CustomJsonField(models.JSONField): + description = "Custom Json Field with Null" + + def __init__(self, *args, **kwargs): + kwargs['default'] = list # Set a default value for the JSON field + kwargs['null'] = True # Allow the field to be nullable + kwargs['help_text'] = kwargs.get('help_text', 'Json field with null true') + super(CustomJsonField, self).__init__(*args, **kwargs) + + +class CustomDateTimeField(models.DateTimeField): + description = "Custom DateTime Field with Auto-Add Now" + + def __init__(self, *args, **kwargs): + kwargs['null'] = True # Allow the field to be nullable + super(CustomDateTimeField, self).__init__(*args, **kwargs) + + +class TextNotNullField(models.TextField): + description = "Custom Text Field with Not Null" + + def __init__(self, *args, **kwargs): + kwargs['null'] = False # Ensure the field is not nullable + kwargs['help_text'] = kwargs.get('help_text', 'text field with null false') + super(TextNotNullField, self).__init__(*args, **kwargs) + + +class StringOptionsField(models.CharField): + description = "Custom String Field with Options" + + def __init__(self, *args, **kwargs): + choices = kwargs.pop('choices', []) # Retrieve choices from kwargs + max_length = kwargs.pop('max_length', 255) # Retrieve max_length from kwargs + default = kwargs.pop('default', '') # Retrieve default value from kwargs + kwargs['help_text'] = kwargs.get('help_text', 'string field with options') + kwargs['null'] = True # Allow the field to be nullable + super(StringOptionsField, self).__init__(max_length=max_length, choices=choices, default=default, **kwargs) + + +class IntegerOptionsField(models.IntegerField): + description = "Custom String Field with Options" + + def __init__(self, *args, **kwargs): + choices = kwargs.pop('choices', []) # Retrieve choices from kwargs + default = kwargs.pop('default', '') # Retrieve default value from kwargs + kwargs['help_text'] = kwargs.get('help_text', 'string field with options') + kwargs['null'] = True # Allow the field to be nullable + super(IntegerOptionsField, self).__init__(choices=choices, default=default, **kwargs) + + +class BooleanFalseField(models.BooleanField): + description = "Custom Boolean Field with Default True" + + def __init__(self, *args, **kwargs): + kwargs['default'] = True # Set the default value to True + super(BooleanFalseField, self).__init__(*args, **kwargs) + + def toggle(self, instance): + value = getattr(instance, self.attname) + setattr(instance, self.attname, not value) + instance.save() + + +class BooleanTrueField(models.BooleanField): + description = "Custom Boolean Field with Default True" + + def __init__(self, *args, **kwargs): + kwargs['default'] = True # Set the default value to True + super(BooleanTrueField, self).__init__(*args, **kwargs) + + def toggle(self, instance): + value = getattr(instance, self.attname) + setattr(instance, self.attname, not value) + instance.save() + + +class CustomEmailField(models.EmailField): + description = "Custom Email Field" + + def __init__(self, *args, **kwargs): + kwargs['max_length'] = kwargs.get('max_length', 254) # Set a default max length for email addresses + kwargs['validators'] = [EmailValidator()] # Add email validation + super(CustomEmailField, self).__init__(*args, **kwargs) + + +class FloatNullField(models.FloatField): + description = "Custom Float Field with Null" + + def __init__(self, *args, **kwargs): + kwargs['null'] = True # Allow the field to be nullable + super(FloatNullField, self).__init__(*args, **kwargs) diff --git a/ms_business_central_api/settings.py b/ms_business_central_api/settings.py new file mode 100644 index 0000000..35bb5fe --- /dev/null +++ b/ms_business_central_api/settings.py @@ -0,0 +1,218 @@ +""" +Django settings for ms_business_central_api project. + +Generated by 'django-admin startproject' using Django 4.2.6. + +For more information on this file, see +https://docs.djangoproject.com/en/4.2/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/4.2/ref/settings/ +""" + +import os +import sys + +import dj_database_url + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = os.environ.get('SECRET_KEY') + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True if os.environ.get('DEBUG') == 'True' else False + +ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS').split(',') + +ENCRYPTION_KEY = os.environ.get('ENCRYPTION_KEY') + + +# Application definition + +INSTALLED_APPS = [ + "django.contrib.admin", + "django.contrib.auth", + "django.contrib.contenttypes", + "django.contrib.sessions", + "django.contrib.messages", + "django.contrib.staticfiles", + + # Installed Apps + 'rest_framework', + 'corsheaders', + 'django_q', + 'fyle_rest_auth', + # 'fyle_accounting_mappings', + + # User Created Apps + 'apps.users', + 'apps.workspaces', + 'apps.fyle', + 'apps.ms_business_central', + 'apps.accounting_exports', + 'apps.mappings' +] + +MIDDLEWARE = [ + 'request_logging.middleware.LoggingMiddleware', + 'corsheaders.middleware.CorsMiddleware', + "django.middleware.security.SecurityMiddleware", + "django.contrib.sessions.middleware.SessionMiddleware", + "django.middleware.common.CommonMiddleware", + "django.middleware.csrf.CsrfViewMiddleware", + "django.contrib.auth.middleware.AuthenticationMiddleware", + "django.contrib.messages.middleware.MessageMiddleware", + "django.middleware.clickjacking.XFrameOptionsMiddleware", + # "ms_business_central_api.logging_middleware.ErrorHandlerMiddleware", +] + +ROOT_URLCONF = "ms_business_central_api.urls" +APPEND_SLASH = False + +AUTH_USER_MODEL = 'users.User' + +TEMPLATES = [ + { + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [], + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.debug", + "django.template.context_processors.request", + "django.contrib.auth.context_processors.auth", + "django.contrib.messages.context_processors.messages", + ], + }, + }, +] + +FYLE_REST_AUTH_SERIALIZERS = { + 'USER_DETAILS_SERIALIZER': 'apps.users.serializers.UserSerializer' +} + +FYLE_REST_AUTH_SETTINGS = { + 'async_update_user': True +} + +# REST_FRAMEWORK = { +# 'DEFAULT_PERMISSION_CLASSES': ( +# 'rest_framework.permissions.IsAuthenticated', +# 'apps.workspaces.permissions.WorkspacePermissions' +# ), +# 'DEFAULT_AUTHENTICATION_CLASSES': ( +# 'fyle_rest_auth.authentication.FyleJWTAuthentication', +# ), +# 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination', +# 'PAGE_SIZE': 100 +# } + +CACHES = { + 'default': { + 'BACKEND': 'django.core.cache.backends.db.DatabaseCache', + 'LOCATION': 'auth_cache', + } +} + +Q_CLUSTER = { + 'name': 'ms_business_central_api', + 'save_limit': 0, + 'retry': 14400, + 'timeout': 3600, + 'catch_up': False, + 'workers': 4, + # How many tasks are kept in memory by a single cluster. + # Helps balance the workload and the memory overhead of each individual cluster + 'queue_limit': 10, + 'cached': False, + 'orm': 'default', + 'ack_failures': True, + 'poll': 1, + 'max_attempts': 1, + 'attempt_count': 1, + # The number of tasks a worker will process before recycling. + # Useful to release memory resources on a regular basis. + 'recycle': 50, + # The maximum resident set size in kilobytes before a worker will recycle and release resources. + # Useful for limiting memory usage. + 'max_rss': 100000 # 100mb +} + + +WSGI_APPLICATION = 'ms_business_central_api.wsgi.application' + +SERVICE_NAME = os.environ.get('SERVICE_NAME') + + +# Database +# https://docs.djangoproject.com/en/3.0/ref/settings/#databases +# Defaulting django engine for qcluster +if len(sys.argv) > 0 and sys.argv[1] == 'qcluster': + DATABASES = { + 'default': dj_database_url.config() + } +else: + DATABASES = { + 'default': dj_database_url.config(engine='django_db_geventpool.backends.postgresql_psycopg2') + } + +DATABASES['cache_db'] = { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': 'cache.db' +} + +DATABASE_ROUTERS = ['ms_business_central_api.cache_router.CacheRouter'] + + +# Password validation +# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", + }, + { + "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", + }, +] + +DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' + +# Fyle Settings +API_URL = os.environ.get('API_URL') +FYLE_TOKEN_URI = os.environ.get('FYLE_TOKEN_URI') +FYLE_CLIENT_ID = os.environ.get('FYLE_CLIENT_ID') +FYLE_CLIENT_SECRET = os.environ.get('FYLE_CLIENT_SECRET') +FYLE_BASE_URL = os.environ.get('FYLE_BASE_URL') +FYLE_JOBS_URL = os.environ.get('FYLE_JOBS_URL') +FYLE_APP_URL = os.environ.get('APP_URL') +FYLE_EXPENSE_URL = os.environ.get('FYLE_APP_URL') + +# Internationalization +# https://docs.djangoproject.com/en/4.2/topics/i18n/ + +LANGUAGE_CODE = "en-us" + +TIME_ZONE = "UTC" + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/4.2/howto/static-files/ + +STATIC_URL = "static/" diff --git a/ms_business_central_api/tests/__init__.py b/ms_business_central_api/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ms_business_central_api/tests/settings.py b/ms_business_central_api/tests/settings.py new file mode 100644 index 0000000..f95c3b5 --- /dev/null +++ b/ms_business_central_api/tests/settings.py @@ -0,0 +1,210 @@ +""" +Django settings for ms_business_central_api project. +Generated by 'django-admin startproject' using Django 3.1.14. +For more information on this file, see +https://docs.djangoproject.com/en/3.1/topics/settings/ +For the full list of settings and their values, see +https://docs.djangoproject.com/en/3.1/ref/settings/ +""" +import os + + +# Build paths inside the project like this: BASE_DIR / 'subdir'. +BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + +# Quick-start development settings - unsuitable for production +# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = os.environ.get('SECRET_KEY') + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True if os.environ.get('DEBUG') == 'True' else False + +ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS').split(',') + +ENCRYPTION_KEY = os.environ.get('ENCRYPTION_KEY') + + +# Application definition + +INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + + # Installed Apps + 'rest_framework', + 'corsheaders', + 'django_q', + 'fyle_rest_auth', + + # User Created Apps + 'apps.users', + 'apps.workspaces', + 'apps.fyle', + 'apps.ms_business_central', + 'apps.accounting_exports', + 'apps.mappings' +] + +MIDDLEWARE = [ + 'request_logging.middleware.LoggingMiddleware', + 'corsheaders.middleware.CorsMiddleware', + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', + # 'ms_business_central_api.logging_middleware.ErrorHandlerMiddleware', +] + +ROOT_URLCONF = 'ms_business_central_api.urls' +APPEND_SLASH = False + +AUTH_USER_MODEL = 'users.User' + +TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, + }, +] + + +FYLE_REST_AUTH_SERIALIZERS = { + 'USER_DETAILS_SERIALIZER': 'apps.users.serializers.UserSerializer' +} + +FYLE_REST_AUTH_SETTINGS = { + 'async_update_user': True +} + +REST_FRAMEWORK = { + # 'DEFAULT_PERMISSION_CLASSES': ( + # 'rest_framework.permissions.IsAuthenticated', + # 'apps.workspaces.permissions.WorkspacePermissions' + # ), + 'DEFAULT_AUTHENTICATION_CLASSES': ( + 'fyle_rest_auth.authentication.FyleJWTAuthentication', + ), + 'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination', + 'PAGE_SIZE': 100 +} + + +# CACHES = { +# 'default': { +# 'BACKEND': 'django.core.cache.backends.db.DatabaseCache', +# 'LOCATION': 'auth_cache', +# } +# } + +Q_CLUSTER = { + 'name': 'ms_business_central_api', + 'save_limit': 0, + 'retry': 14400, + 'timeout': 3600, + 'catch_up': False, + 'workers': 4, + # How many tasks are kept in memory by a single cluster. + # Helps balance the workload and the memory overhead of each individual cluster + 'queue_limit': 10, + 'cached': False, + 'orm': 'default', + 'ack_failures': True, + 'poll': 1, + 'max_attempts': 1, + 'attempt_count': 1, + # The number of tasks a worker will process before recycling. + # Useful to release memory resources on a regular basis. + 'recycle': 50, + # The maximum resident set size in kilobytes before a worker will recycle and release resources. + # Useful for limiting memory usage. + 'max_rss': 100000 # 100mb +} + + +WSGI_APPLICATION = 'ms_business_central_api.wsgi.application' + +SERVICE_NAME = os.environ.get('SERVICE_NAME') + + +# Database +# https://docs.djangoproject.com/en/3.0/ref/settings/#databases +# Defaulting django engine for qcluster +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'NAME': os.environ.get('DB_NAME'), + 'USER': os.environ.get('DB_USER'), + 'PASSWORD': os.environ.get('DB_PASSWORD'), + 'HOST': os.environ.get('DB_HOST'), + 'PORT': os.environ.get('DB_PORT'), + } +} + +# DATABASE_ROUTERS = ['ms_business_central_api.cache_router.CacheRouter'] + +# Password validation +# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators + +AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, +] + + +# Fyle Settings +API_URL = os.environ.get('API_URL') +FYLE_TOKEN_URI = os.environ.get('FYLE_TOKEN_URI') +FYLE_CLIENT_ID = os.environ.get('FYLE_CLIENT_ID') +FYLE_CLIENT_SECRET = os.environ.get('FYLE_CLIENT_SECRET') +FYLE_BASE_URL = os.environ.get('FYLE_BASE_URL') +FYLE_JOBS_URL = os.environ.get('FYLE_JOBS_URL') +FYLE_APP_URL = os.environ.get('APP_URL') +FYLE_EXPENSE_URL = os.environ.get('FYLE_APP_URL') + + +# Internationalization +# https://docs.djangoproject.com/en/3.1/topics/i18n/ + +LANGUAGE_CODE = 'en-us' + +TIME_ZONE = 'UTC' + +USE_I18N = True + +USE_L10N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/3.1/howto/static-files/ + +STATIC_URL = '/static/' diff --git a/ms_business_central_api/urls.py b/ms_business_central_api/urls.py new file mode 100644 index 0000000..c9e6380 --- /dev/null +++ b/ms_business_central_api/urls.py @@ -0,0 +1,23 @@ +""" +URL configuration for ms_business_central_api project. + +The `urlpatterns` list routes URLs to views. For more information please see: + https://docs.djangoproject.com/en/4.2/topics/http/urls/ +Examples: +Function views + 1. Add an import: from my_app import views + 2. Add a URL to urlpatterns: path('', views.home, name='home') +Class-based views + 1. Add an import: from other_app.views import Home + 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') +Including another URLconf + 1. Import the include() function: from django.urls import include, path + 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) +""" +from django.contrib import admin +from django.urls import path, include + +urlpatterns = [ + path("admin/", admin.site.urls), + path('api/workspaces/', include('apps.workspaces.urls')), +] diff --git a/ms_business_central_api/utils.py b/ms_business_central_api/utils.py new file mode 100644 index 0000000..da7e7fd --- /dev/null +++ b/ms_business_central_api/utils.py @@ -0,0 +1,26 @@ +from rest_framework.serializers import ValidationError +from rest_framework.views import Response + + +def assert_valid(condition: bool, message: str) -> Response or None: + """ + Assert conditions + :param condition: Boolean condition + :param message: Bad request message + :return: Response or None + """ + if not condition: + raise ValidationError(detail={ + 'message': message + }) + + +class LookupFieldMixin: + lookup_field = 'workspace_id' + + def filter_queryset(self, queryset): + if self.lookup_field in self.kwargs: + lookup_value = self.kwargs[self.lookup_field] + filter_kwargs = {self.lookup_field: lookup_value} + queryset = queryset.filter(**filter_kwargs) + return super().filter_queryset(queryset) diff --git a/ms_business_central_api/wsgi.py b/ms_business_central_api/wsgi.py new file mode 100644 index 0000000..669d184 --- /dev/null +++ b/ms_business_central_api/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for ms_business_central_api project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/4.2/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ms_business_central_api.settings") + +application = get_wsgi_application() diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..23f16a0 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,8 @@ +[pytest] +DJANGO_SETTINGS_MODULE = ms_business_central_api.tests.settings +python_files = tests.py test_*.py *_tests.py +addopts = -p no:warnings --strict-markers --no-migrations --create-db +log_cli = 1 +log_cli_level = INFO +log_cli_format = %(asctime)s [%(levelname)8s] %(name)s: %(message)s (%(filename)s:%(lineno)s) +log_cli_date_format=%Y-%m-%d %H:%M:%S \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e71c44d --- /dev/null +++ b/requirements.txt @@ -0,0 +1,51 @@ +# Croniter package for djangoq +croniter==1.3.8 + +# Request Authentication Caching +cryptography==38.0.3 + +# Django and Django REST Framework +Django==4.1.2 +django-cors-headers==4.3.0 +django-rest-framework==0.1.0 +djangorestframework==3.14.0 +django-db-geventpool==4.0.1 +django-request-logging==0.7.5 +django-filter==21.1 + +# DjangoQ for running async tasks +django-q==1.3.9 + +# Read Database Credentials as URL +dj-database-url==0.5.0 + +# API server +gevent==23.9.1 +gunicorn==20.1.0 + +# Platform SDK +fyle==0.35.0 + +# Reusable Fyle Packages +fyle-rest-auth==1.5.0 +fyle-accounting-mappings==1.27.3 +fyle-integrations-platform-connector==1.36.1 + +# Postgres Dependincies +psycopg2-binary==2.9.9 + +# Pylint +pylint==2.7.4 + +# Date Parser +python-dateutil==2.8.1 + +# Pylint Related Libraries +pytest==7.1.2 +pytest-cov==3.0.0 +pytest-django==4.5.2 +pytest-mock==3.8.2 + +# Sendgrid for sending emails_selected +sendgrid==6.9.7 +sentry-sdk==1.19.1 diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..05a10c7 --- /dev/null +++ b/run.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +# Run db migrations +python manage.py migrate + +# Creating the cache table +python manage.py createcachetable --database cache_db + +# Running development server +gunicorn -c gunicorn_config.py ms_business_central_api.wsgi -b 0.0.0.0:8000 diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..d646eb8 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,141 @@ +""" +Fixture configuration for all the tests +""" +from datetime import datetime, timezone + +from unittest import mock +import pytest + +from rest_framework.test import APIClient +from fyle.platform.platform import Platform +from fyle_rest_auth.models import User, AuthToken + +from apps.fyle.helpers import get_access_token +from apps.workspaces.models import ( + Workspace, + FyleCredential +) +from ms_business_central_api.tests import settings + +from .test_fyle.fixtures import fixtures as fyle_fixtures + + +@pytest.fixture() +def api_client(): + """ + Fixture required to test views + """ + return APIClient() + + +@pytest.fixture() +def test_connection(db): + """ + Creates a connection with Fyle + """ + client_id = settings.FYLE_CLIENT_ID + client_secret = settings.FYLE_CLIENT_SECRET + token_url = settings.FYLE_TOKEN_URI + refresh_token = 'Dummy.Refresh.Token' + server_url = settings.FYLE_BASE_URL + + fyle_connection = Platform( + token_url=token_url, + client_id=client_id, + client_secret=client_secret, + refresh_token=refresh_token, + server_url=server_url + ) + + access_token = get_access_token(refresh_token) + fyle_connection.access_token = access_token + user_profile = fyle_connection.v1beta.spender.my_profile.get()['data'] + user = User( + password='', last_login=datetime.now(tz=timezone.utc), id=1, email=user_profile['user']['email'], + user_id=user_profile['user_id'], full_name='', active='t', staff='f', admin='t' + ) + + user.save() + + auth_token = AuthToken( + id=1, + refresh_token=refresh_token, + user=user + ) + auth_token.save() + + return fyle_connection + + +@pytest.fixture(scope="session", autouse=True) +def default_session_fixture(request): + patched_1 = mock.patch( + 'fyle_rest_auth.authentication.get_fyle_admin', + return_value=fyle_fixtures['get_my_profile'] + ) + patched_1.__enter__() + + patched_2 = mock.patch( + 'fyle.platform.internals.auth.Auth.update_access_token', + return_value='asnfalsnkflanskflansfklsan' + ) + patched_2.__enter__() + + patched_3 = mock.patch( + 'apps.fyle.helpers.post_request', + return_value={ + 'access_token': 'easnfkjo12233.asnfaosnfa.absfjoabsfjk', + 'cluster_domain': 'https://staging.fyle.tech' + } + ) + patched_3.__enter__() + + patched_4 = mock.patch( + 'fyle.platform.apis.v1beta.spender.MyProfile.get', + return_value=fyle_fixtures['get_my_profile'] + ) + patched_4.__enter__() + + patched_5 = mock.patch( + 'fyle_rest_auth.helpers.get_fyle_admin', + return_value=fyle_fixtures['get_my_profile'] + ) + patched_5.__enter__() + + +@pytest.fixture +@pytest.mark.django_db(databases=['default']) +def create_temp_workspace(): + """ + Pytest fixture to create a temorary workspace + """ + workspace_ids = [ + 1, 2, 3 + ] + for workspace_id in workspace_ids: + Workspace.objects.create( + id=workspace_id, + name='Fyle For Testing {}'.format(workspace_id), + org_id='riseabovehate{}'.format(workspace_id), + last_synced_at=None, + ccc_last_synced_at=None, + created_at=datetime.now(tz=timezone.utc), + updated_at=datetime.now(tz=timezone.utc) + ) + + +@pytest.fixture() +@pytest.mark.django_db(databases=['default']) +def add_fyle_credentials(): + """ + Pytest fixture to add fyle credentials to a workspace + """ + workspace_ids = [ + 1, 2, 3 + ] + for workspace_id in workspace_ids: + FyleCredential.objects.create( + refresh_token='dummy_refresh_token', + workspace_id=workspace_id, + cluster_domain='https://dummy_cluster_domain.com', + ) diff --git a/tests/test_fyle/__init__.py b/tests/test_fyle/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_fyle/fixtures.py b/tests/test_fyle/fixtures.py new file mode 100644 index 0000000..95dea99 --- /dev/null +++ b/tests/test_fyle/fixtures.py @@ -0,0 +1,27 @@ +fixtures = { + 'get_my_profile': { + 'data': { + 'org': { + 'currency': 'USD', + 'domain': 'fyleforqvd.com', + 'id': 'orNoatdUnm1w', + 'name': 'Fyle For MS Dynamics Demo', + }, + 'org_id': 'orNoatdUnm1w', + 'roles': [ + 'FYLER', + 'VERIFIER', + 'PAYMENT_PROCESSOR', + 'FINANCE', + 'ADMIN', + 'AUDITOR', + ], + 'user': { + 'email': 'ashwin.t@fyle.in', + 'full_name': 'Joanna', + 'id': 'usqywo0f3nBY' + }, + 'user_id': 'usqywo0f3nBY', + } + } +} diff --git a/tests/test_users/__init__.py b/tests/test_users/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_users/test_views.py b/tests/test_users/test_views.py new file mode 100644 index 0000000..75e233c --- /dev/null +++ b/tests/test_users/test_views.py @@ -0,0 +1,6 @@ +import pytest + + +@pytest.mark.django_db(databases=['default']) +def test_setup(): + assert 1 == 1 diff --git a/tests/test_workspaces/__init__.py b/tests/test_workspaces/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_workspaces/test_view.py b/tests/test_workspaces/test_view.py new file mode 100644 index 0000000..82e3f9d --- /dev/null +++ b/tests/test_workspaces/test_view.py @@ -0,0 +1,131 @@ +import json +import pytest # noqa +from django.urls import reverse +from apps.workspaces.models import ( + Workspace, + ExportSetting +) + + +def test_post_of_workspace(api_client, test_connection): + ''' + Test post of workspace + ''' + url = reverse('workspaces') + api_client.credentials(HTTP_AUTHORIZATION='Bearer {}'.format(test_connection.access_token)) + response = api_client.post(url) + + workspace = Workspace.objects.filter(org_id='orNoatdUnm1w').first() + + assert response.status_code == 201 + assert workspace.name == response.data['name'] + assert workspace.org_id == response.data['org_id'] + + response = json.loads(response.content) + + response = api_client.post(url) + assert response.status_code == 201 + + +def test_get_of_workspace(api_client, test_connection): + ''' + Test get of workspace + ''' + url = reverse('workspaces') + api_client.credentials(HTTP_AUTHORIZATION='Bearer {}'.format(test_connection.access_token)) + response = api_client.get(url) + + assert response.status_code == 400 + assert response.data['message'] == 'org_id is missing' + + response = api_client.get('{}?org_id=orNoatdUnm1w'.format(url)) + + assert response.status_code == 400 + assert response.data['message'] == 'Workspace not found or the user does not have access to workspaces' + + response = api_client.post(url) + response = api_client.get('{}?org_id=orNoatdUnm1w'.format(url)) + + assert response.status_code == 200 + assert response.data['name'] == 'Fyle For MS Dynamics Demo' + assert response.data['org_id'] == 'orNoatdUnm1w' + + +def test_export_settings(api_client, test_connection): + ''' + Test export settings + ''' + url = reverse('workspaces') + api_client.credentials(HTTP_AUTHORIZATION='Bearer {}'.format(test_connection.access_token)) + response = api_client.post(url) + + workspace_id = response.data['id'] + + url = reverse('export-settings', kwargs={'workspace_id': workspace_id}) + + api_client.credentials(HTTP_AUTHORIZATION='Bearer {}'.format(test_connection.access_token)) + response = api_client.post(url) + assert response.status_code == 400 + + payload = { + 'reimbursable_expenses_export_type': 'PURCHASE_INVOICE', + 'reimbursable_expense_state': 'PAYMENT_PROCESSING', + 'reimbursable_expense_date': 'LAST_SPENT_AT', + 'reimbursable_expense_grouped_by': 'EXPENSE', + 'credit_card_expense_export_type': 'JOURNAL_ENTRY', + 'credit_card_expense_state': 'PAID', + 'credit_card_expense_grouped_by': 'EXPENSE', + 'credit_card_expense_date': 'CREATED_AT', + 'default_reimbursable_account_name': 'reimbursable account', + 'default_reimbursable_account_id': '123', + 'default_ccc_credit_card_account_name': 'CCC credit card account', + 'default_ccc_credit_card_account_id': '123', + 'default_reimbursable_credit_card_account_name': 'reimbursable credit card account', + 'default_reimbursable_credit_card_account_id': '342', + 'default_vendor_name': 'Nilesh', + 'default_vendor_id': '123', + 'default_back_account_id': '123', + 'default_bank_account_name': 'Bank account' + } + + response = api_client.post(url, payload) + + export_settings = ExportSetting.objects.filter(workspace_id=workspace_id).first() + + assert response.status_code == 201 + assert export_settings.reimbursable_expenses_export_type == 'PURCHASE_INVOICE' + assert export_settings.reimbursable_expense_state == 'PAYMENT_PROCESSING' + assert export_settings.reimbursable_expense_date == 'LAST_SPENT_AT' + assert export_settings.reimbursable_expense_grouped_by == 'EXPENSE' + assert export_settings.credit_card_expense_export_type == 'JOURNAL_ENTRY' + assert export_settings.credit_card_expense_state == 'PAID' + assert export_settings.credit_card_expense_grouped_by == 'EXPENSE' + assert export_settings.credit_card_expense_date == 'CREATED_AT' + assert export_settings.default_reimbursable_account_name == 'reimbursable account' + assert export_settings.default_reimbursable_account_id == '123' + assert export_settings.default_ccc_credit_card_account_name == 'CCC credit card account' + assert export_settings.default_ccc_credit_card_account_id == '123' + assert export_settings.default_reimbursable_credit_card_account_name == 'reimbursable credit card account' + assert export_settings.default_reimbursable_credit_card_account_id == '342' + assert export_settings.default_vendor_name == 'Nilesh' + assert export_settings.default_vendor_id == '123' + + response = api_client.get(url) + + assert response.status_code == 200 + assert export_settings.reimbursable_expenses_export_type == 'PURCHASE_INVOICE' + assert export_settings.reimbursable_expense_state == 'PAYMENT_PROCESSING' + assert export_settings.reimbursable_expense_date == 'LAST_SPENT_AT' + assert export_settings.reimbursable_expense_grouped_by == 'EXPENSE' + assert export_settings.credit_card_expense_export_type == 'JOURNAL_ENTRY' + assert export_settings.credit_card_expense_state == 'PAID' + assert export_settings.credit_card_expense_grouped_by == 'EXPENSE' + assert export_settings.credit_card_expense_date == 'CREATED_AT' + assert export_settings.default_reimbursable_account_name == 'reimbursable account' + assert export_settings.default_reimbursable_account_id == '123' + assert export_settings.default_ccc_credit_card_account_name == 'CCC credit card account' + assert export_settings.default_ccc_credit_card_account_id == '123' + assert export_settings.default_reimbursable_credit_card_account_name == 'reimbursable credit card account' + assert export_settings.default_reimbursable_credit_card_account_id == '342' + assert export_settings.default_vendor_name == 'Nilesh' + assert export_settings.default_vendor_id == '123'