Skip to content

Deploy v3.1.0 #433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 42 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
41aa08b
refactor(model): use positive integer fields
injoonH Jul 31, 2023
98c03a7
feat(article): enable search query for top articles
000wan Aug 9, 2023
464ac22
feat(article): add from_view for top articles
000wan Aug 9, 2023
344c1b1
Merge pull request #407 from sparcs-kaist/feat/top-articles
injoonH Aug 26, 2023
6c3c804
fix(home): add prefetches to fix n+1 query
retroinspect Sep 21, 2023
38afe0c
feat(lint): add conventional pre commit hook
injoonH Oct 5, 2023
10141fa
docs(readme): update README with core contents
injoonH Oct 5, 2023
7fdc743
Merge pull request #422 from sparcs-kaist/feat/readme-codestyle
injoonH Oct 5, 2023
f5b7e92
Merge pull request #401 from sparcs-kaist/refactor/model-fields
injoonH Oct 5, 2023
f38e059
Merge pull request #418 from sparcs-kaist/fix/nplus1-2023-09
retroinspect Oct 5, 2023
3548757
fix(article): add custom paginator for count query optimization
retroinspect Oct 26, 2023
062c39d
fix(article): add indices to fix slow query
retroinspect Nov 2, 2023
6f70bec
feat(calendar): add model, viewset, serializer
Qndndn Nov 2, 2023
23a59fa
feat(smtp): Change mail sender to gmail smtp-relay
DoyunShin Nov 3, 2023
6fde1f3
feat(smtp): Abstract MIMEMultipart maker to other function.
DoyunShin Nov 3, 2023
91e5702
feat(smtp): fix type strictly, add usage of smtp_send
DoyunShin Nov 4, 2023
b1fc94b
feat(smtp): Set for sending each email to user.
DoyunShin Nov 7, 2023
6872568
Merge pull request #431 from sparcs-kaist/feat/email
injoonH Nov 7, 2023
7026ce0
fix: models
Qndndn Nov 15, 2023
edf1d43
fix: models
Qndndn Nov 15, 2023
e65399f
fix: models
Qndndn Nov 15, 2023
002d840
fix: serializers and viewsets
Qndndn Nov 15, 2023
0981486
fix: urls
Qndndn Nov 15, 2023
d8b09cf
fix: urls
Qndndn Nov 16, 2023
2bc7074
docs(article): add comment for query optimization
injoonH Nov 16, 2023
c6f976b
Merge pull request #414 from sparcs-kaist/fix/slow-query
injoonH Nov 16, 2023
73899c9
fix: models
Qndndn Nov 16, 2023
879cc8e
fix: serializers
Qndndn Nov 16, 2023
03c5838
fix: serializers
Qndndn Nov 16, 2023
910e9a2
fix: urls
Qndndn Nov 16, 2023
7c16b08
feat: implement new calendar testcode
Qndndn Nov 23, 2023
e03d686
feat: calendar api test code
Qndndn Nov 23, 2023
4fb20bf
refactor(calendar): split models into separate files
injoonH Jan 2, 2024
b51ad62
feat(calendar): rename calendar model to event
injoonH Jan 3, 2024
9f34981
refactor(calendar): split serializers into separate files
injoonH Jan 3, 2024
4eda968
refactor(calendar): split viewsets into separate files
injoonH Jan 3, 2024
fd79b14
feat(calendar): migrate calendar models
injoonH Jan 3, 2024
d9ef2ee
feat(calendar): add admin page
injoonH Jan 3, 2024
f79ebf3
feat(calendar): add endpoints
injoonH Jan 3, 2024
371b6e9
fix(calendar): remove type hint for tags
injoonH Jan 3, 2024
1a41c8a
feat(calendar): add tests about retrievals
injoonH Jan 3, 2024
9a01656
Merge pull request #430 from sparcs-kaist/feat/calendar
injoonH Jan 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
repos:
- repo: https://github.com/psf/black
rev: 22.10.0
rev: 23.9.1
hooks:
- id: black
args: [--safe]

- repo: https://github.com/PyCQA/isort
rev: 5.12.0
hooks:
- id: isort
args: [--profile=black]

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
rev: v4.4.0
hooks:
- id: check-added-large-files
- id: check-ast
- id: check-yaml
- id: end-of-file-fixer
- id: mixed-line-ending
args: [--fix=lf]
- id: name-tests-test
args: [--pytest-test-first]
- id: trailing-whitespace

