Skip to content
This repository has been archived by the owner on Jul 18, 2022. It is now read-only.

Commit

Permalink
Merge branch 'feat/tags'
Browse files Browse the repository at this point in the history
# Conflicts:
#	config.toml.example
#	config.yml.example
  • Loading branch information
PerchunPak committed Feb 19, 2022
2 parents 8595933 + 36be62e commit f47eb02
Show file tree
Hide file tree
Showing 11 changed files with 173 additions and 11 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ lint:

.PHONY: unit
unit:
poetry run python manage.py migrate
poetry run pytest

.PHONY: package
Expand Down
7 changes: 7 additions & 0 deletions autodonate/lib/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.contrib import admin
from autodonate.lib.models import Item, Payment, PaymentProcess


admin.site.register(Item)
admin.site.register(Payment)
admin.site.register(PaymentProcess)
23 changes: 23 additions & 0 deletions autodonate/lib/context_processors/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""The file is responsible for registering new global variables in templates."""

__all__ = ["register_variable", "global_variables"]

VALUES: dict[str, str | bool] = {}


def register_variable(name: str, value: str | bool) -> None:
"""Add a new global variable.
Args:
name: variable name (upper case)
value: variable value
"""
VALUES[name] = value


def global_variables(request) -> dict[str, str | bool]:
"""Return a dictionary of all global variables.
Called on every render() by Django itself.
"""
return VALUES
65 changes: 65 additions & 0 deletions autodonate/lib/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""Basic objects of donations, items."""

from django.db.models import (
Model,
SmallIntegerField,
FloatField,
TextField,
BooleanField,
ForeignKey,
CASCADE,
CharField,
TimeField,
)
from autodonate.lib.utils.rcon import Rcon


class Item(Model):
"""Model for the given item"""

currency = SmallIntegerField(
choices=[(0, "RUB"), (1, "UAH"), (2, "USD"), (3, "EUR")], null=True
)
price = FloatField(null=True)
rcon_command = TextField(null=True)
require_nick = BooleanField(default=False)


class PaymentProcess(Model):
"""Model for pending item payment"""

# TODO: Add more fields
item = ForeignKey(Item, on_delete=CASCADE)
nickname = CharField(max_length=32, null=True)
timestamp = TimeField(auto_now_add=True)

def cleanup_nickname(self) -> None:
self.nickname = (
self.nickname.replace(" ", "")
.replace("'", "")
.replace('"', "")
.replace(",", "")
.replace(".", "")
.replace("`", "")
)
self.save()


class Payment(Model):
"""Model for finished item payment"""

process = ForeignKey(PaymentProcess, on_delete=CASCADE)
timestamp = TimeField(auto_now_add=True)

def format_rcon(self) -> str:
self.process.cleanup_nickname()
if not self.process.item.rcon_command:
raise ValueError("Item.rcon_command required.")
return str(
self.process.item.rcon_command.format(
nickname=self.process.nickname
)
)

def issue(self) -> str:
return Rcon.run(self.format_rcon())
Empty file.
5 changes: 5 additions & 0 deletions autodonate/lib/templatetags/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""Here will be our template tags in future."""

from django import template

register = template.Library()
20 changes: 13 additions & 7 deletions autodonate/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"autodonate.lib", # need for templatetags
],
)

Expand All @@ -62,15 +63,19 @@
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"DIRS": [BASE_DIR / "templates"],
"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",
],
"context_processors": CONFIG.get(
"CONTEXT_PROCESSORS",
[
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
"autodonate.lib.context_processors.global_variables",
],
),
},
},
]
Expand Down Expand Up @@ -123,6 +128,7 @@
# https://docs.djangoproject.com/en/4.0/howto/static-files/

STATIC_URL = "static/"
STATIC_ROOT = CONFIG.get("STATIC_ROOT", "static")

# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
Expand Down
25 changes: 21 additions & 4 deletions autodonate/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,25 @@
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = []

default = settings.CONFIG.get(
"URLPATTERNS", {"admin/": {"path": "eval:admin.site.urls", "name": "admin"}}
)

for entry in default:
if default[entry]["path"].startswith("eval:"):
app_path = eval(default[entry]["path"][5:])
else:
app_path = include(default[entry]["path"])
urlpatterns.append(
path(entry.replace("<index>", ""), app_path, name=default[entry].get("name", None))
)

if settings.CONFIG.get("DEBUG", True):
urlpatterns + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

urlpatterns = [
path("admin/", admin.site.urls),
]
6 changes: 6 additions & 0 deletions config.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ RCON_PORT: 25575 # optional
DATABASE:
ENGINE: "django.db.backends.sqlite3"
NAME: "db.sqlite3"

# urlpatterns
URLPATTERNS:
"admin/":
path: "admin.site.urls"
name: "admin"
11 changes: 11 additions & 0 deletions tests/context_processors/test_processors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from autodonate.lib.context_processors import (
register_variable,
global_variables,
)


def test_add_global():
register_variable("TEST", True)
assert global_variables(None)["TEST"] is True
register_variable("TEST", "123")
assert global_variables(None)["TEST"] == "123"
21 changes: 21 additions & 0 deletions tests/models/test_payments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from autodonate.lib.models import *
from os import system


def test_donation():
i = Item(
currency=0,
price=99.9,
rcon_command="test {nickname}",
require_nick=True,
)
i.save()

pp = PaymentProcess(item=i, nickname="cof ob,")
pp.save()
pp.cleanup_nickname()
assert pp.nickname == "cofob"

p = Payment(process=pp)
p.save()
assert p.format_rcon() == "test cofob"

0 comments on commit f47eb02

Please sign in to comment.