Skip to content

Commit

Permalink
Merge branch 'main' into Feature/487-Use-django-caching-to-improve-TTFB
Browse files Browse the repository at this point in the history
  • Loading branch information
LittleBigProgramming authored Oct 5, 2023
2 parents 110acbe + 8f73357 commit b01c83e
Show file tree
Hide file tree
Showing 27 changed files with 793 additions and 486 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/json-validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
name: Check JSON Files
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: JSON Syntax Check
uses: limitusus/json-syntax-check@v1
with:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/run-unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout the branch
uses: actions/checkout@v2
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: 3.9
cache: 'pip'
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/validate-language-info-files.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
steps:

- name: Checkout the branch
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: 3.9
cache: 'pip'
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/validate-meta-info-file.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
steps:

- name: Checkout the branch
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Set up Python
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: 3.9
cache: 'pip'
Expand Down
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ COPY requirements.txt /code/
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD python manage.py migrate && python manage.py runserver 0.0.0.0:8000
CMD python manage.py migrate && \
python manage.py collectstatic --clear --no-input && \
python manage.py runserver 0.0.0.0:8000
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@ Website that will compare language features side by side.
## Why would you want this?
Good question. If there's an aspect of a language you don't know, you can compare a languages you know with a language you do. It's a good way to quickly learn a new language, or use it as a quick reference to remember things by.