- repo: https://github.com/PyCQA/isort
rev: 5.10.1
- repo: https://github.com/compilerla/conventional-pre-commit
rev: v2.4.0
hooks:
- id: isort
args: [--profile=black]
- id: conventional-pre-commit
stages: [commit-msg]
232 changes: 34 additions & 198 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,218 +1,54 @@
# SPARCS NewAra API
# Ara API

<p align="center">
<a href="https://newara.sparcs.org">
<img src="res/logo.svg" alt="Logo" height="150">
</a>
</p>
<p align="center">
<em>Restful API for NewAra, KAIST's official community service</em>
</p>
<p align="center">
<img
src="https://img.shields.io/github/v/release/sparcs-kaist/new-ara-api?display_name=tag&color=blue"
alt="Release"
>
<img
src="https://img.shields.io/github/license/sparcs-kaist/new-ara-api?color=black"
alt="License"
>
<img
src="https://github.com/sparcs-kaist/new-ara-api/actions/workflows/github-actions.yml/badge.svg"
alt="Test"
>
</p>
Restful API for Ara, KAIST's official community service

## Management Information

- Issue tracking: Notion
- Credentials: S3 sparcs-credentials
- Communication: Slack
- general: #new-ara
- backend: #new-ara-backend
- fontend: #new-ara-frontend
- github commits: #new-ara-noti
- sentry alerts: #new-ara-sentry
- notion changes: #new-ara-notion
- with OB: #ara
- Contact: <new-ara@sparcs.org>
![GitHub Pipenv locked Python version (master)][badge/python-version]
[![Code style: black][badge/black]][repo/black]
[![Imports: isort][badge/isort]][isort]
[![pre-commit][badge/pre-commit]][repo/pre-commit]
[![Conventional Commits][badge/conventional-commits]][conventional-commits]

## Project Setup

### Create & Activate Virtual Environment
Use pipenv to install packages. (e.g., `pipenv install <package>`)

```bash
python3 -m pip install pipenv # It must be Python 3.11
pipenv shell
```
pipenv --python 3.11 # Use python 3.11
pipenv shell # Activate virtual environment
pipenv install --dev # Install packages (`--dev` flag for development)

### Install Requirements

```bash
pipenv install --dev # `--dev` flag for development
# Run `pre-commit` automatically on `git commit`
pre-commit install
pre-commit install --hook-type commit-msg
```

