Skip to content

Commit

Permalink
Merge pull request #1 from waggle-sensor/custom-user
Browse files Browse the repository at this point in the history
Using custom user model, adding auth, adding users endpoints.
  • Loading branch information
seanshahkarami authored Oct 3, 2022
2 parents d0179dd + 5ace9dd commit 84f8979
Show file tree
Hide file tree
Showing 13 changed files with 318 additions and 213 deletions.
29 changes: 16 additions & 13 deletions app/admin.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from django.contrib import admin
from .models import Profile, Node, Project, ProfileMembership, NodeMembership
from django.contrib.auth import admin as auth_admin
from .models import User, Node, Project, UserMembership, NodeMembership


class ProfileMembershipInline(admin.TabularInline):
ordering = ("profile__user__username",)
model = ProfileMembership
class UserMembershipInline(admin.TabularInline):
ordering = ("user__username",)
model = UserMembership
extra = 0


Expand All @@ -14,14 +15,16 @@ class NodeMembershipInline(admin.TabularInline):
extra = 0


@admin.register(Profile)
class ProfileAdmin(admin.ModelAdmin):
list_display = ("username", "name")
@admin.register(User)
class UserAdmin(auth_admin.UserAdmin):
fieldsets = (
(None, {"fields": ("username", "password")}),
("Personal info", {"fields": ("name", "email", "organization", "bio", "ssh_public_keys")}),
("Permissions", {"fields": ("is_active", "is_staff", "is_superuser", "groups", "user_permissions")}),
("Important dates", {"fields": ("last_login", "date_joined")}),
)
list_display = ("username", "name", "is_superuser")
search_fields = ("username", "name")
inlines = (ProfileMembershipInline,)

def username(self, profile):
return profile.user.username


@admin.register(Node)
Expand All @@ -34,6 +37,6 @@ class NodeAdmin(admin.ModelAdmin):

@admin.register(Project)
class ProjectAdmin(admin.ModelAdmin):
list_display = ("name", "number_of_members", "number_of_nodes")
list_display = ("name", "number_of_users", "number_of_nodes")
search_fields = ("name",)
inlines = (ProfileMembershipInline, NodeMembershipInline)
inlines = (UserMembershipInline, NodeMembershipInline)
5 changes: 5 additions & 0 deletions app/authentication.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from rest_framework import authentication


class TokenAuthentication(authentication.TokenAuthentication):
keyword = "Sage"
218 changes: 156 additions & 62 deletions app/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
# Generated by Django 4.1.1 on 2022-09-14 15:24
# Generated by Django 4.1 on 2022-10-03 16:16

from django.conf import settings
import django.contrib.auth.models
import django.contrib.auth.validators
from django.db import migrations, models
import django.db.models.deletion
import django.utils.timezone


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("auth", "0012_alter_user_first_name_max_length"),
]

