def group_add_member(request, username): teamUser = GsuserManager.get_user_by_name(username) team_group_id = int(request.POST.get('team_group_id', '0')) teamGroup = TeamManager.get_teamGroup_by_id(team_group_id) if not teamGroup or teamGroup.team_user_id != teamUser.id: return _response_not_manage_rights(request) member_username = request.POST.get('member_username', '') member_user = GsuserManager.get_user_by_name(member_username) if not member_user: return json_failed(500, u'没有该用户名') teamMember = TeamManager.get_teamMember_by_teamUserId_userId( teamUser.id, member_user.id) if not teamMember: return json_failed( 500, u'用户 %s 还没有加入团队帐号 %s' % (member_user.username, teamUser.username)) groupMember = TeamManager.get_groupMember_by_teamGroupId_memberUserId( teamGroup.id, member_user.id) if groupMember: return json_success(u'用户 %s 已经在该组' % member_user.username) groupMember = GroupMember(team_user_id=teamUser.id, group_id=teamGroup.id, member_user_id=member_user.id) groupMember.save() return json_success(u'成功添加用户 %s 到组 %s' % (member_user.username, teamGroup.name))
def __get_uniq_username(tp_username): if tp_username is not None and not tp_username.startswith('-'): user = GsuserManager.get_user_by_name(tp_username) if user is None: return tp_username for i in range(0, 1000): random_username = '******' % random.getrandbits(64) user = GsuserManager.get_user_by_name(random_username) if user is None: return random_username return None
def __get_uniq_username(tp_username): if tp_username is not None and not tp_username.startswith('-'): user = GsuserManager.get_user_by_name(tp_username) if user is None: return tp_username for i in range(0, 1000): random_username = '******' % random.getrandbits(64) user = GsuserManager.get_user_by_name(random_username) if user is None: return random_username return None
def update(request, user_name, repo_name, issue_id, attr): repo = RepoManager.get_repo_by_name(user_name, repo_name) if repo is None: raise Http404 issue = IssueManager.get_issue(repo.id, issue_id) if issue is None: return _json_failed() has_issue_modify_right = _has_issue_modify_right(request, issue, repo) if not has_issue_modify_right: return _json_failed() orgi_issue = copy.copy(issue) (key, value) = attr.split('___', 1) if key == 'assigned': user = GsuserManager.get_user_by_name(value) if user is None: return _json_failed() repoMember = RepoManager.get_repo_member(repo.id, user.id) if repoMember is None: return _json_failed() issue.assigned = repoMember.user_id issue.save() FeedManager.notif_issue_status(request.user, issue, ISSUE_STATUS.ASSIGNED) FeedManager.feed_issue_change(request.user, repo, orgi_issue, issue.id) return _json_ok() value = int(value) if key == 'tracker': issue.tracker = value elif key == 'status': issue.status = value elif key == 'priority': issue.priority = value issue.save() FeedManager.feed_issue_change(request.user, repo, orgi_issue, issue.id) return _json_ok()
def watch_user(request, user_name): title = u'%s / 关注的用户' % user_name gsuser = GsuserManager.get_user_by_name(user_name) if gsuser is None: raise Http404 gsuserprofile = GsuserManager.get_userprofile_by_id(gsuser.id) feedAction = FeedAction() raw_watch_users = feedAction.get_watch_users(gsuser.id, 0, 100) watch_user_ids = [int(x[0]) for x in raw_watch_users] watch_users_map = GsuserManager.map_users(watch_user_ids) watch_users = [watch_users_map[x] for x in watch_user_ids if x in watch_users_map] raw_bewatch_users = feedAction.get_bewatch_users(gsuser.id, 0, 100) bewatch_user_ids = [int(x[0]) for x in raw_bewatch_users] bewatch_users_map = GsuserManager.map_users(bewatch_user_ids) bewatch_users = [bewatch_users_map[x] for x in bewatch_user_ids if x in bewatch_users_map] # fixed on detect need_fix = False if len(watch_users) != gsuserprofile.watch: gsuserprofile.watch = len(watch_users) need_fix = True if len(bewatch_users) < 100 and len(bewatch_users) != gsuserprofile.be_watched: gsuserprofile.be_watched = len(bewatch_users) need_fix = True if need_fix: gsuserprofile.save() response_dictionary = {'mainnav': 'user', 'title': title, 'watch_users': watch_users, 'bewatch_users': bewatch_users} response_dictionary.update(get_common_user_dict(request, gsuser, gsuserprofile)) return render_to_response('user/watch_user.html', response_dictionary, context_instance=RequestContext(request))
def _do_event(self, event): username = event['username'] reponame = event['reponame'] remote_git_url = event['remote_git_url'] local_user = GsuserManager.get_user_by_name(username) local_repo = RepoManager.get_repo_by_name(username, reponame) if local_user is None or local_repo is None or local_repo.status == 0: return local_repo_path = local_repo.get_abs_repopath() if os.path.exists(local_repo_path): return args = ['/bin/bash', '/opt/bin/git-import-remote-repo.sh' ] + [local_repo_path, remote_git_url] try: popen = Popen(args, stdout=PIPE, shell=False, close_fds=True) output = popen.communicate()[0].strip() returncode = popen.returncode if returncode == 0: RepoManager.check_export_ok_file(local_repo) diff_size = long(output) RepoManager.update_user_repo_quote(local_user, local_repo, diff_size) local_repo.status = 0 local_repo.save() else: local_repo.status = 500 local_repo.save() except Exception, e: local_repo.status = 500 local_repo.save() logger.exception(e)
def _do_event(self, event): username = event['username'] reponame = event['reponame'] remote_git_url = event['remote_git_url'] local_user = GsuserManager.get_user_by_name(username) local_repo = RepoManager.get_repo_by_name(username, reponame) if local_user is None or local_repo is None or local_repo.status == 0: return local_repo_path = local_repo.get_abs_repopath() if os.path.exists(local_repo_path): return args = ['/bin/bash', '/opt/bin/git-import-remote-repo.sh'] + [local_repo_path, remote_git_url] try: popen = Popen(args, stdout=PIPE, shell=False, close_fds=True) output = popen.communicate()[0].strip() returncode = popen.returncode if returncode == 0: RepoManager.check_export_ok_file(local_repo) diff_size = long(output) RepoManager.update_user_repo_quote(local_user, local_repo, diff_size) local_repo.status = 0 local_repo.save() else: local_repo.status = 500 local_repo.save() except Exception, e: local_repo.status = 500 local_repo.save() logger.exception(e)
def watch_repo(request, user_name): title = u'%s / 关注的仓库' % user_name gsuser = GsuserManager.get_user_by_name(user_name) if gsuser is None: raise Http404 gsuserprofile = GsuserManager.get_userprofile_by_id(gsuser.id) feedAction = FeedAction() raw_watch_repos = feedAction.get_watch_repos(gsuser.id, 0, 100) watch_repo_ids = [int(x[0]) for x in raw_watch_repos] watch_repos_map = RepoManager.merge_repo_map_ignore_visibly(watch_repo_ids) watch_repos = [ watch_repos_map[x] for x in watch_repo_ids if x in watch_repos_map ] response_dictionary = { 'mainnav': 'user', 'title': title, 'watch_repos': watch_repos } # fixed on detect if len(watch_repos) != gsuserprofile.watchrepo: gsuserprofile.watchrepo = len(watch_repos) gsuserprofile.save() response_dictionary.update( get_common_user_dict(request, gsuser, gsuserprofile)) return render_to_response('user/watch_repo.html', response_dictionary, context_instance=RequestContext(request))
def notif_at(self, notif_type, from_user_id, relative_id, message): at_name_list = FeedUtils.list_atname(message) user_unread_message_dict = {} for at_name in at_name_list: at_user = GsuserManager.get_user_by_name(at_name) if at_user is not None: to_user_id = at_user.id notifMessage = None # disable duplicate notify exists_notifMessage = self.get_notifmessage_by_userId_notifType_relativeId(to_user_id, notif_type, relative_id) if exists_notifMessage is not None: continue if notif_type == NOTIF_TYPE.AT_COMMIT: notifMessage = NotifMessage.create_at_commit(from_user_id, to_user_id, relative_id) elif notif_type == NOTIF_TYPE.AT_MERGE: notifMessage = NotifMessage.create_at_merge(from_user_id, to_user_id, relative_id) elif notif_type == NOTIF_TYPE.AT_ISSUE: notifMessage = NotifMessage.create_at_issue(from_user_id, to_user_id, relative_id) elif notif_type == NOTIF_TYPE.AT_ISSUE_COMMENT: notifMessage = NotifMessage.create_at_issue_comment(from_user_id, to_user_id, relative_id) if notifMessage is None: continue self.message_save_and_notif(notifMessage) if to_user_id not in user_unread_message_dict: user_unread_message_dict[to_user_id] = 0 user_unread_message_dict[to_user_id] = user_unread_message_dict[to_user_id] + 1 for to_user_id, unread_message in user_unread_message_dict.items(): at_userprofile = GsuserManager.get_userprofile_by_id(to_user_id) at_userprofile.unread_message = at_userprofile.unread_message + unread_message at_userprofile.save()
def group(request, username, group_id): teamUser = GsuserManager.get_user_by_name(username) teamUserprofile = GsuserManager.get_userprofile_by_id(teamUser.id) teamGroup = TeamManager.get_teamGroup_by_id(group_id) if not teamGroup or teamGroup.team_user_id != teamUser.id: raise Http404 groupMembers = TeamManager.list_groupMember_by_teamGroupId(teamGroup.id) userIdInGroupSet = Set([x.member_user_id for x in groupMembers]) teamMembers = TeamManager.list_teamMember_by_teamUserId(teamUser.id) teamMembersNotInGroup = [ x for x in teamMembers if x.user_id not in userIdInGroupSet ] current = 'settings' sub_nav = 'groups' title = u'%s / 设置 / 组管理 / %s' % (teamUser.username, teamGroup.name) response_dictionary = { 'current': current, 'sub_nav': sub_nav, 'title': title, 'teamGroup': teamGroup, 'groupMembers': groupMembers, 'teamMembersNotInGroup': teamMembersNotInGroup } response_dictionary.update( _get_common_team_dict(request, teamUser, teamUserprofile)) return render_to_response('team/group.html', response_dictionary, context_instance=RequestContext(request))
def update(request, user_name, repo_name, issue_id, attr): repo = RepoManager.get_repo_by_name(user_name, repo_name) if repo is None: raise Http404 issue = IssueManager.get_issue(repo.id, issue_id) if issue is None: return _json_failed() has_issue_modify_right = _has_issue_modify_right(request, issue, repo) if not has_issue_modify_right: return _json_failed() orgi_issue = copy.copy(issue) (key, value) = attr.split('___', 1) if key == 'assigned': user = GsuserManager.get_user_by_name(value) if user is None: return _json_failed() repoMember = RepoManager.get_repo_member(repo.id, user.id) if repoMember is None: return _json_failed() issue.assigned = repoMember.user_id issue.save() FeedManager.notif_issue_status(request.user, issue, ISSUE_STATUS.ASSIGNED) FeedManager.feed_issue_change(request.user, repo, orgi_issue, issue.id) return _json_ok() value = int(value) if key == 'tracker': issue.tracker = value elif key == 'status': issue.status = value elif key == 'priority': issue.priority = value issue.save() FeedManager.feed_issue_change(request.user, repo, orgi_issue, issue.id) return _json_ok()
def recommend_delete(request, user_name, recommend_id): gsuser = GsuserManager.get_user_by_name(user_name) if gsuser is None: raise Http404 recommend = GsuserManager.get_recommend_by_id(recommend_id) if recommend.user_id == request.user.id: recommend.visibly = 1 recommend.save() return json_success(u'成功删除评论')
def recommend_delete(request, user_name, recommend_id): gsuser = GsuserManager.get_user_by_name(user_name) if gsuser is None: raise Http404 recommend = GsuserManager.get_recommend_by_id(recommend_id) if recommend.user_id == request.user.id: recommend.visibly = 1 recommend.save() return json_success(u'成功删除评论')
def group_add_member(request, username): teamUser = GsuserManager.get_user_by_name(username) team_group_id = int(request.POST.get('team_group_id', '0')) teamGroup = TeamManager.get_teamGroup_by_id(team_group_id) if not teamGroup or teamGroup.team_user_id != teamUser.id: return _response_not_manage_rights(request) member_username = request.POST.get('member_username', '') member_user = GsuserManager.get_user_by_name(member_username) if not member_user: return json_failed(500, u'没有该用户名') teamMember = TeamManager.get_teamMember_by_teamUserId_userId(teamUser.id, member_user.id) if not teamMember: return json_failed(500, u'用户 %s 还没有加入团队帐号 %s' % (member_user.username, teamUser.username)) groupMember = TeamManager.get_groupMember_by_teamGroupId_memberUserId(teamGroup.id, member_user.id) if groupMember: return json_success(u'用户 %s 已经在该组' % member_user.username) groupMember = GroupMember(team_user_id=teamUser.id, group_id=teamGroup.id, member_user_id=member_user.id) groupMember.save() return json_success(u'成功添加用户 %s 到组 %s' % (member_user.username, teamGroup.name))
def permission_grant(request, username): teamUser = GsuserManager.get_user_by_name(username) grant_type = request.POST.get('grant_type', 'global') permission = int(request.POST.get('permission', '0')) if grant_type == 'global': TeamManager.grant_team_global_permission(teamUser.id, permission) elif grant_type == 'user': user_id = int(request.POST.get('user_id', '0')) TeamManager.grant_team_user_permission(teamUser.id, user_id, permission) return json_success(u'赋予权限成功')
def groups(request, username): teamUser = GsuserManager.get_user_by_name(username) teamUserprofile = GsuserManager.get_userprofile_by_id(teamUser.id) teamGroups = TeamManager.list_teamGroup_by_teamUserId(teamUser.id) current = 'settings'; sub_nav = 'groups'; title = u'%s / 设置 / 组管理' % (teamUser.username) response_dictionary = {'current': current, 'sub_nav': sub_nav, 'title': title, 'teamGroups': teamGroups} response_dictionary.update(_get_common_team_dict(request, teamUser, teamUserprofile)) return render_to_response('team/groups.html', response_dictionary, context_instance=RequestContext(request))
def permission_grant(request, username): teamUser = GsuserManager.get_user_by_name(username) grant_type = request.POST.get('grant_type', 'global') permission = int(request.POST.get('permission', '0')) if grant_type == 'global': TeamManager.grant_team_global_permission(teamUser.id, permission) elif grant_type == 'user': user_id = int(request.POST.get('user_id', '0')) TeamManager.grant_team_user_permission(teamUser.id, user_id, permission) return json_success(u'赋予权限成功')
def _get_team_user_userprofile(request, username): current_user = GsuserManager.get_user_by_name(username) if not current_user: return (request.user, request.userprofile) teamMember = TeamManager.get_teamMember_by_teamUserId_userId(current_user.id, request.user.id) if not teamMember: return (request.user, request.userprofile) current_userprofile = GsuserManager.get_userprofile_by_id(current_user.id) if current_userprofile: return (current_user, current_userprofile) return (request.user, request.userprofile)
def stats(request, user_name): user = GsuserManager.get_user_by_name(user_name) if user is None: raise Http404 stats_dict = get_stats_dict(request, user) gsuserprofile = GsuserManager.get_userprofile_by_id(user.id) response_dictionary = {'title': u'%s / 最近统计' % (user.username), 'gsuserprofile': gsuserprofile} response_dictionary.update(stats_dict) return render_to_response('user/stats.html', response_dictionary, context_instance=RequestContext(request))
def group_add(request, username): teamUser = GsuserManager.get_user_by_name(username) group_name = request.POST.get('group_name', '') if not re.match('^[ 0-9a-zA-Z_-]+$', group_name): return json_failed(403, u'组名不符合规范,只允许[ 0-9a-zA-Z_-]之内的字符') teamGroup = TeamManager.get_teamGroup_by_teamUserId_name(teamUser.id, group_name) if teamGroup: return json_failed(500, u'组名已经存在') teamGroup = TeamGroup(team_user_id = teamUser.id, name = group_name, desc = '') teamGroup.save() return json_success(u'成功创建组 %s' % group_name)
def get_attrs(username, reponame): user = GsuserManager.get_user_by_name(username) if not user: return_all_none() userprofile = GsuserManager.get_userprofile_by_id(user.id) if not userprofile: return_all_none() repo = RepoManager.get_repo_by_userId_name(user.id, reponame) if not repo: return_all_none() abs_repo_path = repo.get_abs_repopath() return (user, userprofile, repo, abs_repo_path)
def star_repo(request, user_name): title = u'%s / 标星的仓库' % user_name gsuser = GsuserManager.get_user_by_name(user_name) if gsuser is None: raise Http404 gsuserprofile = GsuserManager.get_userprofile_by_id(gsuser.id) star_repos = RepoManager.list_star_repo(gsuser.id, 0, 500) response_dictionary = {'mainnav': 'user', 'title': title, 'star_repos': star_repos} response_dictionary.update(get_common_user_dict(request, gsuser, gsuserprofile)) return render_to_response('user/star_repo.html', response_dictionary, context_instance=RequestContext(request))
def get_attrs(username, reponame): user = GsuserManager.get_user_by_name(username) if not user: return_all_none() userprofile = GsuserManager.get_userprofile_by_id(user.id) if not userprofile: return_all_none() repo = RepoManager.get_repo_by_userId_name(user.id, reponame) if not repo: return_all_none() abs_repo_path = repo.get_abs_repopath() return (user, userprofile, repo, abs_repo_path)
def wrap(request, *args, **kwargs): if len(args) >= 1: username = args[0] teamUser = GsuserManager.get_user_by_name(username) if not teamUser: return _response_not_admin_rights(request) if not request.user.is_authenticated(): return HttpResponseRedirect('/login/?next=' + urlquote(request.path)) teamMember = TeamManager.get_teamMember_by_teamUserId_userId(teamUser.id, request.user.id) if not teamMember or not teamMember.has_admin_rights(): return _response_not_admin_rights(request) return function(request, *args, **kwargs)
def group_add(request, username): teamUser = GsuserManager.get_user_by_name(username) group_name = request.POST.get('group_name', '') if not re.match('^[ 0-9a-zA-Z_-]+$', group_name): return json_failed(403, u'组名不符合规范,只允许[ 0-9a-zA-Z_-]之内的字符') teamGroup = TeamManager.get_teamGroup_by_teamUserId_name( teamUser.id, group_name) if teamGroup: return json_failed(500, u'组名已经存在') teamGroup = TeamGroup(team_user_id=teamUser.id, name=group_name, desc='') teamGroup.save() return json_success(u'成功创建组 %s' % group_name)
def _get_team_user_userprofile(request, username): current_user = GsuserManager.get_user_by_name(username) if not current_user: return (request.user, request.userprofile) teamMember = TeamManager.get_teamMember_by_teamUserId_userId( current_user.id, request.user.id) if not teamMember: return (request.user, request.userprofile) current_userprofile = GsuserManager.get_userprofile_by_id(current_user.id) if current_userprofile: return (current_user, current_userprofile) return (request.user, request.userprofile)
def group_remove_member(request, username): teamUser = GsuserManager.get_user_by_name(username) team_group_id = int(request.POST.get('team_group_id', '0')) teamGroup = TeamManager.get_teamGroup_by_id(team_group_id) if not teamGroup or teamGroup.team_user_id != teamUser.id: return _response_not_manage_rights(request) member_user_id = int(request.POST.get('member_user_id', '0')) groupMember = TeamManager.get_groupMember_by_teamGroupId_memberUserId(teamGroup.id, member_user_id) if groupMember: groupMember.visibly = 1 groupMember.save() return json_success(u'从 %s 组移除用户' % (teamGroup.name))
def get_user_repo_attr(username, reponame): nones = (None, None, None, None) user = GsuserManager.get_user_by_name(username) if not user: return nones userprofile = GsuserManager.get_userprofile_by_id(user.id) if not userprofile: return nones repo = RepoManager.get_repo_by_userId_name(user.id, reponame) if not repo: return nones abs_repopath = repo.get_abs_repopath() return (user, userprofile, repo, abs_repopath)
def group_remove(request, username): teamUser = GsuserManager.get_user_by_name(username) team_group_id = int(request.POST.get('team_group_id', '0')) teamGroup = TeamManager.get_teamGroup_by_id(team_group_id) if not teamGroup or teamGroup.team_user_id != teamUser.id: return _response_not_manage_rights(request) groupMembers = TeamManager.list_groupMember_by_teamGroupId(teamGroup.id) for groupMember in groupMembers: groupMember.visibly = 1 groupMember.save() teamGroup.visibly = 1 teamGroup.save() return json_success(u'成功删除组 %s' % teamGroup.name)
def get_user_repo_attr(username, reponame): nones = (None, None, None, None) user = GsuserManager.get_user_by_name(username) if not user: return nones userprofile = GsuserManager.get_userprofile_by_id(user.id) if not userprofile: return nones repo = RepoManager.get_repo_by_userId_name(user.id, reponame) if not repo: return nones abs_repopath = repo.get_abs_repopath() return (user, userprofile, repo, abs_repopath)
def group_remove_member(request, username): teamUser = GsuserManager.get_user_by_name(username) team_group_id = int(request.POST.get('team_group_id', '0')) teamGroup = TeamManager.get_teamGroup_by_id(team_group_id) if not teamGroup or teamGroup.team_user_id != teamUser.id: return _response_not_manage_rights(request) member_user_id = int(request.POST.get('member_user_id', '0')) groupMember = TeamManager.get_groupMember_by_teamGroupId_memberUserId( teamGroup.id, member_user_id) if groupMember: groupMember.visibly = 1 groupMember.save() return json_success(u'从 %s 组移除用户' % (teamGroup.name))
def _set_real_author_name(self, item): if 'author_name' not in item or 'author_email' not in item: return author_name = item['author_name'] author_email = item['author_email'] item['real_author_name'] = '' user = GsuserManager.get_user_by_email(author_email) if user: item['real_author_name'] = user.username return user = GsuserManager.get_user_by_name(author_name) if user: item['real_author_name'] = user.username
def _set_real_committer_name(self, item): if 'committer_name' not in item or 'committer_email' not in item: return committer_name = item['committer_name'] committer_email = item['committer_email'] item['real_committer_name'] = '' user = GsuserManager.get_user_by_email(committer_email) if user: item['real_committer_name'] = user.username return user = GsuserManager.get_user_by_name(committer_name) if user: item['real_committer_name'] = user.username
def group_remove(request, username): teamUser = GsuserManager.get_user_by_name(username) team_group_id = int(request.POST.get('team_group_id', '0')) teamGroup = TeamManager.get_teamGroup_by_id(team_group_id) if not teamGroup or teamGroup.team_user_id != teamUser.id: return _response_not_manage_rights(request) groupMembers = TeamManager.list_groupMember_by_teamGroupId(teamGroup.id) for groupMember in groupMembers: groupMember.visibly = 1 groupMember.save() teamGroup.visibly = 1 teamGroup.save() return json_success(u'成功删除组 %s' % teamGroup.name)
def user(request, user_name): title = u'%s / 概括' % user_name gsuser = GsuserManager.get_user_by_name(user_name) if gsuser is None: raise Http404 gsuserprofile = GsuserManager.get_userprofile_by_id(gsuser.id) if gsuserprofile.is_team_account == 1 and TeamManager.is_teamMember( gsuser.id, request.user.id): return HttpResponseRedirect('/%s/-/dashboard/' % user_name) recommendsForm = RecommendsForm() repos = [] if gsuser.id == request.user.id: repos = RepoManager.list_repo_by_userId(gsuser.id, 0, 100) else: repos = RepoManager.list_unprivate_repo_by_userId(gsuser.id, 0, 100) now = datetime.now() last30days = timeutils.getlast30days(now) last30days_commit = get_last30days_commit(gsuser) feedAction = FeedAction() raw_watch_repos = feedAction.get_watch_repos(gsuser.id, 0, 10) watch_repo_ids = [int(x[0]) for x in raw_watch_repos] watch_repos_map = RepoManager.merge_repo_map(watch_repo_ids) watch_repos = [ watch_repos_map[x] for x in watch_repo_ids if x in watch_repos_map ] pri_user_feeds = feedAction.get_pri_user_feeds(gsuser.id, 0, 10) pub_user_feeds = feedAction.get_pub_user_feeds(gsuser.id, 0, 10) feeds_as_json = get_feeds_as_json(request, pri_user_feeds, pub_user_feeds) star_repos = RepoManager.list_star_repo(gsuser.id, 0, 20) response_dictionary = { 'mainnav': 'user', 'title': title, 'recommendsForm': recommendsForm, 'repos': repos, 'watch_repos': watch_repos, 'star_repos': star_repos, 'last30days': last30days, 'last30days_commit': last30days_commit, 'feeds_as_json': feeds_as_json } response_dictionary.update( get_common_user_dict(request, gsuser, gsuserprofile)) return render_to_response('user/user.html', response_dictionary, context_instance=RequestContext(request))
def _join(request, joinForm, joinVia, tip, step): if step is None: step = '0' error = u''; title = u'注册' if step == '0' and request.method == 'POST': joinForm = JoinForm(request.POST) if joinForm.is_valid(): email = joinForm.cleaned_data['email'] username = joinForm.cleaned_data['username'] password = joinForm.cleaned_data['password'] ref_hash = joinForm.cleaned_data['ref_hash'] user_by_email = GsuserManager.get_user_by_email(email) user_by_username = GsuserManager.get_user_by_name(username) if user_by_email is None and user_by_username is None: if ref_hash: userViaRef = GsuserManager.get_userViaRef_by_refhash(ref_hash) if userViaRef and _create_user_and_authenticate(request, username, email, password, ref_hash, True): return HttpResponseRedirect('/join/3/') client_ip = _get_client_ip(request) cache_join_client_ip_count = cache.get(CacheKey.JOIN_CLIENT_IP % client_ip) if cache_join_client_ip_count is None: cache_join_client_ip_count = 0 cache_join_client_ip_count = cache_join_client_ip_count + 1 cache.set(CacheKey.JOIN_CLIENT_IP % client_ip, cache_join_client_ip_count) if cache_join_client_ip_count < 10 and _create_user_and_authenticate(request, username, email, password, ref_hash, False): return HttpResponseRedirect('/join/3/') Mailer().send_verify_account(email, username, password, ref_hash) goto = '' email_suffix = email.split('@')[-1] if email_suffix in COMMON_EMAIL_DOMAIN: goto = COMMON_EMAIL_DOMAIN[email_suffix] return HttpResponseRedirect('/join/1/?goto=' + goto) error = u'欢迎回来, email: %s 或者 name: %s 已经注册过了, 您只需要直接登陆就行。' % (email, username) else: error = u'啊? 邮箱或验证码有误输入。注意大小写和前后空格。' if len(step) > 1: email = cache.get(step + '_email') username = cache.get(step + '_username') password = cache.get(step + '_password') ref_hash = cache.get(step + '_ref_hash') if email is None or username is None or password is None or not email_re.match(email) or not re.match("^[a-zA-Z0-9_-]+$", username) or username.startswith('-') or username in MAIN_NAVS: return HttpResponseRedirect('/join/4/') if _create_user_and_authenticate(request, username, email, password, ref_hash, True): return HttpResponseRedirect('/join/3/') else: error = u'啊? 用户名或密码有误输入,注意大小写和前后空格。' response_dictionary = {'step': step, 'error': error, 'title': title, 'joinForm': joinForm, 'joinVia': joinVia, 'tip': tip} return render_to_response('user/join.html', response_dictionary, context_instance=RequestContext(request))
def stats(request, user_name): title = u'%s / 统计' % user_name gsuser = GsuserManager.get_user_by_name(user_name) if gsuser is None: raise Http404 gsuserprofile = GsuserManager.get_userprofile_by_id(gsuser.id) now = datetime.now() last30days = timeutils.getlast30days(now) last30days_commit = get_last30days_commit(gsuser) response_dictionary = {'mainnav': 'user', 'title': title, 'last30days': last30days, 'last30days_commit': last30days_commit} response_dictionary.update(get_common_user_dict(request, gsuser, gsuserprofile)) return render_to_response('user/stats.html', response_dictionary, context_instance=RequestContext(request))
def wrap(request, *args, **kwargs): if len(args) >= 1: username = args[0] teamUser = GsuserManager.get_user_by_name(username) if not teamUser: return _response_not_admin_rights(request) if not request.user.is_authenticated(): return HttpResponseRedirect('/login/?next=' + urlquote(request.path)) teamMember = TeamManager.get_teamMember_by_teamUserId_userId( teamUser.id, request.user.id) if not teamMember or not teamMember.has_admin_rights(): return _response_not_admin_rights(request) return function(request, *args, **kwargs)
def stats(request, user_name): user = GsuserManager.get_user_by_name(user_name) if user is None: raise Http404 stats_dict = get_stats_dict(request, user) gsuserprofile = GsuserManager.get_userprofile_by_id(user.id) response_dictionary = { 'title': u'%s / 最近统计' % (user.username), 'gsuserprofile': gsuserprofile } response_dictionary.update(stats_dict) return render_to_response('user/stats.html', response_dictionary, context_instance=RequestContext(request))
def get_gitshell_username(name, email): global NAME_CACHE, EMAIL_CACHE if email in EMAIL_CACHE: return EMAIL_CACHE[email] if name in NAME_CACHE: return NAME_CACHE[name] user = GsuserManager.get_user_by_email(email) if user: EMAIL_CACHE[email] = user.username return user.username user = GsuserManager.get_user_by_name(name) if user: NAME_CACHE[name] = user.username return user.username
def get_gitshell_username(name, email): global NAME_CACHE, EMAIL_CACHE if email in EMAIL_CACHE: return EMAIL_CACHE[email] if name in NAME_CACHE: return NAME_CACHE[name] user = GsuserManager.get_user_by_email(email) if user: EMAIL_CACHE[email] = user.username return user.username user = GsuserManager.get_user_by_name(name) if user: NAME_CACHE[name] = user.username return user.username
def active(request, user_name): title = u'%s / 动态' % user_name gsuser = GsuserManager.get_user_by_name(user_name) if gsuser is None: raise Http404 gsuserprofile = GsuserManager.get_userprofile_by_id(gsuser.id) feedAction = FeedAction() pri_user_feeds = feedAction.get_pri_user_feeds(gsuser.id, 0, 50) pub_user_feeds = feedAction.get_pub_user_feeds(gsuser.id, 0, 50) feeds_as_json = get_feeds_as_json(request, pri_user_feeds, pub_user_feeds) response_dictionary = {'mainnav': 'user', 'title': title, 'feeds_as_json': feeds_as_json} response_dictionary.update(get_common_user_dict(request, gsuser, gsuserprofile)) return render_to_response('user/active.html', response_dictionary, context_instance=RequestContext(request))
def find(request): user = None is_user_exist = False username = request.POST.get('username') if username is not None: if username in MAIN_NAVS: is_user_exist = True user = GsuserManager.get_user_by_name(username) email = request.POST.get('email') if email is not None: user = GsuserManager.get_user_by_email(email) if user is not None: is_user_exist = True response_dictionary = {'is_user_exist': is_user_exist} return json_httpResponse(response_dictionary)
def find(request): user = None is_user_exist = False username = request.POST.get('username') if username is not None: if username in MAIN_NAVS: is_user_exist = True user = GsuserManager.get_user_by_name(username) email = request.POST.get('email') if email is not None: user = GsuserManager.get_user_by_email(email) if user is not None: is_user_exist = True response_dictionary = { 'is_user_exist': is_user_exist } return json_httpResponse(response_dictionary)
def get_repo_by_forkrepo(self, username, repo): user = GsuserManager.get_user_by_name(username) if user is None: return None if repo.user_id == user.id: return repo child_repo = self.get_childrepo_by_user_forkrepo(user, repo) if child_repo is not None: return child_repo if repo.fork_repo_id is None: return None parent_repo = RepoManager.get_repo_by_id(repo.fork_repo_id) if parent_repo is None: return None if parent_repo.user_id == user.id: return parent_repo
def group(request, username, group_id): teamUser = GsuserManager.get_user_by_name(username) teamUserprofile = GsuserManager.get_userprofile_by_id(teamUser.id) teamGroup = TeamManager.get_teamGroup_by_id(group_id) if not teamGroup or teamGroup.team_user_id != teamUser.id: raise Http404 groupMembers = TeamManager.list_groupMember_by_teamGroupId(teamGroup.id) userIdInGroupSet = Set([x.member_user_id for x in groupMembers]) teamMembers = TeamManager.list_teamMember_by_teamUserId(teamUser.id) teamMembersNotInGroup = [x for x in teamMembers if x.user_id not in userIdInGroupSet] current = 'settings'; sub_nav = 'groups'; title = u'%s / 设置 / 组管理 / %s' % (teamUser.username, teamGroup.name) response_dictionary = {'current': current, 'sub_nav': sub_nav, 'title': title, 'teamGroup': teamGroup, 'groupMembers': groupMembers, 'teamMembersNotInGroup': teamMembersNotInGroup} response_dictionary.update(_get_common_team_dict(request, teamUser, teamUserprofile)) return render_to_response('team/group.html', response_dictionary, context_instance=RequestContext(request))
def get_repo_by_forkrepo(self, username, repo): user = GsuserManager.get_user_by_name(username) if user is None: return None if repo.user_id == user.id: return repo child_repo = self.get_childrepo_by_user_forkrepo(user, repo) if child_repo is not None: return child_repo if repo.fork_repo_id is None: return None parent_repo = RepoManager.get_repo_by_id(repo.fork_repo_id) if parent_repo is None: return None if parent_repo.user_id == user.id: return parent_repo
def change(request): thirdpartyUser = GsuserManager.get_thirdpartyUser_by_id(request.user.id) user = None is_user_exist = True is_exist_repo = False username = request.POST.get('username') if username is not None and re.match( "^[a-zA-Z0-9_-]+$", username ) and username != request.user.username and username not in MAIN_NAVS and not username.startswith( '-'): repo_count = RepoManager.count_repo_by_userId(request.user.id) if repo_count > 0: return json_httpResponse({'is_exist_repo': True}) user = GsuserManager.get_user_by_name(username) if user is None: request.user.username = username request.userprofile.username = username request.user.save() request.userprofile.save() for repo in RepoManager.list_repo_by_userId( request.user.id, 0, 100): repo.username = username repo.save() is_user_exist = False goto = '' email = request.POST.get('email') if email is not None and email_re.match(email): user = GsuserManager.get_user_by_email(email) if user is None: Mailer().send_change_email(request.user, email) email_suffix = email.split('@')[-1] if email_suffix in COMMON_EMAIL_DOMAIN: goto = COMMON_EMAIL_DOMAIN[email_suffix] is_user_exist = False if thirdpartyUser is not None: thirdpartyUser.init = 1 thirdpartyUser.save() if username == request.user.username: is_user_exist = False response_dictionary = { 'is_exist_repo': is_exist_repo, 'is_user_exist': is_user_exist, 'goto': goto, 'new_username': username, 'new_email': email } return json_httpResponse(response_dictionary)
def star_repo(request, user_name): title = u'%s / 标星的仓库' % user_name gsuser = GsuserManager.get_user_by_name(user_name) if gsuser is None: raise Http404 gsuserprofile = GsuserManager.get_userprofile_by_id(gsuser.id) star_repos = RepoManager.list_star_repo(gsuser.id, 0, 500) response_dictionary = { 'mainnav': 'user', 'title': title, 'star_repos': star_repos } response_dictionary.update( get_common_user_dict(request, gsuser, gsuserprofile)) return render_to_response('user/star_repo.html', response_dictionary, context_instance=RequestContext(request))
def watch_user(request, user_name): title = u'%s / 关注的用户' % user_name gsuser = GsuserManager.get_user_by_name(user_name) if gsuser is None: raise Http404 gsuserprofile = GsuserManager.get_userprofile_by_id(gsuser.id) feedAction = FeedAction() raw_watch_users = feedAction.get_watch_users(gsuser.id, 0, 100) watch_user_ids = [int(x[0]) for x in raw_watch_users] watch_users_map = GsuserManager.map_users(watch_user_ids) watch_users = [ watch_users_map[x] for x in watch_user_ids if x in watch_users_map ] raw_bewatch_users = feedAction.get_bewatch_users(gsuser.id, 0, 100) bewatch_user_ids = [int(x[0]) for x in raw_bewatch_users] bewatch_users_map = GsuserManager.map_users(bewatch_user_ids) bewatch_users = [ bewatch_users_map[x] for x in bewatch_user_ids if x in bewatch_users_map ] # fixed on detect need_fix = False if len(watch_users) != gsuserprofile.watch: gsuserprofile.watch = len(watch_users) need_fix = True if len(bewatch_users) < 100 and len( bewatch_users) != gsuserprofile.be_watched: gsuserprofile.be_watched = len(bewatch_users) need_fix = True if need_fix: gsuserprofile.save() response_dictionary = { 'mainnav': 'user', 'title': title, 'watch_users': watch_users, 'bewatch_users': bewatch_users } response_dictionary.update( get_common_user_dict(request, gsuser, gsuserprofile)) return render_to_response('user/watch_user.html', response_dictionary, context_instance=RequestContext(request))
def groups(request, username): teamUser = GsuserManager.get_user_by_name(username) teamUserprofile = GsuserManager.get_userprofile_by_id(teamUser.id) teamGroups = TeamManager.list_teamGroup_by_teamUserId(teamUser.id) current = 'settings' sub_nav = 'groups' title = u'%s / 设置 / 组管理' % (teamUser.username) response_dictionary = { 'current': current, 'sub_nav': sub_nav, 'title': title, 'teamGroups': teamGroups } response_dictionary.update( _get_common_team_dict(request, teamUser, teamUserprofile)) return render_to_response('team/groups.html', response_dictionary, context_instance=RequestContext(request))
def issues_list(request, user_name, repo_name, assigned, tracker, status, priority, orderby, page): refs = 'master'; path = '.'; current = 'issues'; title = u'%s / %s / 问题列表' % (user_name, repo_name) repo = RepoManager.get_repo_by_name(user_name, repo_name) if repo is None: raise Http404 user_id = request.user.id memberUsers = RepoManager.list_repo_team_memberUser(repo.id) memberUsers = _let_request_user_first(memberUsers, user_id) member_ids = [x.id for x in memberUsers] assigneds = [x.username for x in memberUsers] assigneds.insert(0, '0') if assigned is None: assigned = assigneds[0] assigned_id = 0 assigned_user = GsuserManager.get_user_by_name(assigned) if assigned_user is not None and assigned in assigneds: assigned_id = assigned_user.id tracker = int(tracker); status = int(status); priority = int(priority); page = int(page) current_attrs = { 'assigned': str(assigned), 'tracker': tracker, 'status': status, 'priority': priority, 'orderby': str(orderby), 'page': page } issues = [] page_size = 50; offset = page*page_size; row_count = page_size + 1 if assigned_id == 0 and tracker == 0 and status == 0 and priority == 0: issues = IssueManager.list_issues(repo.id, orderby, offset, row_count) else: assigned_ids = member_ids if assigned_id == 0 else [assigned_id] trackeres = TRACKERS_VAL if tracker == 0 else [tracker] statuses = STATUSES_VAL if status == 0 else [status] priorities = PRIORITIES_VAL if priority == 0 else [priority] issues = IssueManager.list_issues_cons(repo.id, assigned_ids, trackeres, statuses, priorities, orderby, offset, row_count) hasPre = False ; hasNext = False if page > 0: hasPre = True if len(issues) > page_size: hasNext = True issues.pop() response_dictionary = {'mainnav': 'repo', 'current': current, 'title': title, 'path': path, 'assigneds': assigneds, 'assigned': assigned, 'tracker': tracker, 'status': status, 'priority': priority, 'orderby': orderby, 'page': page, 'current_attrs': current_attrs, 'issues': issues, 'hasPre': hasPre, 'hasNext': hasNext} response_dictionary.update(ISSUE_ATTRS) response_dictionary.update(get_common_repo_dict(request, repo, user_name, repo_name, refs)) return render_to_response('repo/issues.html', response_dictionary, context_instance=RequestContext(request))
def stats(request, user_name): title = u'%s / 统计' % user_name gsuser = GsuserManager.get_user_by_name(user_name) if gsuser is None: raise Http404 gsuserprofile = GsuserManager.get_userprofile_by_id(gsuser.id) now = datetime.now() last30days = timeutils.getlast30days(now) last30days_commit = get_last30days_commit(gsuser) response_dictionary = { 'mainnav': 'user', 'title': title, 'last30days': last30days, 'last30days_commit': last30days_commit } response_dictionary.update( get_common_user_dict(request, gsuser, gsuserprofile)) return render_to_response('user/stats.html', response_dictionary, context_instance=RequestContext(request))
def active(request, user_name): title = u'%s / 动态' % user_name gsuser = GsuserManager.get_user_by_name(user_name) if gsuser is None: raise Http404 gsuserprofile = GsuserManager.get_userprofile_by_id(gsuser.id) feedAction = FeedAction() pri_user_feeds = feedAction.get_pri_user_feeds(gsuser.id, 0, 50) pub_user_feeds = feedAction.get_pub_user_feeds(gsuser.id, 0, 50) feeds_as_json = get_feeds_as_json(request, pri_user_feeds, pub_user_feeds) response_dictionary = { 'mainnav': 'user', 'title': title, 'feeds_as_json': feeds_as_json } response_dictionary.update( get_common_user_dict(request, gsuser, gsuserprofile)) return render_to_response('user/active.html', response_dictionary, context_instance=RequestContext(request))
def notif_at(self, notif_type, from_user_id, relative_id, message): at_name_list = FeedUtils.list_atname(message) user_unread_message_dict = {} for at_name in at_name_list: at_user = GsuserManager.get_user_by_name(at_name) if at_user is not None: to_user_id = at_user.id notifMessage = None # disable duplicate notify exists_notifMessage = self.get_notifmessage_by_userId_notifType_relativeId( to_user_id, notif_type, relative_id) if exists_notifMessage is not None: continue if notif_type == NOTIF_TYPE.AT_COMMIT: notifMessage = NotifMessage.create_at_commit( from_user_id, to_user_id, relative_id) elif notif_type == NOTIF_TYPE.AT_MERGE: notifMessage = NotifMessage.create_at_merge( from_user_id, to_user_id, relative_id) elif notif_type == NOTIF_TYPE.AT_ISSUE: notifMessage = NotifMessage.create_at_issue( from_user_id, to_user_id, relative_id) elif notif_type == NOTIF_TYPE.AT_ISSUE_COMMENT: notifMessage = NotifMessage.create_at_issue_comment( from_user_id, to_user_id, relative_id) if notifMessage is None: continue self.message_save_and_notif(notifMessage) if to_user_id not in user_unread_message_dict: user_unread_message_dict[to_user_id] = 0 user_unread_message_dict[ to_user_id] = user_unread_message_dict[to_user_id] + 1 for to_user_id, unread_message in user_unread_message_dict.items(): at_userprofile = GsuserManager.get_userprofile_by_id(to_user_id) at_userprofile.unread_message = at_userprofile.unread_message + unread_message at_userprofile.save()
def watch_repo(request, user_name): title = u'%s / 关注的仓库' % user_name gsuser = GsuserManager.get_user_by_name(user_name) if gsuser is None: raise Http404 gsuserprofile = GsuserManager.get_userprofile_by_id(gsuser.id) feedAction = FeedAction() raw_watch_repos = feedAction.get_watch_repos(gsuser.id, 0, 100) watch_repo_ids = [int(x[0]) for x in raw_watch_repos] watch_repos_map = RepoManager.merge_repo_map_ignore_visibly(watch_repo_ids) watch_repos = [watch_repos_map[x] for x in watch_repo_ids if x in watch_repos_map] response_dictionary = {'mainnav': 'user', 'title': title, 'watch_repos': watch_repos} # fixed on detect if len(watch_repos) != gsuserprofile.watchrepo: gsuserprofile.watchrepo = len(watch_repos) gsuserprofile.save() response_dictionary.update(get_common_user_dict(request, gsuser, gsuserprofile)) return render_to_response('user/watch_repo.html', response_dictionary, context_instance=RequestContext(request))
def recommend(request, user_name): gsuser = GsuserManager.get_user_by_name(user_name) if gsuser is None: raise Http404 if request.method == 'POST': recommendsForm = RecommendsForm(request.POST) if recommendsForm.is_valid() and request.user.is_authenticated(): content = recommendsForm.cleaned_data['content'].strip() if content != '': recommend = Recommend() recommend.user_id = gsuser.id recommend.content = content recommend.from_user_id = request.user.id recommend.save() gsuserprofile = GsuserManager.get_userprofile_by_id(gsuser.id) raw_recommends = GsuserManager.list_recommend_by_userid(gsuser.id, 0, 50) recommends = __conver_to_recommends_vo(raw_recommends) response_dictionary = {'mainnav': 'user', 'recommends': recommends} response_dictionary.update(get_common_user_dict(request, gsuser, gsuserprofile)) return render_to_response('user/recommend.html', response_dictionary, context_instance=RequestContext(request))
def recommend(request, user_name): gsuser = GsuserManager.get_user_by_name(user_name) if gsuser is None: raise Http404 if request.method == 'POST': recommendsForm = RecommendsForm(request.POST) if recommendsForm.is_valid() and request.user.is_authenticated(): content = recommendsForm.cleaned_data['content'].strip() if content != '': recommend = Recommend() recommend.user_id = gsuser.id recommend.content = content recommend.from_user_id = request.user.id recommend.save() gsuserprofile = GsuserManager.get_userprofile_by_id(gsuser.id) raw_recommends = GsuserManager.list_recommend_by_userid(gsuser.id, 0, 50) recommends = __conver_to_recommends_vo(raw_recommends) response_dictionary = {'mainnav': 'user', 'recommends': recommends} response_dictionary.update( get_common_user_dict(request, gsuser, gsuserprofile)) return render_to_response('user/recommend.html', response_dictionary, context_instance=RequestContext(request))
def keyauth(request, fingerprint, command): command = command.strip() last_blank_idx = command.rfind(' ') if last_blank_idx == -1: return not_git_command() pre_command = command[0:last_blank_idx] short_repo_path = command[last_blank_idx + 1:] if pre_command == '' or '"' in pre_command or '\'' in pre_command or short_repo_path == '': return not_git_command() first_repo_char_idx = -1 slash_idx = -1 last_repo_char_idx = -1 for i in range(0, len(short_repo_path)): schar = short_repo_path[i] if first_repo_char_idx == -1 and re.match('\w', schar): first_repo_char_idx = i if schar == '/': slash_idx = i if re.match('[a-zA-Z0-9_\-]', schar): last_repo_char_idx = i if not (first_repo_char_idx > -1 and first_repo_char_idx < slash_idx and slash_idx < last_repo_char_idx): return not_git_command() username = short_repo_path[first_repo_char_idx:slash_idx] reponame = short_repo_path[slash_idx + 1:last_repo_char_idx + 1] if reponame.endswith('.git'): reponame = reponame[0:len(reponame) - 4] if not (re.match('^[a-zA-Z0-9_\-]+$', username) and RepoManager.is_allowed_reponame_pattern(reponame)): return not_git_command() user = GsuserManager.get_user_by_name(username) if user is None: return not_git_command() userprofile = GsuserManager.get_userprofile_by_id(user.id) if userprofile is None: return not_git_command() if userprofile.used_quote > userprofile.quote: return not_git_command() repo = RepoManager.get_repo_by_userId_name(user.id, reponame) if repo is None: return not_git_command() quote = userprofile.quote # author of the repo userPubkey = KeyauthManager.get_userpubkey_by_userId_fingerprint( user.id, fingerprint) if userPubkey is not None: return response_full_git_command(quote, pre_command, user, user, repo) userpubkeys = KeyauthManager.list_userpubkey_by_fingerprint(fingerprint) for userpubkey in userpubkeys: # member of the repo repoMember = RepoManager.get_repo_member(repo.id, userpubkey.user_id) # member of the team user teamMember = TeamManager.get_teamMember_by_teamUserId_userId( user.id, userpubkey.user_id) if repoMember or teamMember: pushUser = GsuserManager.get_user_by_id(userpubkey.user_id) if 'git-receive-pack' in pre_command: if RepoManager.is_allowed_access_repo(repo, pushUser, REPO_PERMISSION.WRITE): return response_full_git_command(quote, pre_command, pushUser, user, repo) elif RepoManager.is_allowed_access_repo(repo, pushUser, REPO_PERMISSION.READ_ONLY): return response_full_git_command(quote, pre_command, pushUser, user, repo) return not_git_command()
def get_repo_by_name(self, user_name, repo_name): user = GsuserManager.get_user_by_name(user_name) if user is None: return None return RepoManager.get_repo_by_userId_name(user.id, repo_name)