def authorization(self): """ User Authorization. :returns: dto.UserModel """ google_user = users.get_current_user() if not google_user: # didn't login with google account return UserModel() if User.all().count(1) == 0: # set up default user with google account user = self.__register_user(google_user, UserPermission.root) return UserModel(user) if google_user: # auth with google account members = User.gql('where email = :1', google_user.email().lower()).fetch(1) if len(members) > 0: # got the user return UserModel(members[0]) else: # register a new user user = self.__register_user(google_user) return UserModel(user) return UserModel()
def get_users(request): form = SearchForm(**request.GET.dict()) if form.keyword.data: users, total = UserModel.search(form.keyword.data, form.index.data, utils.default_page_size) else: query = UserModel.all().order('name') total = query.count() users = query.fetch(utils.default_page_size, form.index.data * utils.default_page_size) return JsonResponse(PageList(form.index.data, utils.default_page_size, total, users))
def test_user_model_with_data(self): fake_key = MagicMock() fake_key.id.return_value = 100 user = User() user.name = 'name' user.email = '*****@*****.**' user.key = MagicMock(return_value=fake_key) model = UserModel(user) self.assertEqual(model.id, 100) self.assertEqual(model.permission, UserPermission.normal) self.assertEqual(model.name, 'name') self.assertEqual(model.email, '*****@*****.**') self.assertEqual(model.logout_url, 'logout_url')
def invite_user(request): form = UserForm(name='invite', **json.loads(request.body)) if not form.validate(): raise Http400 user = UserModel.invite_user(request, form.email.data) return JsonResponse(user)
def __register_user(self, google_user, permission=UserPermission.normal): """ Register the user. :param google_user: The google.appengine.api.users.get_current_user(). :param permission: The user's permission. :return: The datastore.user_model.UserModel object. """ user = User() user.email = google_user.email().lower() user.name = google_user.nickname() user.permission = permission user.put() user.get(user.key()) return user
def delete_user(request, user_id): user_id = long(user_id) if request.user.key().id() == user_id: raise Http403 user = UserModel.get_by_id(user_id) if user is None: raise Http404 user.delete() return HttpResponse()
def update_profile(request): form = ProfileForm(**json.loads(request.body)) if not form.validate(): raise Http400 user = UserModel.get_by_id(request.user.key().id()) user.name = form.name.data user.put() return JsonResponse(user)
def get_application(request, application_id): application = ApplicationModel.get_by_id(long(application_id)) if application is None: raise Http404 if request.user.permission != UserPermission.root and\ request.user.key().id() not in application.member_ids: raise Http403 result = application.dict() result['members'] = [x.dict() for x in UserModel.get_by_id(application.member_ids) if not x is None] return JsonResponse(result)
def update_profile(request): form = ProfileForm(**json.loads(request.body)) if not form.validate(): raise Http400 user = UserModel.get_by_id(request.user.key().id()) if user is None: raise Http404 user.name = form.name.data user.put() return JsonResponse(user)
def update_user(request, user_id): user_id = long(user_id) form = UserForm(**json.loads(request.body)) if not form.validate(): raise Http400 user = UserModel.get_by_id(user_id) if user is None: raise Http404 user.name = form.name.data user.permission = form.permission.data user.put() return JsonResponse(user)
def get_project(request, project_id): project = ProjectModel.get_by_id(long(project_id)) if project is None: raise Http404('not found the project %s' % project_id) if request.user.permission != UserPermission.root and\ request.user.key().id() not in project.member_ids: raise Http403 labels = LabelModel.all().filter('project =', project.key()).order('title').fetch(100) result = project.dict() result['members'] = [x.dict() for x in UserModel.get_by_id(project.member_ids) if not x is None] result['labels'] = [x.dict() for x in labels] return JsonResponse(result)
def add_project_member(request, project_id): form = UserForm(name='invite', **json.loads(request.body)) if not form.validate(): raise Http400 project = ProjectModel.get_by_id(long(project_id)) if project is None: raise Http404 if request.user.permission != UserPermission.root and\ request.user.key().id() not in project.root_ids: raise Http403 user = UserModel.invite_user(request, form.email.data) project.member_ids.append(user.key().id()) project.put() return JsonResponse(user)
def add_application_member(request, application_id): form = UserForm(name='invite', **json.loads(request.body)) if not form.validate(): raise Http400 application = ApplicationModel.get_by_id(long(application_id)) if application is None: raise Http404 if request.user.permission != UserPermission.root and\ request.user.key().id() not in application.root_ids: raise Http403 user = UserModel.invite_user(request, form.email.data) application.member_ids.append(user.key().id()) application.put() return JsonResponse(user)
def __add_log(request, application_key, args): """ Add the log. :param args: {dict} The log. :param is_jsonp: {bool} :return: The django response. """ form = APILogForm(key=application_key, **args) if not form.validate(): raise Http400 applications = ApplicationModel.all().filter("app_key =", form.key.data).fetch(1) if not len(applications): raise Http404 application = applications[0] # Is the log exist? logs = ( LogModel.all() .filter("title =", form.title.data) .filter("is_close =", False) .filter("application =", application.key()) .fetch(1) ) if len(logs): # update log log = logs[0] log.count += 1 log.update_time = datetime.utcnow() if not form.user.data is None and form.user.data not in log.users: log.users.append(form.user.data) else: # add the new log log = LogModel(application=application, title=form.title.data) if not form.user.data is None: log.users = [form.user.data] if not form.document.data is None: log.document = form.document.data log.user_agent = request.META.get("HTTP_USER_AGENT") log.ip = os.environ.get("REMOTE_ADDR") log.put() if log.count == 1 and application.email_notification: # send email notification to members gae_account = getattr(settings, "GAE_ACCOUNT") domain = getattr(settings, "HOST") users = UserModel.get_by_id(application.member_ids) message = mail.EmailMessage(sender=gae_account, subject="%s has a new log at Victorique." % application.title) message.to = [x.email for x in users if not x is None] message.body = "There is a new log at Victorique.\n%s\nhttps://%s/applications/%s/logs/%s" % ( log.title, domain, application.key().id(), log.key().id(), ) message.send() index = search.Index(namespace="Logs", name=str(application.key().id())) search_document = search.Document( doc_id=str(log.key().id()), fields=[ search.TextField(name="users", value=unicode(log.users)), search.TextField(name="title", value=log.title), search.TextField(name="document", value=log.document_json), search.TextField(name="ip", value=log.ip), search.TextField(name="user_agent", value=log.user_agent), search.DateField(name="update_time", value=log.update_time), ], ) index.put(search_document)
def get_users(request): form = SearchForm(**request.GET.dict()) query = UserModel.all().order('create_time') total = query.count() applications = query.fetch(utils.default_page_size, form.index.data * utils.default_page_size) return JsonResponse(PageList(form.index.data, utils.default_page_size, total, applications))
def process_request(self, request): request.user = UserModel.authorization()
def get_user(request, user_id): user_id = long(user_id) user = UserModel.get_by_id(user_id) if user is None: raise Http404 return JsonResponse(user)