Skip to content

Commit

Permalink
add create and update StudentProfile components
Browse files Browse the repository at this point in the history
  • Loading branch information
ajatprabha committed Oct 19, 2018
1 parent a9942f8 commit f32172c
Show file tree
Hide file tree
Showing 7 changed files with 505 additions and 7 deletions.
23 changes: 23 additions & 0 deletions src/account/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@ def test_invalid_user_id(self):
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.content.decode('utf-8'), '{"user":["Invalid pk \\"13\\" - object does not exist."]}')

def test_current_student_profile_route(self):
StudentProfile.objects.create(user=self.user, phone='9999999999', github='https://github.com',
gender=StudentProfile.GENDER_CHOICES[0][0],
year=StudentProfile.YEAR_CHOICES[0][0],
branch=StudentProfile.BRANCH_CHOICES[0][0])
self.client.login(username=self.user.username, password='password')
response = self.client.get(reverse('api:account:student-profile-current'))
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.content.decode('utf-8'), '{"id":1,"phone":"9999999999","github":"https://github.com",'
'"gender":"M","branch":"CSE","year":"1","user":1}')


class MentorProfileViewSetTest(TestCase):
@classmethod
Expand Down Expand Up @@ -109,6 +120,18 @@ def test_invalid_user_id(self):
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.content.decode('utf-8'), '{"user":["Invalid pk \\"13\\" - object does not exist."]}')

def test_current_mentor_profile_route(self):
MentorProfile.objects.create(user=self.user, phone='9999999999', github='https://github.com',
gender=StudentProfile.GENDER_CHOICES[0][0],
about_me='random',
past_experience='random')
self.client.login(username=self.user.username, password='password')
response = self.client.get(reverse('api:account:mentor-profile-current'))
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.content.decode('utf-8'), '{"id":1,"is_approved":false,"phone":"9999999999",'
'"github":"https://github.com","gender":"M","about_me"'
':"random","past_experience":"random","user":1}')


