Skip to content

Commit

Permalink
Import/start plain-api
Browse files Browse the repository at this point in the history
  • Loading branch information
davegaeddert committed Jan 22, 2025
1 parent 14691e7 commit d7dccad
Show file tree
Hide file tree
Showing 15 changed files with 1,031 additions and 0 deletions.
28 changes: 28 additions & 0 deletions plain-api/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
BSD 3-Clause License

Copyright (c) 2025, Dropseed, LLC

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3 changes: 3 additions & 0 deletions plain-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- This file is compiled from plain-api/plain/api/README.md. Do not edit this file directly. -->

# plain.api
1 change: 1 addition & 0 deletions plain-api/plain/api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# plain.api
Empty file added plain-api/plain/api/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions plain-api/plain/api/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from plain.packages import PackageConfig


class Config(PackageConfig):
name = "plain.api"
label = "plainapi" # Primarily for migrations
25 changes: 25 additions & 0 deletions plain-api/plain/api/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# from bolt.exceptions import ValidationError

# class APIFormMixin:
# def clean(self):
# cleaned_data = super().clean()

# # Make sure all the field names are present in the input data
# for name, field in self.fields.items():
# if name not in self.data:
# raise ValidationError(f"Missing field {name}")

# return cleaned_data


class APIPartialFormMixin:
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

# If any field is not present in the JSON input,
# then act as if it's "disabled" so Bolt
# will keep the initial value instead of setting it to the default.
# This is required because stuff like checkbox doesn't submit in HTML form data when false.
for name, field in self.fields.items():
if name not in self.data:
field.disabled = True
38 changes: 38 additions & 0 deletions plain-api/plain/api/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Plain 0.17.0 on 2025-01-22 20:02

import uuid

import plain.api.models
from plain import models
from plain.models import migrations


class Migration(migrations.Migration):
initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="APIKey",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True)),
(
"uuid",
models.UUIDField(default=uuid.uuid4, editable=False, unique=True),
),
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
("last_used_at", models.DateTimeField(blank=True, null=True)),
("name", models.CharField(blank=True, max_length=255)),
(
"token",
models.CharField(
default=plain.api.models.generate_token,
max_length=40,
unique=True,
),
),
],
),
]
18 changes: 18 additions & 0 deletions plain-api/plain/api/migrations/0002_apikey_expires_at.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Plain 0.17.0 on 2025-01-22 21:02

from plain import models
from plain.models import migrations


class Migration(migrations.Migration):
dependencies = [
("plainapi", "0001_initial"),
]

operations = [
migrations.AddField(
model_name="apikey",
name="expires_at",
field=models.DateTimeField(blank=True, null=True),
),
]
Empty file.
33 changes: 33 additions & 0 deletions plain-api/plain/api/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import binascii
import os
import uuid

from plain import models


def generate_token():
return binascii.hexlify(os.urandom(20)).decode()


class APIKey(models.Model):
uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
expires_at = models.DateTimeField(blank=True, null=True)
last_used_at = models.DateTimeField(blank=True, null=True)

name = models.CharField(max_length=255, blank=True)

token = models.CharField(max_length=40, default=generate_token, unique=True)

# Connect to a user, for example, from your own model:
# api_key = models.OneToOneField(
# APIKey,
# on_delete=models.CASCADE,
# related_name="user",
# null=True,
# blank=True,
# )

def __str__(self):
return self.name or str(self.uuid)
Loading

0 comments on commit d7dccad

Please sign in to comment.