Learn more about some of the considerations behind this on the [Project Architecture](https://docs.codethesaur.us/project_architecture/) docs page.
Learn more about some of the considerations behind this on the [Project Architecture](https://docs.codethesaur.us/website/project-architecture/) docs page.

## Documentation

Learn everything you'd need to know about how the project is built, how to install and run it, how to contribute, and more over at the [Documentation](https://docs.codethesaur.us/) site!

## Cloning and running it locally

Check out our [Installation/Running Locally](https://docs.codethesaur.us/install/) page on our documentation.
Check out our [Installation/Running Locally](https://docs.codethesaur.us/install/quick_start/) page on our documentation.

## Contributing

Check out the [Contributing Guide](https://docs.codethesaur.us/contributing/) to learn more about how you can help add more language data, fix bugs, or add features!

## Is this project available for Hacktoberfest contributions?

Yes! The Code Thesaurus code and documentation projects are both enabled for Hacktoberfest contributions.

## Code of Conduct

All contributors are required to follow the [Code Thesaurus Code of Conduct](https://docs.codethesaur.us/code_of_conduct/).
Expand Down
10 changes: 5 additions & 5 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
asgiref==3.4.1
asgiref==3.7.2
astroid==2.6.6
bleach==4.1.0
colorama==0.4.4
dj-database-url==0.5.0
Django==4.0.8
Django==4.2.3
django-markdownify==0.9.0
django-on-heroku==1.1.2
gunicorn==20.1.0
Expand All @@ -15,14 +15,14 @@ mccabe==0.6.1
packaging==21.3
protobuf==3.18.3
psycopg2-binary==2.9.5
Pygments==2.9.0
Pygments==2.15.0
pylint==2.9.6
pyparsing==3.0.6
pytz==2021.1
six==1.16.0
sqlparse==0.4.2
sqlparse==0.4.4
toml==0.10.2
typing-extensions==3.10.0.0
typing-extensions==4.7.1
tzdata==2021.5
webencodings==0.5.1
whitenoise==5.3.0
Expand Down
2 changes: 1 addition & 1 deletion runtime.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
python-3.10.8
python-3.11.4
4 changes: 2 additions & 2 deletions web/management/commands/generate_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ class Command(BaseCommand):
def add_arguments(self, parser):
structures = list(MetaInfo().structures.keys())

parser.add_argument('language', help="Id of the programming language")
parser.add_argument('language', help="Key of the programming language")
parser.add_argument(
'structures',
nargs='+',
help="Id(s) of the structure(s)",
help="Key(s) of the structure(s)",
choices=structures
)
parser.add_argument('--language-version', required=True)
Expand Down
94 changes: 69 additions & 25 deletions web/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,18 @@ class MetaStructure:
concepts
"""

def __init__(self, structure_key, friendly_name):
def __init__(self, key, name):
"""
Inits the MetaStructure object by loading in the concepts and
categories from a language's structure file
:param structure_key: key for the structure
:param friendly_name: the human-friendly name for the specified
structure
:param key: key for the structure
:param name: the human-friendly name for the specified structure
"""
self.key = structure_key
self.friendly_name = friendly_name
self.key = key
self.name = name
meta_structure_file_path = os.path.join(
"web", "thesauruses", "_meta", structure_key) + ".json"
"web", "thesauruses", "_meta", f"{key}.json")
with open(meta_structure_file_path, 'r', encoding='UTF-8') as meta_structure_file:
meta_structure_file_json = json.load(meta_structure_file)

Expand All @@ -38,19 +37,22 @@ class Language:
structure key
"""

def __init__(self, key, friendly_name):
def __init__(self, key, name):
"""
Initialize the Language object, which will contain concepts for a given
structure
:param key: ID of the language in the meta_info.json file
:param key: key of the language in the meta_info.json file
:param name: the human-friendly name for the specified language
"""

# Add an empty string to convert SafeString to str
self.key = str(key + "")
self.friendly_name = friendly_name
self.name = name
self.concepts = None
self.version = None
self.language_dir = os.path.join("web", "thesauruses", self.key)
self.version = None


def versions(self):
Expand Down Expand Up @@ -82,14 +84,14 @@ def load_concepts(self, structure_key, version):
"""
Loads the structure file into the Language object
:param structure_key: the ID for the structure to load
:param structure_key: the key for the structure to load
:param version: the version of the language
"""
structure_file_name = f"{structure_key}.json"
file_path = os.path.join(self.language_dir, version, structure_file_name)
file_path = os.path.join(self.language_dir, version, f"{structure_key}.json")
with open(file_path, 'r', encoding='UTF-8') as file:
file_json = json.load(file)
self.concepts = file_json["concepts"]
self.version = version

def load_filled_concepts(self, structure_key, version):
from web.thesaurus_template_generators import generate_language_template
Expand Down Expand Up @@ -169,7 +171,7 @@ def concept_unknown(self, concept_key):
"""
Returns a Boolean if the concept is not known
:param concept_key: ID for the concept
:param concept_key: key for the concept
:return: Boolean if the concept is not known
"""
return self.concepts.get(concept_key) is None
Expand All @@ -178,7 +180,7 @@ def concept_implemented(self, concept_key):
"""
Returns a Boolean if the concept is implemented
:param concept_key: ID for the concept
:param concept_key: key for the concept
:return: Boolean if the language defines this concept
"""
return not self.concept(concept_key).get("not-implemented", False)
Expand All @@ -187,7 +189,7 @@ def concept_code(self, concept_key):
"""
Returns the code portion of the provided concept
:param concept_key: ID for the concept
:param concept_key: key for the concept
:return: the string containing the concept's code
"""
code = self.concept(concept_key).get("code")
Expand All @@ -199,12 +201,32 @@ def concept_comment(self, concept_key):
"""
Returns the comment portion of the provided concept
:param concept_key: ID for the concept
:param concept_key: key for the concept
:return: the string containing the concept's comment
"""
return self.concept(concept_key).get("comment", "")


class MissingLanguageError(Exception):
"""Error for when a requested language is not defined in `meta.json`"""
def __init__(self, key):
super().__init__()
self.key = key


class MissingStructureError(Exception):
"""
Error that signals that a specific language & version does not have the structure
defined
"""
def __init__(self, structure, language_key, language_name, language_version):
super().__init__()
self.structure = structure
self.language_key = language_key
self.language_name = language_name
self.language_version = language_version


class MetaInfo:
"""Holds info about structures and languages"""

Expand All @@ -221,11 +243,11 @@ def __init__(self):
self.structures = meta_info_json["structures"]
self.languages = meta_info_json["languages"]

def language_friendly_name(self, language_key):
def language_name(self, language_key):
"""
Given a structure key (from meta_info.json), returns the language's human-friendly name
:param language_key: ID of the language located in the meta_info.json file
:param language_key: key of the language located in the meta_info.json file
:return: string with the human-friendly name
"""
return self.languages[language_key]
Expand All @@ -235,22 +257,44 @@ def language(self, language_key):
Given a language key (from meta_info.json), returns the whole
Language for it
:param language_key: ID of the language located in the meta_info.json
:param language_key: key of the language located in the meta_info.json
file
:return: Language for the requested key
:rtype: Language
"""
return Language(
language_key,
self.language_friendly_name(language_key),
self.language_name(language_key),
)

def structure_friendly_name(self, structure_key):

def load_languages(self, language_keys_versions, meta_structure):
"""Tries to load all languages from `language_keys` and the requested `structure`"""
languages = []
for language_key, version in language_keys_versions:
try:
language = self.language(language_key)
version = version or sorted(language.versions())[-1]
language.load_concepts(meta_structure.key, version)
languages.append(language)
except FileNotFoundError as file_not_found:
raise MissingStructureError(
meta_structure,
language_key,
self.language_friendly_name(language_key),
version,
) from file_not_found
except KeyError as key_error:
raise MissingLanguageError(language_key) from key_error
return languages


def structure_name(self, structure_key):
"""
Given a structure key (from meta_info.json), returns the structure's
human-friendly name
:param structure_key: ID of the structure located in the meta_info.json
:param structure_key: key of the structure located in the meta_info.json
file
:return: string with the human-friendly name
:rtype: String
Expand All @@ -262,14 +306,14 @@ def structure(self, structure_key):
Given a structure key (from meta_info.json), returns the whole
MetaStructure for it
:param structure_key: ID of the structure located in the meta_info.json
:param structure_key: key of the structure located in the meta_info.json
file
:return: MetaStructure for the requested key
:rtype: MetaStructure
"""
return MetaStructure(
structure_key,
self.structure_friendly_name(structure_key)
self.structure_name(structure_key)
)

class SiteVisit(models.Model):
Expand Down
Loading

0 comments on commit b01c83e

Please sign in to comment.