Skip to content

Commit 22d9d5c

Browse files
committed
Add some more things for the api
1 parent 0e838ca commit 22d9d5c

File tree

7 files changed

+111
-21
lines changed

7 files changed

+111
-21
lines changed

client/out/app.js

+38-7
Original file line numberDiff line numberDiff line change
@@ -33829,6 +33829,7 @@ ${JSON.stringify(newTargetLocation, null, 2)}
3382933829
server,
3383033830
user: userData,
3383133831
messages: void 0,
33832+
message: void 0,
3383233833
pkdata: void 0,
3383333834
loading: false
3383433835
};
@@ -33843,6 +33844,9 @@ ${JSON.stringify(newTargetLocation, null, 2)}
3384333844
setMessages(state, data) {
3384433845
state.messages = data;
3384533846
},
33847+
setMessage(state, data) {
33848+
state.message = data;
33849+
},
3384633850
setServer(state, data) {
3384733851
state.server = data;
3384833852
}
@@ -33904,6 +33908,7 @@ ${JSON.stringify(newTargetLocation, null, 2)}
3390433908
context.commit("setLoading", false);
3390533909
context.commit("setUserData", void 0);
3390633910
context.commit("setMessages", void 0);
33911+
context.commit("setMessage", void 0);
3390733912
resolve();
3390833913
});
3390933914
});
@@ -33914,12 +33919,12 @@ ${JSON.stringify(newTargetLocation, null, 2)}
3391433919
}
3391533920
});
3391633921
},
33917-
fetchMessages(context) {
33922+
fetchParticipantMessages(context) {
3391833923
console.log("MESSAGEs ", context.state);
3391933924
context.commit("setLoading", true);
3392033925
console.log("Fetch messages");
3392133926
return new Promise(async (resolve, reject) => {
33922-
await fetch(context.state.server + "/messages/api/messages/", {
33927+
await fetch(context.state.server + "/messages/api/participants/", {
3392333928
method: "GET",
3392433929
headers: {
3392533930
"Content-Type": "application/json",
@@ -33936,6 +33941,29 @@ ${JSON.stringify(newTargetLocation, null, 2)}
3393633941
});
3393733942
});
3393833943
});
33944+
},
33945+
fetchMessage(context, { id }) {
33946+
console.log("MESSAGE: ", context.state);
33947+
context.commit("setLoading", true);
33948+
console.log("Fetch message ", id);
33949+
return new Promise(async (resolve, reject) => {
33950+
await fetch(context.state.server + "/messages/api/messages/" + id + "/", {
33951+
method: "GET",
33952+
headers: {
33953+
"Content-Type": "application/json",
33954+
"Authorization": `Token ${context.state.user.token}`
33955+
}
33956+
}).then((res) => {
33957+
console.log(res);
33958+
if (res.status !== 200) {
33959+
throw new Error("Error");
33960+
}
33961+
res.json().then((data) => {
33962+
context.commit("setLoading", false);
33963+
context.commit("setMessage", data);
33964+
});
33965+
});
33966+
});
3393933967
}
3394033968
}
3394133969
});
@@ -34083,7 +34111,7 @@ ${JSON.stringify(newTargetLocation, null, 2)}
3408334111
]),
3408434112
created() {
3408534113
if (this.messages == void 0) {
34086-
this.$store.dispatch("fetchMessages");
34114+
this.$store.dispatch("fetchParticipantMessages");
3408734115
}
3408834116
},
3408934117
template: `
@@ -34117,16 +34145,19 @@ ${JSON.stringify(newTargetLocation, null, 2)}
3411734145
// src/components/Message.js
3411834146
var Message_default = {
3411934147
computed: mapState([
34120-
"messages",
34148+
"message",
3412134149
"loading"
3412234150
]),
3412334151
created() {
34124-
if (this.messages == void 0) {
34125-
this.$store.dispatch("fetchMessages");
34126-
}
34152+
this.$store.dispatch("fetchMessage", {
34153+
id: this.$route.params.id
34154+
});
3412734155
},
3412834156
template: `
3412934157
<div>Message {{ $route.params.id }}</div>
34158+
<div>{{ message }}</div>
34159+
34160+
3413034161
`
3413134162
};
3413234163

client/src/Store.js

+30-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const Store = createStore({
1414
server,
1515
user: userData,
1616
messages: undefined,
17+
message: undefined,
1718
pkdata: undefined,
1819
loading: false
1920
}
@@ -28,6 +29,9 @@ const Store = createStore({
2829
setMessages(state, data) {
2930
state.messages = data
3031
},
32+
setMessage(state, data) {
33+
state.message = data
34+
},
3135
setServer(state, data) {
3236
state.server = data
3337
}
@@ -94,6 +98,7 @@ const Store = createStore({
9498
context.commit('setLoading', false)
9599
context.commit('setUserData', undefined)
96100
context.commit('setMessages', undefined)
101+
context.commit('setMessage', undefined)
97102
resolve()
98103
})
99104
})
@@ -105,12 +110,12 @@ const Store = createStore({
105110

106111
})
107112
},
108-
fetchMessages (context) {
113+
fetchParticipantMessages (context) {
109114
console.log("MESSAGEs ", context.state)
110115
context.commit('setLoading', true)
111116
console.log("Fetch messages")
112117
return new Promise(async (resolve, reject) => {
113-
await fetch(context.state.server + '/messages/api/messages/', {
118+
await fetch(context.state.server + '/messages/api/participants/', {
114119
method: 'GET',
115120
headers: {
116121
'Content-Type': 'application/json',
@@ -126,6 +131,29 @@ const Store = createStore({
126131
})
127132
})
128133

134+
})
135+
},
136+
fetchMessage (context, {id}) {
137+
console.log("MESSAGE: ", context.state)
138+
context.commit('setLoading', true)
139+
console.log("Fetch message ", id)
140+
return new Promise(async (resolve, reject) => {
141+
await fetch(context.state.server + '/messages/api/messages/' + id + '/', {
142+
method: 'GET',
143+
headers: {
144+
'Content-Type': 'application/json',
145+
'Authorization': `Token ${context.state.user.token}`
146+
}}).then(res => {
147+
console.log(res)
148+
if(res.status !== 200) {
149+
throw new Error('Error')
150+
}
151+
res.json().then(data => {
152+
context.commit('setLoading', false)
153+
context.commit('setMessage', data)
154+
})
155+
})
156+
129157
})
130158
}
131159
}

client/src/components/Message.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@ import { mapState } from 'vuex'
22

33
export default {
44
computed: mapState([
5-
'messages', 'loading'
5+
'message', 'loading'
66
]),
77
created() {
88

9-
if(this.messages== undefined) {
10-
this.$store.dispatch('fetchMessages')
11-
}
12-
9+
this.$store.dispatch('fetchMessage', {
10+
id: this.$route.params.id
11+
})
12+
1313
},
1414
template: `
1515
<div>Message {{ $route.params.id }}</div>
16+
<div>{{ message }}</div>
17+
18+
1619
`
1720
}
1821

client/src/components/Messages.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default {
77
created() {
88

99
if(this.messages== undefined) {
10-
this.$store.dispatch('fetchMessages')
10+
this.$store.dispatch('fetchParticipantMessages')
1111
}
1212

1313
},

etsd/msgs/serializers.py

+23-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Meta:
1111
fields = ["id", "name", "code", "kind", "email", "is_active"]
1212

1313

14-
class MessageSerializer(serializers.ModelSerializer):
14+
class ParticipantMessageSerializer(serializers.ModelSerializer):
1515
participants = AuthoritySerializer(read_only=True, many=True)
1616

1717
class Meta:
@@ -20,8 +20,29 @@ class Meta:
2020

2121

2222
class ParticipantSerializer(serializers.ModelSerializer):
23-
message = MessageSerializer(read_only=True)
23+
message = ParticipantMessageSerializer(read_only=True)
2424

2525
class Meta:
2626
model = models.Participant
2727
fields = ["id", "message_id", "status", "message"]
28+
29+
30+
class DataSerializer(serializers.ModelSerializer):
31+
class Meta:
32+
model = models.Data
33+
fields = [
34+
"id", "number", "extension"
35+
]
36+
37+
38+
class MessageSerializer(serializers.ModelSerializer):
39+
participants = AuthoritySerializer(read_only=True, many=True)
40+
data = DataSerializer(read_only=True, source="data_set", many=True, )
41+
42+
class Meta:
43+
model = models.Message
44+
fields = [
45+
"id",
46+
"participants",
47+
"data",
48+
]

etsd/msgs/urls.py

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ def any_permission_required(*args):
9191
from django.urls import path, include
9292

9393
router = DefaultRouter()
94+
router.register(r"participants", api.ParticipantViewSet)
9495
router.register(r"messages", api.MessageViewSet)
9596

9697
urlpatterns += [

etsd/msgs/views/api.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,25 @@
33
from rest_framework.views import APIView
44
from rest_framework.response import Response
55
from rest_framework import viewsets, authentication, permissions
6-
from . import ParticipantQuerysetMixin
6+
from . import ParticipantQuerysetMixin, MessageAccessMixin
77

88

99
class IsAuthenticatedAllowOptions(permissions.IsAuthenticated):
10-
1110
def has_permission(self, request, view):
12-
if request.method == 'OPTIONS':
11+
if request.method == "OPTIONS":
1312
return True
1413
return super().has_permission(request, view)
1514

1615

17-
class MessageViewSet(ParticipantQuerysetMixin, viewsets.ModelViewSet):
16+
class ParticipantViewSet(ParticipantQuerysetMixin, viewsets.ModelViewSet):
1817
authentication_classes = [authentication.TokenAuthentication]
1918
permission_classes = [IsAuthenticatedAllowOptions]
2019
serializer_class = serializers.ParticipantSerializer
2120
queryset = models.Participant.objects.all()
21+
22+
23+
class MessageViewSet(MessageAccessMixin, viewsets.ModelViewSet):
24+
authentication_classes = [authentication.TokenAuthentication]
25+
permission_classes = [IsAuthenticatedAllowOptions]
26+
serializer_class = serializers.MessageSerializer
27+
queryset = models.Message.objects.all()

0 commit comments

Comments
 (0)