fix: Transmit confidant_email on edit

This commit is contained in:
aldrin 2021-05-18 11:06:02 +02:00
parent 6835240173
commit a8464d7536
7 changed files with 89 additions and 14 deletions

View file

@ -2,7 +2,7 @@ import Cookies from "js-cookie";
type HTTPMethod = "GET" | "POST" | "PUT" | "PATCH"; type HTTPMethod = "GET" | "POST" | "PUT" | "PATCH";
class APIError extends Error { export class APIError extends Error {
constructor(message: string, public readonly errors: unknown) { constructor(message: string, public readonly errors: unknown) {
super(message); super(message);
} }
@ -42,8 +42,8 @@ async function request(
export class User { export class User {
email: string | undefined; email: string | undefined;
password: string | undefined; password: string | undefined;
private username: string | null = null; username: string | null = null;
private confidantEmail: string | null = null; confidantEmail: string | null = null;
isAuthenticated = false; isAuthenticated = false;
private token = ""; private token = "";
@ -67,6 +67,7 @@ export class User {
200, 200,
{ {
username: this.username, username: this.username,
confidant_email: this.confidantEmail,
}, },
this.token this.token
); );

View file

@ -24,11 +24,15 @@
</tr> </tr>
<tr> <tr>
<td>Benutzername</td> <td>Benutzername</td>
<td><InlineEditor v-model="user.username" @input="user.save()" /></td> <td>
<InlineEditor v-model="user.username" @input="user.save()" />
</td>
</tr> </tr>
<tr> <tr>
<td>Vertrauensperson</td> <td>Vertrauensperson</td>
<td><inline-editor v-model="user.confidant_email" /></td> <td>
<inline-editor v-model="user.confidantEmail" @input="user.save()" />
</td>
</tr> </tr>
<tr> <tr>
<td>E-Mail-Adresse</td> <td>E-Mail-Adresse</td>

19
src/mixins.ts Normal file
View file

@ -0,0 +1,19 @@
import Component from "vue-class-component";
import Vue from "vue";
@Component
export class NotifyMixin extends Vue {
showError(): void {
this.$buefy.toast.open({
message: "Es ist leider ein Fehler aufgetreten.",
type: "is-danger",
});
}
showSuccess(message: string): void {
this.$buefy.toast.open({
message,
type: "is-success",
});
}
}

View file

@ -15,24 +15,34 @@
</template> </template>
<script lang="ts"> <script lang="ts">
import { Component, Vue } from "vue-property-decorator"; import { Component } from "vue-property-decorator";
import LoginForm from "@/components/LoginForm.vue"; import LoginForm from "@/components/LoginForm.vue";
import { mixins } from "vue-class-component";
import { User } from "@/api"; import { User } from "@/api";
import UserTable from "@/components/UserTable.vue"; import UserTable from "@/components/UserTable.vue";
import { NotifyMixin } from "@/mixins";
@Component({ components: { UserTable, LoginForm } }) @Component({ components: { UserTable, LoginForm } })
export default class Home extends Vue { export default class Home extends mixins(NotifyMixin) {
private user = new User(); private user = new User();
public async created(): Promise<void> { public async created(): Promise<void> {
if (this.$route.name === "confirm") { if (this.$route.name === "confirm") {
await this.doConfirm();
} else if (!this.user.isAuthenticated) {
this.$router.push({ name: "login" });
}
}
private async doConfirm() {
try {
await User.confirm(this.$route.params.uid, this.$route.params.token); await User.confirm(this.$route.params.uid, this.$route.params.token);
this.$router.push({ name: "login" }); this.$router.push({ name: "login" });
this.$buefy.toast.open({ this.showSuccess(
message: "Deine E-Mail-Adresse wurde bestätigt. Du kannst dich nun anmelden."
"Deine E-Mail-Adresse wurde bestätigt. Du kannst dich nun anmelden.", );
type: "is-success", } catch {
}); this.showError();
} }
} }
} }

View file

@ -0,0 +1,20 @@
# Generated by Django 2.2.20 on 2021-05-18 08:09
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('userausfall', '0003_auto_20210414_0827'),
]
operations = [
migrations.AddField(
model_name='user',
name='confidant',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL),
),
]

View file

@ -68,6 +68,7 @@ class User(AbstractBaseUser, PermissionsMixin):
), ),
) )
date_joined = models.DateTimeField(_('date joined'), default=timezone.now) date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
confidant = models.ForeignKey("User", on_delete=models.SET_NULL, null=True)
objects = UserManager() objects = UserManager()
@ -83,6 +84,9 @@ class User(AbstractBaseUser, PermissionsMixin):
super().clean() super().clean()
self.email = self.__class__.objects.normalize_email(self.email) self.email = self.__class__.objects.normalize_email(self.email)
def get_confidant_email(self):
return ""
def get_full_name(self): def get_full_name(self):
""" """
Return the first_name plus the last_name, with a space in between. Return the first_name plus the last_name, with a space in between.

View file

@ -7,9 +7,26 @@ from userausfall.models import AccountRequest
class AccountRequestSerializer(serializers.HyperlinkedModelSerializer): class AccountRequestSerializer(serializers.HyperlinkedModelSerializer):
class Meta: class Meta:
model = AccountRequest model = AccountRequest
fields = ('url', 'email', 'confidant_email', 'username', 'is_verified', 'is_trustable') fields = (
"url",
"email",
"confidant_email",
"username",
"is_verified",
"is_trustable",
)
class UserSerializer(BaseUserSerializer): class UserSerializer(BaseUserSerializer):
confidant_email = serializers.EmailField(source="get_confidant_email")
class Meta(BaseUserSerializer.Meta): class Meta(BaseUserSerializer.Meta):
fields = ('email', 'username') fields = ("email", "username", "confidant_email")
def get_confidant_email(self):
return ""
def update(self, instance, validated_data):
print(validated_data)
confidant = validated_data.pop("get_confidant_email")
return super().update(instance, validated_data)