For macOS, you may need to install `openssl` & `mysqlclient` and set
`LDFLAGS=-L/usr/local/opt/openssl/lib` before installing requirements. Only
tested for macOS Mojave. See [link](https://stackoverflow.com/questions/50940302/installing-mysql-python-causes-command-clang-failed-with-exit-status-1-on-mac).

### Fill Environment Configuration

Refer to [.env.example file](https://github.com/sparcs-kaist/new-ara-api/blob/master/.env.example)
and write your own `.env` file with required informations filled-in. For SPARCS
SSO, create a test service or ask SYSOP to deploy production server.

For the test service in [SPARCS SSO](https://sparcssso.kaist.ac.kr/) for local
settings, fill in as below.

- **Main URL**: (can be anything)
- **Login Callback URL**: <http://127.0.0.1:9000/api/users/sso_login_callback/>
- **Unregister URL**: <http://127.0.0.1:9000/api/users/sso_unregister/>

After this, you should make the information in the `.env` file as environment
variables for local run. (In development or porduction server, we do not use
`.env` file. Rather, we use environment variables in `docker-compose.yml` file.)

To make the make the information in the `.env` file as environment variables for
local run, you can export each manually, or you can use the below command. Check
the `Makefile`'s [env command](https://github.com/sparcs-kaist/new-ara-api/blob/master/Makefile#L32)
and revise it. The command is written assuming you are using `~/.bashrc`. You
might want to revise it to `~/.bash_profile` or `~/.zshrc` according to your
settings.

```bash
make env
```

### Useful Commands

Most useful commands are already written in the [Makefile](https://github.com/sparcs-kaist/new-ara-api/blob/master/Makefile).
Refer to the `Makefile` and try to understand and use them.

### Create and Migrate Database

```bash
make init
```

### Collect static files

```bash
python manage.py collectstatic
```

`collectstatic` command collects all static files required to run installed apps
to selected storage - for this project, static S3 bucket. You should open public
access for the static bucket to get appropriate response.

### Internationalization (i18n)

[xgettext](https://man7.org/linux/man-pages/man1/xgettext.1.html) is required in
order to generate translation files. Detailed background on this can be found on
[Django's documentations](https://docs.djangoproject.com/en/3.1/topics/i18n/translation/).
To generate translation files, run:

```bash
make i18n_generate
```

Translation files are generated under `ara/locale/(Locale name)/LC_MESSAGES`.
After writing translations, run the following command to apply the translations.

```bash
make i18n_compile
```

### Create log directory

```bash
sudo mkdir -p /var/log/newara
sudo chown $(whoami) /var/log/newara
```

### Run lightweight server for development
## How to Run

```bash
pipenv shell
make run
```

`0` is abbreviation for `0.0.0.0` which refers to 'listening to every incoming hosts'.
**Do not deploy with runserver command - use WSGI. This command is only for development.**

---

## Deployment with Docker

### docker-compose.yml

`docker-compose.yml` file is managed in S3 `sparcs-newara` bucket `docker-compose`
directory. For simple local deployment, refer to [docker-compose.example.yml file](https://github.com/sparcs-kaist/new-ara-api/blob/master/docker-compose.example.yml)
and fill in the information needed.

For [development server](https://newara.dev.sparcs.org/), two docker containsers
are up with the docker-compose file. Using AWS RDS for mysql, elastiCache for redis.

- api container (used gunicorn for serving. celery-beat also here.)
- celery-worker container

### docker image

For managing docker images, we are using AWS ECR, `newara` repository.

- Using tag `newara:dev` for development server.

### other docker related files

- **Dockerfile**: We use virtual environment also inside the docker container.
- **.docker/run.sh**: api container's entrypoint.
- **.docker/run-celery.sh**: celery worker container's entrypoint.
- **.docker/supervisor-app.conf**
- **.docker/supervisor-celery-worker.conf**

## Project Stack

### Interpreter

- `Python 3.11`
- `Pipenv` is used as package manager
- When adding libraries to the virtual environment, you should not use `pip`.
Rather, use `pipenv` command. Refer to [this link](https://pipenv.pypa.io/en/latest/commands/)
for pipenv commands.

### Framework

- `Django 4.2`
- `djangorestframework 3.14`

### Database

- MySQL 8.0
- `mysqlclient 2.2`

Works with MySQL for Linux & macOS, not tested in Windows. Timezone is
automatically adjusted. It is strongly recommended to set default charset of
database or MySQL server to `utf8mb4`.

### Storage

- AWS S3
- `django-s3-storage 0.14`

Two buckets are used - one for storing static files, one for media files that
users upload. Go to django-s3-storage documentation for required permissions.

### Authentication

- SPARCS SSO v2 API
- `djangorestframework SessionAuthentication`

### API Documentation
## URLs

- `drf-spectacular 0.26`
- Base URL: `/api`
- Admin page: `/api/admin/`
- API documentations:
- `/api/schema/swagger/`
- `/api/schema/redoc/`

---
## How to Contribute

## Contributors
1. Follow [Conventional Commits][conventional-commits] for writing commit messages.
2. Use type hints strictly. (Check [PEP 484][pep-484].)

See [contributors](https://github.com/sparcs-kaist/new-ara-api/graphs/contributors).
[badge/python-version]: https://img.shields.io/github/pipenv/locked/python-version/sparcs-kaist/new-ara-api/master
[badge/black]: https://img.shields.io/badge/code%20style-black-000000
[badge/isort]: https://img.shields.io/badge/%20imports-isort-%231674b1?labelColor=ef8336
[badge/pre-commit]: https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit
[badge/conventional-commits]: https://img.shields.io/badge/Conventional%20Commits-1.0.0-%23FE5196?logo=conventionalcommits&logoColor=white
[isort]: https://pycqa.github.io/isort
[conventional-commits]: https://conventionalcommits.org
[pep-484]: https://peps.python.org/pep-0484/
[repo/black]: https://github.com/psf/black
[repo/pre-commit]: https://github.com/pre-commit/pre-commit
Empty file added apps/calendar/__init__.py
Empty file.
37 changes: 37 additions & 0 deletions apps/calendar/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from django.contrib import admin

from ara.classes.admin import MetaDataModelAdmin

from .models import Event, Tag


@admin.register(Event)
class EventAdmin(MetaDataModelAdmin):
list_display = (
"ko_title",
"en_title",
"is_all_day",
"start_at",
"end_at",
"location",
"url",
)
fields = (
("ko_title", "ko_description"),
("en_title", "en_description"),
("start_at", "end_at"),
"is_all_day",
"location",
"url",
"tags",
)
filter_horizontal = ("tags",)


@admin.register(Tag)
class TagAdmin(admin.ModelAdmin):
list_display = (
"colored_ko_name",
"en_name",
"color",
)
5 changes: 5 additions & 0 deletions apps/calendar/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class CalendarConfig(AppConfig):
name = "apps.calendar"
Loading