Skip to content

Commit

Permalink
fix course_set in LongUserSerializer because we delete courses from s…
Browse files Browse the repository at this point in the history
…tudent in models
  • Loading branch information
Re-Krass committed Jun 20, 2018
1 parent d0d7f64 commit 0127e65
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 7 deletions.
5 changes: 4 additions & 1 deletion ydl_api/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
class Language(models.Model):
name = models.CharField(max_length=100)

def __str__(self):
return self.name

class User(AbstractUser):
def upload_to(self, filename):
return "images/profiles/{}/{}".format(self.id, filename)
Expand Down Expand Up @@ -127,7 +130,7 @@ class CalendarEntry(models.Model):
('HOMEWORK', 'Homework'),
('EXAM', 'Exam'),
)
date = models.DateField()
date = models.DateTimeField()
matter = models.CharField(choices=MATTER_CHOICES, max_length=12)
course = models.ForeignKey(Course, on_delete=models.CASCADE)

Expand Down
54 changes: 49 additions & 5 deletions ydl_api/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,14 @@ def create(self, validated_data):
'link':'https://api.ydlearning.com/activate/{}/{}'.format(uid, token),
'expires_in':str(settings.JWT_AUTH['JWT_EXPIRATION_DELTA']),
'expires_time': ' hours', # change plural!
'logo_img_link':"https://lh3.googleusercontent.com/PL8M-2OhoDITza8WOCdveAax9yQuXzaDakaJHcivO1ZjJg5D1u0eb9gzgx8VSLlfVT4vitIV2GIPkc8OfGJrR6rpko1U8JuV4CAZ2p-gvc4NhVUthlbaEz9HcKwY98UFiwN79pzu=s742-no",
#'logo_img_link':"https://lh3.googleusercontent.com/PL8M-2OhoDITza8WOCdveAax9yQuXzaDakaJHcivO1ZjJg5D1u0eb9gzgx8VSLlfVT4vitIV2GIPkc8OfGJrR6rpko1U8JuV4CAZ2p-gvc4NhVUthlbaEz9HcKwY98UFiwN79pzu=s742-no",
'email_sendto':user.email,
'ydl_context': "Context Text",
'ydl_email':"admin@ydlearning.com",
'ydl_url':"https://www.ydlearning.com",
'ydl_url_github': "https://github.com/YoungAndDigitalLearning",
'ydl_url_impr': "https://www.ydlearning.com/impressum.html",
'ydl_url_prpol': "https://www.ydlearning.com/privacypolicy",
'ydl_url_impr': "https://www.ydlearning.com/sites/impressum.html",
#'ydl_url_prpol': "https://www.ydlearning.com/privacypolicy",
}
html = html_template.render(context)

Expand Down Expand Up @@ -102,12 +103,53 @@ class Meta:
class LongUserSerializer(serializers.ModelSerializer):
courses = serializers.SerializerMethodField()

def update(self, instance, validated_data):
if instance.email != validated_data.get('email', instance.email):
instance.isEmailActivated = False

uid = urlsafe_base64_encode(force_bytes(instance.pk)).decode()

payload = jwt_payload_handler(instance)
token = jwt_encode_handler(payload)

html_template = get_template('api/verification_email.html')

context = {
'user': validated_data.get('username', instance.username),
'link':'https://api.ydlearning.com/activate/{}/{}'.format(uid, token),
'expires_in':str(settings.JWT_AUTH['JWT_EXPIRATION_DELTA']),
'expires_time': ' hours', # change plural!
#'logo_img_link':"https://lh3.googleusercontent.com/PL8M-2OhoDITza8WOCdveAax9yQuXzaDakaJHcivO1ZjJg5D1u0eb9gzgx8VSLlfVT4vitIV2GIPkc8OfGJrR6rpko1U8JuV4CAZ2p-gvc4NhVUthlbaEz9HcKwY98UFiwN79pzu=s742-no",
'email_sendto': validated_data.get('email', instance.email),
'ydl_context': "Context Text",
'ydl_email':"admin@ydlearning.com",
'ydl_url':"https://www.ydlearning.com",
'ydl_url_github': "https://github.com/YoungAndDigitalLearning",
'ydl_url_impr': "https://www.ydlearning.com/sites/impressum.html",
}
html = html_template.render(context)
send_mail(
# Subject
'[Y&D Learning] Please verify your email address.',
'',
# Content
#
# Email send from
#'admin@ydlearning.com',
'no-reply@ydlearning.com',
# Email send to
[validated_data.get('email', instance.email)],
# fail silently
fail_silently=False,
html_message = html,
)

def get_courses(self, obj):

if obj.is_teacher:
return TeacherSerializer(obj.teacher).data["course_set"]
elif obj.is_student:
return StudentSerializer(obj.student ).data["course_set"]
return StudentSerializer(obj.student ).data["courses"]

class Meta:
model = User
Expand All @@ -117,7 +159,7 @@ class StudentSerializer(serializers.ModelSerializer):

class Meta:
model = Student
fields = ["user", "course_set"]
fields = ["user", "courses"]

class TeacherSerializer(serializers.ModelSerializer):

Expand All @@ -138,6 +180,8 @@ def get_content(self, obj):

def get_size(self, obj):
return obj.content.size

html_template = get_template('api/verification_email.html')

class Meta:
model = Resource
Expand Down
2 changes: 1 addition & 1 deletion ydl_api/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ def activate(request, uidb64, token):
pass

