|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +import json |
| 4 | +from django.http import HttpResponse |
| 5 | +from django.http import HttpResponseRedirect |
| 6 | +from django.views.decorators.csrf import csrf_exempt |
| 7 | +from vilya.libs.template import st |
| 8 | + |
| 9 | + |
| 10 | +@csrf_exempt |
| 11 | +def index(request): |
| 12 | + return HttpResponseRedirect("/settings/emails/") |
| 13 | + |
| 14 | + |
| 15 | +@csrf_exempt |
| 16 | +def emails(request): |
| 17 | + from vilya.models.user import CodeDoubanUserEmails |
| 18 | + errors = [] |
| 19 | + user = request.user |
| 20 | + emails = user.emails |
| 21 | + if request.method == "POST": |
| 22 | + email = request.POST.get('email') |
| 23 | + errors = CodeDoubanUserEmails.validate(user.name, email) |
| 24 | + if not errors: |
| 25 | + CodeDoubanUserEmails.add(user.name, email) |
| 26 | + return HttpResponseRedirect('/settings/emails') |
| 27 | + return HttpResponse(st('/settings/emails.html', **locals())) |
| 28 | + |
| 29 | + |
| 30 | +@csrf_exempt |
| 31 | +def emails_delete(request, id): |
| 32 | + from vilya.models.user import CodeDoubanUserEmails |
| 33 | + user = request.user |
| 34 | + email = CodeDoubanUserEmails.check_own_by_user(user.name, id) |
| 35 | + if email: |
| 36 | + email.delete() |
| 37 | + return HttpResponseRedirect('/settings/emails') |
| 38 | + |
| 39 | + |
| 40 | +@csrf_exempt |
| 41 | +def emails_set_notif(request, id): |
| 42 | + from vilya.models.user import CodeDoubanUserEmails |
| 43 | + user = request.user |
| 44 | + email = CodeDoubanUserEmails.check_own_by_user(user.name, id) |
| 45 | + if email: |
| 46 | + addr = email.email |
| 47 | + user.settings.notif_other_emails \ |
| 48 | + = user.settings.notif_other_emails + [addr] |
| 49 | + return HttpResponseRedirect('/settings/emails') |
| 50 | + |
| 51 | + |
| 52 | +@csrf_exempt |
| 53 | +def emails_un_notif(request, id): |
| 54 | + from vilya.models.user import CodeDoubanUserEmails |
| 55 | + user = request.user |
| 56 | + email = CodeDoubanUserEmails.check_own_by_user(user.name, id) |
| 57 | + if email: |
| 58 | + addr = email.email |
| 59 | + user.settings.notif_other_emails = [ |
| 60 | + e for e in user.settings.notif_other_emails |
| 61 | + if e != addr] |
| 62 | + return HttpResponseRedirect('/settings/emails') |
| 63 | + |
| 64 | + |
| 65 | +@csrf_exempt |
| 66 | +def github(request): |
| 67 | + from vilya.models.user import CodeDoubanUserGithub |
| 68 | + errors = [] |
| 69 | + user = request.user |
| 70 | + githubs = user.githubs |
| 71 | + if request.method == "POST": |
| 72 | + user_name = request.POST.get('github') |
| 73 | + errors = CodeDoubanUserGithub.validate(user.name, user_name) |
| 74 | + if not errors: |
| 75 | + CodeDoubanUserGithub.add(user.name, user_name) |
| 76 | + return HttpResponseRedirect('/settings/github') |
| 77 | + return HttpResponse(st('/settings/github.html', **locals())) |
| 78 | + |
| 79 | + |
| 80 | +@csrf_exempt |
| 81 | +def github_delete(request, id): |
| 82 | + from vilya.models.user import CodeDoubanUserGithub |
| 83 | + if request.POST.get('_method') == 'delete': |
| 84 | + user = request.user |
| 85 | + github = CodeDoubanUserGithub.check_own_by_user(user.name, id) |
| 86 | + if github: |
| 87 | + github.delete() |
| 88 | + return HttpResponseRedirect('/settings/github') |
| 89 | + |
| 90 | + |
| 91 | +def notification(request): |
| 92 | + user = request.user |
| 93 | + return HttpResponse(st('settings/notification.html', **locals())) |
| 94 | + |
| 95 | + |
| 96 | +@csrf_exempt |
| 97 | +def notification_setting(request): |
| 98 | + is_on = request.POST.get('is_on') |
| 99 | + notifications_meta = request.POST.get('notifications_meta') |
| 100 | + user = request.user |
| 101 | + result = "success" |
| 102 | + try: |
| 103 | + user.settings.__setattr__(notifications_meta, is_on) |
| 104 | + except Exception: |
| 105 | + result = "fail" |
| 106 | + return HttpResponse(json.dumps({"result": result})) |
| 107 | + |
| 108 | + |
| 109 | +@csrf_exempt |
| 110 | +def ssh(request): |
| 111 | + from vilya.models.sshkey import SSHKey |
| 112 | + errors = [] |
| 113 | + key_lines = '' |
| 114 | + user = request.user |
| 115 | + sshkeys = user.sshkeys |
| 116 | + if request.method == "POST": |
| 117 | + key_lines = request.POST.get('ssh') |
| 118 | + newsshkeys = [] |
| 119 | + errorkeys = [] |
| 120 | + for index, line in enumerate(key_lines.splitlines()): |
| 121 | + valid = SSHKey.validate(user.name, line) |
| 122 | + if not valid: |
| 123 | + errorkeys.append((index, line)) |
| 124 | + continue |
| 125 | + duplicated = SSHKey.is_duplicated(user.name, line) |
| 126 | + if duplicated: |
| 127 | + errorkeys.append((index, line)) |
| 128 | + continue |
| 129 | + newsshkeys.append(line) |
| 130 | + |
| 131 | + if not errorkeys: |
| 132 | + for key in newsshkeys: |
| 133 | + SSHKey.add(user.name, key) |
| 134 | + return HttpResponseRedirect('/settings/ssh') |
| 135 | + |
| 136 | + error_prefix = 'Please check your SSH Key, Line: ' |
| 137 | + for no, key in errorkeys: |
| 138 | + error = error_prefix + '%s ' % no |
| 139 | + errors.append(error) |
| 140 | + return HttpResponse(st('/settings/ssh.html', **locals())) |
| 141 | + |
| 142 | + |
| 143 | +@csrf_exempt |
| 144 | +def ssh_delete(request, id): |
| 145 | + from vilya.models.sshkey import SSHKey |
| 146 | + user = request.user |
| 147 | + if request.POST.get('_method') == 'delete': |
| 148 | + sshkey = SSHKey.check_own_by_user(user.name, id) |
| 149 | + if sshkey: |
| 150 | + sshkey.delete() |
| 151 | + return HttpResponseRedirect('/settings/ssh') |
| 152 | + |
| 153 | + |
| 154 | +def codereview(request): |
| 155 | + user = request.user |
| 156 | + return HttpResponse(st('settings/codereview.html', **locals())) |
| 157 | + |
| 158 | + |
| 159 | +@csrf_exempt |
| 160 | +def codereview_setting(request): |
| 161 | + is_enable = request.POST.get('is_enable') |
| 162 | + field = request.POST.get('field') |
| 163 | + user = request.user |
| 164 | + result = "success" |
| 165 | + origin = user.settings.__getattr__(field) |
| 166 | + try: |
| 167 | + user.settings.__setattr__(field, is_enable) |
| 168 | + except Exception: |
| 169 | + result = "fail" |
| 170 | + return HttpResponse(json.dumps({"result": result, "origin": origin})) |
0 commit comments