operations = [
migrations.CreateModel(
name="Node",
name="User",
fields=[
(
"id",
Expand All @@ -26,28 +29,105 @@ class Migration(migrations.Migration):
verbose_name="ID",
),
),
("password", models.CharField(max_length=128, verbose_name="password")),
(
"vsn",
models.CharField(max_length=10, unique=True, verbose_name="VSN"),
"last_login",
models.DateTimeField(
blank=True, null=True, verbose_name="last login"
),
),
],
),
migrations.CreateModel(
name="Profile",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
"is_superuser",
models.BooleanField(
default=False,
help_text="Designates that this user has all permissions without explicitly assigning them.",
verbose_name="superuser status",
),
),
(
"username",
models.CharField(
error_messages={
"unique": "A user with that username already exists."
},
help_text="Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.",
max_length=150,
unique=True,
validators=[
django.contrib.auth.validators.UnicodeUsernameValidator()
],
verbose_name="username",
),
),
(
"email",
models.EmailField(
blank=True, max_length=254, verbose_name="email address"
),
),
(
"is_staff",
models.BooleanField(
default=False,
help_text="Designates whether the user can log into this admin site.",
verbose_name="staff status",
),
),
(
"is_active",
models.BooleanField(
default=True,
help_text="Designates whether this user should be treated as active. Unselect this instead of deleting accounts.",
verbose_name="active",
),
),
(
"date_joined",
models.DateTimeField(
default=django.utils.timezone.now, verbose_name="date joined"
),
),
("name", models.CharField(blank=True, max_length=255)),
("organization", models.CharField(blank=True, max_length=255)),
("bio", models.TextField(blank=True)),
(
"ssh_public_keys",
models.TextField(blank=True, verbose_name="SSH public keys"),
),
(
"groups",
models.ManyToManyField(
blank=True,
help_text="The groups this user belongs to. A user will get all permissions granted to each of their groups.",
related_name="user_set",
related_query_name="user",
to="auth.group",
verbose_name="groups",
),
),
(
"user_permissions",
models.ManyToManyField(
blank=True,
help_text="Specific permissions for this user.",
related_name="user_set",
related_query_name="user",
to="auth.permission",
verbose_name="user permissions",
),
),
],
options={
"verbose_name": "user",
"verbose_name_plural": "users",
"abstract": False,
},
managers=[
("objects", django.contrib.auth.models.UserManager()),
],
),
migrations.CreateModel(
name="Project",
name="Node",
fields=[
(
"id",
Expand All @@ -58,11 +138,24 @@ class Migration(migrations.Migration):
verbose_name="ID",
),
),
("name", models.CharField(max_length=20, unique=True)),
(
"vsn",
models.CharField(max_length=10, unique=True, verbose_name="VSN"),
),
(
"mac",
models.CharField(
blank=True,
max_length=16,
null=True,
unique=True,
verbose_name="MAC",
),
),
],
),
migrations.CreateModel(
name="ProfileMembership",
name="NodeMembership",
fields=[
(
"id",
Expand All @@ -77,56 +170,55 @@ class Migration(migrations.Migration):
"can_schedule",
models.BooleanField(
default=False,
help_text="Designates whether user can schedule.",
help_text="Designates whether node allows scheduling.",
verbose_name="Schedule?",
),
),
(
"can_develop",
models.BooleanField(
default=False,
help_text="Designates whether user has developer access.",
help_text="Designates whether node allows developer access.",
verbose_name="Develop?",
),
),
(
"can_access_files",
models.BooleanField(
default=False,
help_text="Designates whether user has file access.",
help_text="Designates whether node allows file access.",
verbose_name="Files?",
),
),
(
"profile",
"node",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="app.profile"
on_delete=django.db.models.deletion.CASCADE, to="app.node"
),
),
],
),
migrations.CreateModel(
name="Project",
fields=[
(
"project",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="app.project"
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("name", models.CharField(max_length=255, unique=True)),
(
"nodes",
models.ManyToManyField(through="app.NodeMembership", to="app.node"),
),
],
),
migrations.AddField(
model_name="profile",
name="projects",
field=models.ManyToManyField(
through="app.ProfileMembership", to="app.project"
),
),
migrations.AddField(
model_name="profile",
name="user",
field=models.OneToOneField(
on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL
),
),
migrations.CreateModel(
name="NodeMembership",
name="UserMembership",
fields=[
(
"id",
Expand All @@ -141,30 +233,32 @@ class Migration(migrations.Migration):
"can_schedule",
models.BooleanField(
default=False,
help_text="Designates whether node allows scheduling.",
help_text="Designates whether user can schedule.",
verbose_name="Schedule?",
),
),
(
"can_develop",
models.BooleanField(
default=False,
help_text="Designates whether node allows developer access.",
help_text="Designates whether user has developer access.",
verbose_name="Develop?",
),
),
(
"can_access_files",
models.BooleanField(
default=False,
help_text="Designates whether node allows file access.",
help_text="Designates whether user has file access.",
verbose_name="Files?",
),
),
(
"node",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="app.node"
"allow_view",
models.BooleanField(
default=False,
help_text="Designates whether user has view access to project.",
verbose_name="View?",
),
),
(
Expand All @@ -173,27 +267,27 @@ class Migration(migrations.Migration):
on_delete=django.db.models.deletion.CASCADE, to="app.project"
),
),
(
"user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
],
),
migrations.AddField(
model_name="node",
name="projects",
model_name="project",
name="users",
field=models.ManyToManyField(
through="app.NodeMembership", to="app.project"
through="app.UserMembership", to=settings.AUTH_USER_MODEL
),
),
migrations.AddConstraint(
model_name="profilemembership",
constraint=models.UniqueConstraint(
models.F("profile"),
models.F("project"),
name="app_profilemembership_uniq",
),
),
migrations.AddConstraint(
migrations.AddField(
model_name="nodemembership",
constraint=models.UniqueConstraint(
models.F("node"), models.F("project"), name="app_nodemembership_uniq"
name="project",
field=models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="app.project"
),
),
]
Loading

0 comments on commit 84f8979

Please sign in to comment.