if user is not None and validated:
user.isEmailActivated = True
user.save()
login(request, user)
# return redirect('home')
return HttpResponse('Thank you for your email confirmation. Now you can login your account.')
else:
Expand Down
180 changes: 180 additions & 0 deletions ydl_api/database_dump/dump_basic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
[
{
"model": "api.user",
"pk": 3,
"fields": {
"password": "pbkdf2_sha256$100000$1Y0Flfbwkr1c$csLdormuw7AyoJAusRUEEF21L4b7GdmzqnlsrgUwPO4=",
"last_login": "2018-06-19T23:46:24Z",
"is_superuser": false,
"username": "Croozer",
"first_name": "KeepKroozin",
"last_name": "Croozer",
"email": "ren.karrass+Croozer@gmail.com",
"is_staff": false,
"is_active": true,
"date_joined": "2018-06-19T23:41:03Z",
"is_student": true,
"is_teacher": true,
"isEmailActivated": true,
"profile_picture": "",
"languages": null,
"groups": [],
"user_permissions": []
}
},
{
"model": "api.user",
"pk": 4,
"fields": {
"password": "pbkdf2_sha256$100000$WMjUZ372hQ65$+GrzeZscG678azkrGeozxuGaNowJ9zVYq1hQybEE0hw=",
"last_login": "2018-06-19T23:46:07Z",
"is_superuser": false,
"username": "Hansi",
"first_name": "Hansi",
"last_name": "Hansi",
"email": "ren.karrass+Croozer@gmail.com",
"is_staff": false,
"is_active": true,
"date_joined": "2018-06-19T23:41:47Z",
"is_student": true,
"is_teacher": false,
"isEmailActivated": false,
"profile_picture": "",
"languages": null,
"groups": [],
"user_permissions": []
}
},
{
"model": "api.user",
"pk": 5,
"fields": {
"password": "pbkdf2_sha256$100000$4vi4C89CuKCf$wdfZaxswoBDgtqPdAhuP5+0qQTmFBbj96AN34xT3vrU=",
"last_login": null,
"is_superuser": false,
"username": "HansiF",
"first_name": "",
"last_name": "",
"email": "ren.karrass@gmail.com",
"is_staff": false,
"is_active": true,
"date_joined": "2018-06-20T00:21:48.248Z",
"is_student": true,
"is_teacher": false,
"isEmailActivated": false,
"profile_picture": "",
"languages": null,
"groups": [],
"user_permissions": []
}
},
{
"model": "api.user",
"pk": 6,
"fields": {
"password": "pbkdf2_sha256$100000$hk8DC9nBqbhm$toi4OI/znpPwC7WJ/vivXe476MOfMkOsIPfzlKxhuns=",
"last_login": null,
"is_superuser": false,
"username": "HansiFF",
"first_name": "",
"last_name": "",
"email": "ren.karrass@gmail.com",
"is_staff": false,
"is_active": true,
"date_joined": "2018-06-20T00:23:35.832Z",
"is_student": true,
"is_teacher": false,
"isEmailActivated": false,
"profile_picture": "",
"languages": null,
"groups": [],
"user_permissions": []
}
},
{
"model": "api.user",
"pk": 7,
"fields": {
"password": "pbkdf2_sha256$100000$0sWwqPRiDZQA$oYMfEX6Z9G/R3RcURKgiPUnCvBfML6uec4X6lldFlfk=",
"last_login": "2018-06-20T00:28:15.171Z",
"is_superuser": false,
"username": "HansiFFF",
"first_name": "",
"last_name": "",
"email": "ren.karrass@gmail.com",
"is_staff": false,
"is_active": true,
"date_joined": "2018-06-20T00:27:59.249Z",
"is_student": true,
"is_teacher": false,
"isEmailActivated": false,
"profile_picture": "",
"languages": null,
"groups": [],
"user_permissions": []
}
},
{
"model": "api.student",
"pk": 3,
"fields": {
"courses": []
}
},
{
"model": "api.student",
"pk": 4,
"fields": {
"courses": []
}
},
{
"model": "api.student",
"pk": 5,
"fields": {
"courses": []
}
},
{
"model": "api.student",
"pk": 6,
"fields": {
"courses": []
}
},
{
"model": "api.student",
"pk": 7,
"fields": {
"courses": []
}
},
{
"model": "api.teacher",
"pk": 3,
"fields": {}
},
{
"model": "api.announcement",
"pk": 1,
"fields": {
"title": "Neuer Klingonisch Kurs!",
"author": 3,
"date": "2018-06-20T00:30:09Z",
"image": "",
"content": "Lerne gerne (h\u00f6h\u00f6) hier Klingonisch"
}
},
{
"model": "api.resource",
"pk": 1,
"fields": {
"name": "sadad",
"uploaded": "2018-06-19T23:47:40Z",
"effective_from": "2018-06-19T23:47:41Z",
"uploader": 3,
"expires": "2018-06-19T23:47:45Z",
"content": "resources/None/YDL-Logo05x.png"
}
}
]
37 changes: 37 additions & 0 deletions ydl_api/database_dump/languages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[
{
"model": "api.language",
"pk": 1,
"fields": {
"name": "german"
}
},
{
"model": "api.language",
"pk": 2,
"fields": {
"name": "english"
}
},
{
"model": "api.language",
"pk": 3,
"fields": {
"name": "klingonisch"
}
},
{
"model": "api.language",
"pk": 4,
"fields": {
"name": "elbisch"
}
},
{
"model": "api.language",
"pk": 5,
"fields": {
"name": "arabisch"
}
}
]

0 comments on commit 0127e65

Please sign in to comment.