class UserViewSetTest(TestCase):
@classmethod
Expand Down
8 changes: 8 additions & 0 deletions src/account/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ class StudentProfileViewSet(mixins.CreateModelMixin, mixins.RetrieveModelMixin,
serializer_class = StudentProfileSerializer
queryset = StudentProfile.objects.all()

def get_object(self):
return get_object_or_404(StudentProfile,
user=self.request.user) if self.action == 'current' else super().get_object()

@action(methods=['get'], detail=False)
def current(self, request, *args, **kwargs):
return self.retrieve(request, args, kwargs)


class MentorProfileViewSet(mixins.CreateModelMixin, mixins.RetrieveModelMixin, mixins.ListModelMixin,
mixins.UpdateModelMixin, GenericViewSet):
Expand Down
2 changes: 1 addition & 1 deletion ui/src/components/CreateMentorProfile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
message: 'Profile created successfully',
color: 'success'
}, {root: true})
this.$emit('profile_created', this.profile)
this.$emit('profile_created', {id: this.profile.id, type: 'mentor-profile'})
}).catch(() => this.$store.dispatch('messages/showMessage', {
message: 'Failed to create mentor profile',
color: 'error',
Expand Down
160 changes: 160 additions & 0 deletions ui/src/components/CreateStudentProfile.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<template>
<v-layout column>
<v-flex>
<v-card flat>
<v-container fluid>
<v-card-title primary-title>
<p class="display-1 primary--text">Create Student Profile</p>
</v-card-title>
<v-form>
<v-layout row wrap>
<v-flex sm6 xs12>
<v-text-field prepend-icon="person" v-model="localUser.first_name"
:rules="[rules.required]" name="first_name"
label="First Name"/>
</v-flex>
<v-flex sm6 xs12>
<v-text-field prepend-icon="person" v-model="localUser.last_name" name="last_name"
:rules="[rules.required]" label="Last Name"/>
</v-flex>
<v-flex sm6 xs12>
<v-select
prepend-icon="fa-venus-mars"
v-model="profile.gender"
:items="genderItems"
item-text="label"
item-value="value"
:rules="[rules.required]" label="Gender"
/>
</v-flex>
<v-flex sm6 xs12>
<v-select
prepend-icon="fa-code-fork"
v-model="profile.branch"
:items="branchItems"
item-text="label"
item-value="value"
:rules="[rules.required]" label="Branch"
/>
</v-flex>
<v-flex sm6 xs12>
<v-select
prepend-icon="fa-graduation-cap"
v-model="profile.year"
:items="yearItems"
item-text="label"
item-value="value"
:rules="[rules.required]" label="Year"
/>
</v-flex>
<v-flex sm6 xs12>
<v-text-field prepend-icon="fa-phone" v-model="profile.phone" name="phone"
:rules="[rules.required, rules.phone]" label="Contact Number"/>
</v-flex>
<v-flex xs12>
<v-text-field prepend-icon="fa-github" v-model="profile.github" name="github_link"
:rules="[rules.required, rules.url]" label="Github Link"/>
</v-flex>
</v-layout>
</v-form>
<v-card-actions>
<v-flex xs4></v-flex>
<v-flex xs4>
<v-btn primary round large block color="primary" @click="createStudentProfile">Create</v-btn>
</v-flex>
<v-flex xs4></v-flex>
</v-card-actions>
</v-container>
</v-card>
</v-flex>
</v-layout>
</template>

<script>
export default {
name: 'CreateStudentProfile',
props: ['user'],
data () {
return {
localUser: {
first_name: '',
last_name: ''
},
profile: {
id: null,
gender: null,
phone: null,
github: null,
about_me: null,
past_experience: null
},
rules: {
required: value => !!value || 'Required.',
phone: value => /\(?\+[0-9]{1,3}\)? ?-?[0-9]{1,3} ?-?[0-9]{3,5} ?-?[0-9]{4}( ?-?[0-9]{3})? ?(\w{1,10}\s?\d{1,6})?|([6-9][0-9]{9})/.test(value) || 'Invalid phone number.',
url: value => /(http(s)?:\/\/.)(www\.)?[-a-zA-Z0-9@:%._~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_.~#?&//=]*)/g.test(value) || 'Invalid URL (include https://)'
}
}
},
computed: {
genderItems () {
return [{label: 'Male', value: 'M'}, {label: 'Female', value: 'F'}]
},
branchItems () {
return [
{label: 'Computer Science and Engineering', value: 'CSE'},
{label: 'Electrical Engineering', value: 'EE'},
{label: 'Mechanical Engineering', value: 'ME'},
{label: 'BioScience and BioTechnology', value: 'BB'}
]
},
yearItems () {
return [
{label: '1st year', value: '1'},
{label: '2nd year', value: '2'},
{label: '3rd year', value: '3'},
{label: '4th year', value: '4'}
]
}
},
methods: {
createStudentProfile () {
this.$httpClient.patch(`/api/account/user/${this.user.id}/`, {
first_name: this.localUser.first_name,
last_name: this.localUser.last_name
}).then(response => {
this.localUser = response.data
this.$httpClient.post('/api/account/student-profile/', {
user: this.localUser.id,
phone: this.profile.phone,
github: this.profile.github,
gender: this.profile.gender,
branch: this.profile.branch,
year: this.profile.year
}).then(response => {
this.profile = response.data
this.$store.dispatch('messages/showMessage', {
message: 'Profile created successfully',
color: 'success'
}, {root: true})
this.$emit('profile_created', {id: this.profile.id, type: 'student-profile'})
}).catch(() => this.$store.dispatch('messages/showMessage', {
message: 'Failed to create student profile',
color: 'error',
timeout: 6000
}, {root: true}))
}).catch(() => this.$store.dispatch('messages/showMessage', {
message: 'Failed to update user',
color: 'error',
timeout: 6000
}, {root: true}))
}
},
mounted () {
this.localUser = this.user
}
}
</script>

<style scoped>
</style>
60 changes: 54 additions & 6 deletions ui/src/components/Dashboard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
flat
>
</v-toolbar>
<v-layout v-if="this.profile.id && !showCreateMentorProfileDialogue" row justify-center pb-2>
<v-layout v-if="this.profile.type === 'mentor-profile' && !showCreateMentorProfileDialogue" row justify-center pb-2>
<v-flex xs11 sm10 md8>
<v-card class="card--flex-toolbar">
<v-toolbar card prominent>
Expand Down Expand Up @@ -52,6 +52,26 @@
</v-card>
</v-flex>
</v-layout>
<v-layout v-if="this.profile.type === 'student-profile' && !showCreateStudentProfileDialogue" row justify-center
pb-2>
<v-flex xs11 sm10 md8>
<v-card class="card--flex-toolbar">
<v-toolbar card prominent>
<v-toolbar-title class="body-2 grey--text">Hi, {{user.first_name}}</v-toolbar-title>
<v-spacer></v-spacer>
</v-toolbar>
<v-divider class="mb-4"></v-divider>
<StudentProfile/>
<v-alert
:value="true"
type="info"
>
All set for now! Now contact <router-link class="white--text" :to="{name: 'MentorList'}">mentors</router-link> regarding a
project, ask them to float it and once it is approved, you'll be able to submit a proposal for it.
</v-alert>
</v-card>
</v-flex>
</v-layout>
<v-layout v-if="showCreateMentorProfileDialogue" row justify-center pb-2>
<v-flex xs11 sm10 md8>
<v-card class="card--flex-toolbar">
Expand All @@ -66,19 +86,42 @@
</v-card>
</v-flex>
</v-layout>
<v-layout v-if="showCreateStudentProfileDialogue" row justify-center pb-2>
<v-flex xs11 sm10 md8>
<v-card class="card--flex-toolbar">
<CreateStudentProfile :user="user" @profile_created="refreshDashboard"/>
<v-alert
:value="showCreateStudentProfileDialogue"
type="info"
black-text
>
Do not register as a student if you want to be a mentor!
</v-alert>
</v-card>
</v-flex>
</v-layout>
</v-card>
</template>

<script>
import CreateMentorProfile from './CreateMentorProfile'
import CreateStudentProfile from './CreateStudentProfile'
import ProjectCreateUpdate from './ProjectCreateUpdate'
import Project from './Project'
import MentorProfile from './MentorProfile'
import StudentProfile from './StudentProfile'
import {mapGetters, mapActions} from 'vuex'
export default {
name: 'Dashboard',
components: {CreateMentorProfile, MentorProfile, ProjectCreateUpdate, Project},
components: {
CreateMentorProfile,
CreateStudentProfile,
MentorProfile,
StudentProfile,
ProjectCreateUpdate,
Project
},
data () {
return {
dialog: false,
Expand All @@ -91,7 +134,8 @@
type: null,
id: null
},
showCreateMentorProfileDialogue: false
showCreateMentorProfileDialogue: false,
showCreateStudentProfileDialogue: false
}
},
props: ['index'],
Expand All @@ -101,6 +145,9 @@
]),
...mapGetters('mentorProfile', [
'mentorProfile'
]),
...mapGetters('studentProfile', [
'studentProfile'
])
},
methods: {
Expand All @@ -114,7 +161,7 @@
this.$httpClient.get('/api/account/user/profile/')
.then(response => {
if (response.status === 204) {
this.showCreateMentorProfileDialogue = true
this.showCreateStudentProfileDialogue = true
} else {
this.profile = response.data
}
Expand All @@ -123,15 +170,16 @@
},
refreshDashboard (profile) {
this.showCreateMentorProfileDialogue = false
this.profile.id = profile.id
this.showCreateStudentProfileDialogue = false
this.profile = profile
}
},
mounted () {
this.fetchUserType()
},
watch: {
profile (value) {
if (value.id) this.fetchProjectList()
if (value.id && value.type === 'mentor-profile') this.fetchProjectList()
}
}
}
Expand Down
Loading

0 comments on commit f32172c

Please sign in to comment.