def add(request, userid, slug): template = "issues/addnew.html" obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) message = None team_members = obj.get('repo').team.all() if request.method == "GET": form = NewIssue() return render_to_response(template, {'repoadmin':obj.get('repo').user,'team':team_members,'activelink':'add', 'href':"/%d/%s/" % (obj.get('user_obj').id, slug), 'form':form}, context_instance=RequestContext(request)) elif request.method == "POST": form = NewIssue(request.POST) if form.is_valid(): issue = form.save(commit=False) issue.author = request.user issue.published = datetime.datetime.now() issue.open = True issue.repository = obj.get('repo') issue.save() message = "Your issue was added." team_members = obj.get('repo').team.all() mess = "User user#%s posted new issue in repo#%s" % (request.user, obj.get('reponame')) title = "New issue in %s" % (obj.get('reponame')) now = datetime.datetime.now() if request.user != obj.get('repo').user: Message.objects.create(title=title, content=mess, datetime=now, user=obj.get('repo').user, read=False) for member in team_members: if request.user != member: Message.objects.create(title=title, content=mess, datetime=now, user=member, read=False) return render_to_response(template, {'message':message,'activelink':'add', 'href':"/%d/%s/" % (obj.get('user_obj').id, slug), 'form':form}, context_instance=RequestContext(request))
def editcomment(request, userid, slug, sha, commentid): template = "repocontrol/editcomment.html" obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) comment = get_object_or_404(CommitComment, pk=commentid) team = obj.get('repo').team.all() if not comment.repository.user == request.user and not request.user in team and not comment.author == request.user: return redirect("/") if request.method == "GET": form = EditComment({'comment': comment.comment}) return render_to_response( template, { 'form': form, 'comment': comment, 'reponame': obj.get('reponame'), 'activelink': 'viewcommits', 'href': "/%d/%s/" % (obj.get('user_obj').id, slug) }, context_instance=RequestContext(request)) elif request.method == "POST": new_comm = request.POST.get('comment', '') if new_comm: comment.comment = new_comm comment.save() return redirect("/%s/%s/commit/%s/" % (userid, slug, comment.sha))
def deletepermission(request, userid, slug): obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) if request.user != obj.get('repo').user: return HttpResponse("Only the owner can delete user permissions") requser = request.GET.get('user', '') if requser == "@all": a = func.delTeamMember(obj.get('reponame'), "@all") if a: func.gitAdd("%s/conf/%s.conf" % (TEMP_REPODIR, obj.get('reponame'))) func.commitChange("Deleted permissions in %s for user %s" % (obj.get('reponame'), "@all")) func.pushUpstream() return redirect("/%s/%s/team/" % (userid, slug)) user_obj = User.objects.get(username=requser) if not requser or not user_obj: return redirect("/%s/%s/team/" % (userid, slug)) if user_obj.username == obj.get('repo').user.username: return HttpResponse("Can't delete owner!") a = func.delTeamMember(obj.get('reponame'), user_obj.username) if a: repo = obj.get('repo') repo.team.remove(user_obj) mess = "User user#%s deleted you as a team member in repo#%s" % (request.user, obj.get('reponame')) title = "Permission changes in %s" % (obj.get('reponame')) now = datetime.datetime.now() Message.objects.create(title=title, content=mess, datetime=now, user=user_obj, read=False) func.gitAdd("%s/conf/%s.conf" % (TEMP_REPODIR, obj.get('reponame'))) func.commitChange("Deleted permissions in %s for user %s" % (obj.get('reponame'), user_obj.username)) func.pushUpstream() return redirect("/%s/%s/team/" % (userid, slug))
def difflist(request, userid, slug): template = "repocontrol/difflist.html" obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) sessname = "%s%s" % (userid, slug) reposess = request.session.get(sessname, None) commitobjects = [] if reposess: for sha in reposess: try: commit = obj.get('repoObj').commit(sha) commitobj = { 'author': smart_str(commit.author), 'date': datetime.datetime.fromtimestamp(commit.committed_date), 'message': smart_str(commit.message), 'tree': commit.tree, 'parents': commit.parents, 'sha': commit.hexsha } except: commitobj = None if commitobj: commitobjects.append(commitobj) return render_to_response( template, { 'commits': commitobjects, 'reponame': obj.get('reponame'), 'activelink': 'difflist', 'href': "/%d/%s/" % (obj.get('user_obj').id, slug) }, context_instance=RequestContext(request))
def committers_history(request, userid, slug): template = "repocontrol/graphs_history_bycommiters.html" obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) branches = func.getBranches(obj.get('repoObj')) return render_to_response(template, {'branches':branches,'reponame':obj.get('reponame') , 'activelink':'graphs', 'href':"/%d/%s/" % (obj.get('user_obj').id, slug)}, context_instance=RequestContext(request))
def myissues(request,userid,slug): template = "issues/active.html" obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) issues = Issue.objects.filter(author=request.user, repository=obj.get('repo')).order_by('-published') return render_to_response(template, {'issues':issues, 'reponame':obj.get('reponame'), 'href':"/%d/%s/" % (obj.get('user_obj').id, slug), 'activelink':'my'}, context_instance=RequestContext(request))
def commit(request, userid, slug, sha): template = "repocontrol/viewcommit.html" obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) form = NewComment() try: commit = obj.get('repoObj').commit(sha) difflist = obj.get('repoObj').git.execute( ["git", "diff-tree", "-p", sha]).splitlines() except: commit = None if not commit: raise Http404 try: comments = CommitComment.objects.filter(repository=obj.get('repo'), sha=sha) except: comments = None difffiles = [] seperate = [] if difflist: del difflist[0] for line in difflist: if re.search("diff --git a/.* b/.*$", line): linespl = line.split(" ") line = linespl[2][2:] if seperate: difffiles.append(seperate) seperate = [] seperate.append(line + "\n") if line == difflist[-1]: difffiles.append(seperate) team = obj.get('repo').team.all() commitobj = { 'author': smart_str(commit.author), 'date': datetime.datetime.fromtimestamp(commit.committed_date), 'message': smart_str(commit.message), 'tree': commit.tree, 'parents': commit.parents, 'sha': commit.hexsha } return render_to_response( template, { 'team': team, 'form': form, 'comments': comments, 'diff': difffiles, 'commit': commitobj, 'reponame': obj.get('reponame'), 'activelink': 'viewcommits', 'href': "/%d/%s/" % (obj.get('user_obj').id, slug) }, context_instance=RequestContext(request))
def details(request, userid, slug, keyid): template = "issues/details.html" obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) issue = get_object_or_404(Issue, pk=keyid) comments = IssueComment.objects.filter(issue=issue) form = NewComment() changelabel = ChangeLabel({'title':issue.title,'label':issue.label, 'deadline':issue.deadline}) team = obj.get('repo').team.all() return render_to_response(template, {'team':team,'changelabel':changelabel,'form':form, 'issue':issue, 'comments':comments,'reponame':obj.get('reponame'), 'activelink':'browse', 'href':"/%d/%s/" % (obj.get('user_obj').id, slug)} , context_instance=RequestContext(request))
def deletecomment(request, userid, slug, sha, commentid): obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) comment = get_object_or_404(CommitComment, pk=commentid) team = obj.get('repo').team.all() if not comment.repository.user == request.user and not request.user in team and not comment.author == request.user: return redirect("/") comment.delete() return redirect("/%s/%s/commit/%s/" % (userid, slug, comment.sha))
def download(request,userid,slug): obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) func.cloneRepo(obj.get('reponame')) func.addToZip(TEMP_CLONE_DIR +"/"+obj.get('repo').slug + ".zip", TEMP_CLONE_DIR +"/"+obj.get('reponame')+"/", obj.get('reponame')) filename = TEMP_CLONE_DIR + "/" + obj.get('repo').slug + ".zip" wrapper = FileWrapper(file(filename)) response = HttpResponse(wrapper, content_type='text/plain') response['Content-Length'] = os.path.getsize(filename) return response
def deleteissue(request,userid,slug,keyid): if request.method == "POST": obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) issue = get_object_or_404(Issue,pk=keyid) team = obj.get('repo').team.all() if not issue.repository.user == request.user and request.user not in team: return redirect("/%s/%s/issues/%s/" %(userid,slug,keyid)) issue.delete() return redirect("/%s/%s/issues/" %(userid,slug))
def graphs(request, userid, slug): template = "repocontrol/graphs.html" obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) return render_to_response( template, { 'reponame': obj.get('reponame'), 'activelink': 'graphs', 'href': "/%d/%s/" % (obj.get('user_obj').id, slug) }, context_instance=RequestContext(request))
def removecomment(request,userid,slug,keyid,commentid): obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) comment = get_object_or_404(IssueComment,pk=commentid) issue = get_object_or_404(Issue, pk=keyid) team = obj.get('repo').team.all() if not issue.repository.user == request.user and request.user not in team and not comment.author == request.user: return redirect("/%s/%s/issues/%s/" %(userid,slug,keyid)) comment.delete() return redirect("/%s/%s/issues/%s/" %(userid,slug,keyid))
def viewfiles(request, userid, slug, branch="master"): template = "repocontrol/viewfiles.html" obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) branches = func.getBranches(obj.get('repoObj')) if branches and branch == "master": branch = branches[0] #teeeeest #if branches: # branch = branches[0] if not branch in branches: raise Http404 return render_to_response(template, {'chosenbranch':branch, 'reponame':obj.get('reponame'), 'branches':branches, 'href':"/%d/%s/" % (obj.get('user_obj').id, slug), 'activelink':'files'}, context_instance=RequestContext(request))
def postcomment(request, userid, slug, sha): if request.method == "POST": obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) form = NewComment(request.POST) if form.is_valid(): object = form.save(commit=False) object.author = request.user object.date = datetime.datetime.now() object.sha = sha object.repository = obj.get('repo') object.save() return redirect("/%s/%s/commit/%s" % (userid, slug, sha))
def download(request, userid, slug): obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) func.cloneRepo(obj.get('reponame')) func.addToZip(TEMP_CLONE_DIR + "/" + obj.get('repo').slug + ".zip", TEMP_CLONE_DIR + "/" + obj.get('reponame') + "/", obj.get('reponame')) filename = TEMP_CLONE_DIR + "/" + obj.get('repo').slug + ".zip" wrapper = FileWrapper(file(filename)) response = HttpResponse(wrapper, content_type='text/plain') response['Content-Length'] = os.path.getsize(filename) return response
def team(request, userid, slug): template = "repocontrol/team.html" obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) team = func.getTeam(obj.get('user_obj').username, slug) repo = obj.get('repo') if request.method == "POST": if request.user != obj.get('repo').user: return HttpResponse("Only the owner can change user permissions") requser = request.POST.get('username', '') reqperm = request.POST.get('perm', '') permlist = {'r':'R', 'rw':'RW', 'rw+':'RW+'} perm = permlist.get(reqperm, '') if requser != "@all": newuser = get_object_or_404(User, username=requser) else: newuser = "******" if perm and newuser: for u in team: if newuser != "@all" and newuser == u.get('user'): return redirect("/%s/%s/team/" % (userid, slug)) elif newuser == "@all" and newuser == u.get('user'): return redirect("/%s/%s/team/" % (userid, slug)) if newuser != "@all": repo.team.add(newuser) repo.save mess = "User user#%s added you as a team member in repo#%s You can now %s" % (request.user, obj.get('reponame'), func.getPermissions(perm)) title = "Permission changes in %s" % (obj.get('reponame')) now = datetime.datetime.now() Message.objects.create(title=title, content=mess, datetime=now, user=newuser, read=False) func.addTeamMember(obj.get('reponame'), newuser.username, perm) file = "%s/conf/%s.conf" % (TEMP_REPODIR, obj.get('reponame')) func.gitAdd(file) func.commitChange("Added %s permission for user %s in %s" % (perm, newuser.username, obj.get('reponame'))) func.pushUpstream() else: func.addTeamMember(obj.get('reponame'), "@all", perm) file = "%s/conf/%s.conf" % (TEMP_REPODIR, obj.get('reponame')) func.gitAdd(file) func.commitChange("Added %s permission for user %s in %s" % (perm, newuser, obj.get('reponame'))) func.pushUpstream() return redirect("/%s/%s/team/" % (userid, slug)) elif request.method == "GET": return render_to_response(template, {'owner':obj.get('repo').user, 'team':team, 'reponame':obj.get('reponame'), 'activelink':'team', 'href':"/%d/%s/" % (obj.get('user_obj').id, slug)} , context_instance=RequestContext(request))
def committers_history(request, userid, slug): template = "repocontrol/graphs_history_bycommiters.html" obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) branches = func.getBranches(obj.get('repoObj')) return render_to_response( template, { 'branches': branches, 'reponame': obj.get('reponame'), 'activelink': 'graphs', 'href': "/%d/%s/" % (obj.get('user_obj').id, slug) }, context_instance=RequestContext(request))
def diff(request, userid, slug, sha1, sha2): template = "repocontrol/diff.html" obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) try: commit1 = obj.get('repoObj').commit(sha1) commit2 = obj.get('repoObj').commit(sha2) except: commit1 = None commit2 = None if not commit1 or not commit2: return redirect("/%s/%s" % (userid, slug)) try: diff = obj.get('repoObj').git.execute(["git", "diff", sha1, sha2]).splitlines() except: diff = None if not diff: return redirect("/%s/%s" % (userid, slug)) difffiles = [] seperate = [] if diff: for line in diff: if re.search("diff --git a/.* b/.*$", line): linespl = line.split(" ") line = linespl[2][2:] if seperate: difffiles.append(seperate) seperate = [] seperate.append(line + "\n") if line == diff[-1]: difffiles.append(seperate) return render_to_response( template, { 'diff': difffiles, 'sha1': sha1, 'sha2': sha2, 'activelink': 'difflist', 'reponame': obj.get('reponame'), 'href': "/%d/%s/" % (obj.get('user_obj').id, slug) }, context_instance=RequestContext(request))
def addfiles(request,userid,slug): template = "repocontrol/addfiles.html" obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) user = User.objects.get(username=request.user) if not user.username == obj.get('user_obj').username: return redirect("/%s/%s" %(userid,slug)) if request.method == "POST": form = FileForm(request.POST, request.FILES) if form.is_valid(): files = request.FILES.getlist('files') updir = UPLOAD_DIR + obj.get('user_obj').username + "/" + slug if not os.path.exists(updir): os.makedirs(updir) func.cloneLocalRepository(obj.get('reponame'), updir) temp_repo = Repo(updir) file_names = [] for f in files: file_names.append(f.name) with open(updir + "/" + f.name, 'wb+') as dest: for chunk in f.chunks(): dest.write(chunk) for name in file_names: temp_repo.git.execute(["git","add",name]) com_author = user.username + " <" + user.email + ">" com_message = "Files " + ', '.join(file_names) + " added" au = "--author="+ "\""+ com_author +"\"" try: temp_repo.git.execute(["git","commit","-m",com_message,au]) command = ["/usr/share/gitolite/gl-admin-push"] temp_repo.git.execute(command) except: return HttpResponse("Cannot commit, push failed!") form = FileForm() message = "Your files has been successfully added" else: message = "Error!" return render_to_response(template, {'message': message,'form':form,'reponame':obj.get('reponame') , 'activelink':'addfiles', 'href':"/%d/%s/" % (obj.get('user_obj').id, slug)}, context_instance=RequestContext(request)) elif request.method == "GET": form = FileForm() return render_to_response(template, {'form':form,'reponame':obj.get('reponame') , 'activelink':'addfiles', 'href':"/%d/%s/" % (obj.get('user_obj').id, slug)}, context_instance=RequestContext(request))
def changestatus(request,userid,slug,keyid): if request.method == "POST": obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) issue = get_object_or_404(Issue, pk=keyid) team = obj.get('repo').team.all() if not issue.repository.user == request.user and not request.user in team: return redirect("/") if issue.open: issue.open = False else: issue.open = True issue.save() return redirect("/%s/%s/issues/%s/" %(userid,slug,keyid)) else: return redirect("/")
def deletepermission(request, userid, slug): obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) if request.user != obj.get('repo').user: return HttpResponse("Only the owner can delete user permissions") requser = request.GET.get('user', '') if requser == "@all": a = func.delTeamMember(obj.get('reponame'), "@all") if a: func.gitAdd("%s/conf/%s.conf" % (TEMP_REPODIR, obj.get('reponame'))) func.commitChange("Deleted permissions in %s for user %s" % (obj.get('reponame'), "@all")) func.pushUpstream() return redirect("/%s/%s/team/" % (userid, slug)) user_obj = User.objects.get(username=requser) if not requser or not user_obj: return redirect("/%s/%s/team/" % (userid, slug)) if user_obj.username == obj.get('repo').user.username: return HttpResponse("Can't delete owner!") a = func.delTeamMember(obj.get('reponame'), user_obj.username) if a: repo = obj.get('repo') repo.team.remove(user_obj) mess = "User user#%s deleted you as a team member in repo#%s" % ( request.user, obj.get('reponame')) title = "Permission changes in %s" % (obj.get('reponame')) now = datetime.datetime.now() Message.objects.create(title=title, content=mess, datetime=now, user=user_obj, read=False) func.gitAdd("%s/conf/%s.conf" % (TEMP_REPODIR, obj.get('reponame'))) func.commitChange("Deleted permissions in %s for user %s" % (obj.get('reponame'), user_obj.username)) func.pushUpstream() return redirect("/%s/%s/team/" % (userid, slug))
def editcomment(request, userid, slug, sha, commentid): template = "repocontrol/editcomment.html" obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) comment = get_object_or_404(CommitComment, pk=commentid) team = obj.get('repo').team.all() if not comment.repository.user == request.user and not request.user in team and not comment.author == request.user: return redirect("/") if request.method == "GET": form = EditComment({'comment':comment.comment}) return render_to_response(template, {'form':form, 'comment':comment, 'reponame':obj.get('reponame'), 'activelink':'viewcommits', 'href':"/%d/%s/" % (obj.get('user_obj').id, slug)} , context_instance=RequestContext(request)) elif request.method == "POST": new_comm = request.POST.get('comment', '') if new_comm: comment.comment = new_comm comment.save() return redirect("/%s/%s/commit/%s/" % (userid, slug, comment.sha))
def difflist(request, userid, slug): template = "repocontrol/difflist.html" obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) sessname = "%s%s" % (userid, slug) reposess = request.session.get(sessname, None) commitobjects = [] if reposess: for sha in reposess: try: commit = obj.get('repoObj').commit(sha) commitobj = {'author':smart_str(commit.author), 'date':datetime.datetime.fromtimestamp(commit.committed_date), 'message': smart_str(commit.message), 'tree':commit.tree, 'parents':commit.parents, 'sha':commit.hexsha} except: commitobj = None if commitobj: commitobjects.append(commitobj) return render_to_response(template, {'commits':commitobjects, 'reponame':obj.get('reponame'), 'activelink':'difflist', 'href':"/%d/%s/" % (obj.get('user_obj').id, slug)}, context_instance=RequestContext(request))
def edit(request,userid,slug,keyid): template = "issues/editissue.html" obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) issue = get_object_or_404(Issue,pk=keyid) team = obj.get('repo').team.all() if request.user == issue.author or request.user == issue.repository.user or request.user in team: if request.method == "GET": form = EditIssue({'content':issue.content}) return render_to_response(template, {'form':form, 'issue':issue,'reponame':obj.get('reponame'), 'activelink':'browse', 'href':"/%d/%s/" % (obj.get('user_obj').id, slug)} , context_instance=RequestContext(request)) elif request.method == "POST": content = request.POST.get('content','') if content: issue.content = content issue.save() return redirect("/%s/%s/issues/%s/" %(userid,slug,keyid)) else: return redirect("/%s/%s/issues/%s/" %(userid,slug,keyid))
def showcommits(request, userid, slug, branch="master"): template = "repocontrol/viewcommits.html" obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) branches = func.getBranches(obj.get('repoObj')) if branches and branch == "master": branch = branches[0] #teeeeest if not branch in branches: raise Http404 page = request.GET.get("page", 1) try: page = int(page) except: page = 1 show_per_page = 8 commits = list(func.getCommits(obj.get('repoObj'), branch, show_per_page, (page * show_per_page) - show_per_page)) nextlist = list(func.getCommits(obj.get('repoObj'), branch, show_per_page, (page * show_per_page))) haspreviouslist = list(func.getCommits(obj.get('repoObj'), branch, show_per_page, (page * show_per_page) - 2 * show_per_page)) hasnext = True hasprevious = True if len(nextlist) == 0: hasnext = False if page == 1 or len(haspreviouslist) == 0: hasprevious = False comlist = [] if commits: for commit in commits: newcom = {'author':smart_str(commit.author), 'message':smart_str(commit.message), 'date':datetime.datetime.fromtimestamp(commit.committed_date), 'sha':commit.hexsha} comlist.append(newcom) return render_to_response(template, {'previouspage':page - 1, 'nextpage':page + 1, 'hasprevious':hasprevious, 'hasnext':hasnext, 'page':page, 'reponame':obj.get('reponame'), 'requestedbranch': branch, 'commits':comlist, 'branches':branches, 'repodb':obj.get('repo'), 'href':"/%d/%s/" % (obj.get('user_obj').id, slug), 'activelink':'viewcommits'}, context_instance=RequestContext(request))
def editcomment(request,userid,slug,keyid,commentid): obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) comment = get_object_or_404(IssueComment,pk=commentid) issue = get_object_or_404(Issue, pk=keyid) team = obj.get('repo').team.all() if not issue.repository.user == request.user and request.user not in team and not comment.author == request.user: return redirect("/%s/%s/issues/%s/" %(userid,slug,keyid)) if request.method == "GET": template = "issues/editcomment.html" form = EditComment({'comment':comment.comment}) return render_to_response(template, {'form':form, 'issue':issue,'comment':comment,'reponame':obj.get('reponame'), 'activelink':'browse', 'href':"/%d/%s/" % (obj.get('user_obj').id, slug)} , context_instance=RequestContext(request)) elif request.method == "POST": new_comm = request.POST.get('comment','') if new_comm: comment.comment = new_comm comment.save() return redirect("/%s/%s/issues/%s/" %(userid,slug,keyid))
def viewfiles(request, userid, slug, branch="master"): template = "repocontrol/viewfiles.html" obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) branches = func.getBranches(obj.get('repoObj')) if branches and branch == "master": branch = branches[0] #teeeeest #if branches: # branch = branches[0] if not branch in branches: raise Http404 return render_to_response( template, { 'chosenbranch': branch, 'reponame': obj.get('reponame'), 'branches': branches, 'href': "/%d/%s/" % (obj.get('user_obj').id, slug), 'activelink': 'files' }, context_instance=RequestContext(request))
def diff(request, userid, slug, sha1, sha2): template = "repocontrol/diff.html" obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) try: commit1 = obj.get('repoObj').commit(sha1) commit2 = obj.get('repoObj').commit(sha2) except: commit1 = None commit2 = None if not commit1 or not commit2: return redirect("/%s/%s" % (userid, slug)) try: diff = obj.get('repoObj').git.execute(["git", "diff", sha1, sha2]).splitlines() except: diff = None if not diff: return redirect("/%s/%s" % (userid, slug)) difffiles = [] seperate = [] if diff: for line in diff: if re.search("diff --git a/.* b/.*$", line): linespl = line.split(" ") line = linespl[2][2:] if seperate: difffiles.append(seperate) seperate = [] seperate.append(line + "\n") if line == diff[-1]: difffiles.append(seperate) return render_to_response(template, {'diff':difffiles, 'sha1':sha1, 'sha2':sha2, 'activelink':'difflist', 'reponame':obj.get('reponame'), 'href':"/%d/%s/" % (obj.get('user_obj').id, slug)}, context_instance=RequestContext(request))
def changelabel(request,userid,slug,keyid): if request.method == "POST": obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) issue = get_object_or_404(Issue,pk=keyid) team = obj.get('repo').team.all() if not issue.repository.user == request.user and request.user not in team: return redirect("/%s/%s/issues/%s/" %(userid,slug,keyid)) labelvalue = request.POST.get('label','') title = request.POST.get('title','') deadline = request.POST.get('deadline','') if labelvalue in dict(Issue.LABELS): issue.label = labelvalue if title: issue.title = title if deadline: issue.deadline = deadline issue.save() return redirect("/%s/%s/issues/%s/" %(userid,slug,keyid))
def postcomment(request,userid,slug,keyid): if request.method == "POST": obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) issue = get_object_or_404(Issue, pk=keyid) form = NewComment(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.author = request.user comment.date = datetime.datetime.now() issue.last_action = datetime.datetime.now() issue.save() comment.issue = issue comment.save() comments = IssueComment.objects.filter(issue=issue) users = [] mess = "User user#%s posted new comment in repo#%s" % (request.user, obj.get('reponame')) title = "New comment on issue %s in %s" % (issue.title,obj.get('reponame')) now = datetime.datetime.now() for comment in comments: if comment.author == request.user: continue if comment.author not in users: users.append(comment.author) Message.objects.create(title=title, content=mess, datetime=now, user=comment.author, read=False) if obj.get('repo').user not in users and obj.get('repo').user != request.user: Message.objects.create(title=title, content=mess, datetime=now, user=obj.get('repo').user, read=False) if issue.author not in users and issue.author != request.user: Message.objects.create(title=title, content=mess, datetime=now, user=issue.author, read=False) return redirect("/%s/%s/issues/%s/" %(userid,slug,keyid)) else: return redirect("/")
def commit(request, userid, slug, sha): template = "repocontrol/viewcommit.html" obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) form = NewComment() try: commit = obj.get('repoObj').commit(sha) difflist = obj.get('repoObj').git.execute(["git", "diff-tree", "-p", sha]).splitlines() except: commit = None if not commit: raise Http404 try: comments = CommitComment.objects.filter(repository=obj.get('repo'), sha=sha) except: comments = None difffiles = [] seperate = [] if difflist: del difflist[0] for line in difflist: if re.search("diff --git a/.* b/.*$", line): linespl = line.split(" ") line = linespl[2][2:] if seperate: difffiles.append(seperate) seperate = [] seperate.append(line + "\n") if line == difflist[-1]: difffiles.append(seperate) team = obj.get('repo').team.all() commitobj = {'author':smart_str(commit.author), 'date':datetime.datetime.fromtimestamp(commit.committed_date), 'message': smart_str(commit.message), 'tree':commit.tree, 'parents':commit.parents, 'sha':commit.hexsha} return render_to_response(template, {'team':team, 'form':form, 'comments':comments, 'diff':difffiles, 'commit':commitobj, 'reponame':obj.get('reponame'), 'activelink':'viewcommits', 'href':"/%d/%s/" % (obj.get('user_obj').id, slug)}, context_instance=RequestContext(request))
def graphs(request, userid, slug, branch, graph, sk=0): obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) branches = func.getBranches(obj.get('repoObj')) dajax = Dajax() if branch not in branches: branch = branches[0] skip = int(sk) num = 50 commits = list(func.getCommits(obj.get('repoObj'), branch, num, skip)) date_val = {} list_values = [] if commits: if graph == "commits": for index, c in enumerate(commits): skip = index if len(date_val) >= 5: break dt = datetime.datetime.fromtimestamp(c.committed_date) temp_val = date_val.get(str(dt.date())) if not temp_val: date_val[str(dt.date())] = 1 else: temp_val += 1 date_val[str(dt.date())] = temp_val elif graph == "committers": first_date = None last_date = None for index, c in enumerate(commits): skip = index dt = datetime.datetime.fromtimestamp(c.committed_date) if index == 0: first_date = dt.date() last_date = first_date - datetime.timedelta(7) if dt.date() >= last_date: temp_val = date_val.get(smart_str(c.author.name)) if not temp_val: date_val[smart_str(c.author.name)] = 1 else: temp_val += 1 date_val[smart_str(c.author.name)] = temp_val else: break for item in date_val: if graph == "commits": tmp_dict = {'date':item, 'value':date_val[item], 'sk':skip} list_values.append(tmp_dict) elif graph == "committers": tmp_dict = {'commiter':item, 'value': date_val[item], 'first':str(first_date), 'last':str(last_date), 'sk':skip} list_values.append(tmp_dict) dajax.add_data(sorted(list_values), "set_graph_data") return dajax.json()
def graphs(request, userid, slug, branch, graph, sk=0): obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) branches = func.getBranches(obj.get('repoObj')) dajax = Dajax() if branch not in branches: branch = branches[0] skip = int(sk) num = 50 commits = list(func.getCommits(obj.get('repoObj'), branch, num, skip)) date_val = {} list_values = [] if commits: if graph == "commits": for index, c in enumerate(commits): skip = index if len(date_val) >= 5: break dt = datetime.datetime.fromtimestamp(c.committed_date) temp_val = date_val.get(str(dt.date())) if not temp_val: date_val[str(dt.date())] = 1 else: temp_val += 1 date_val[str(dt.date())] = temp_val elif graph == "committers": first_date = None last_date = None for index, c in enumerate(commits): skip = index dt = datetime.datetime.fromtimestamp(c.committed_date) if index == 0: first_date = dt.date() last_date = first_date - datetime.timedelta(7) if dt.date() >= last_date: temp_val = date_val.get(smart_str(c.author.name)) if not temp_val: date_val[smart_str(c.author.name)] = 1 else: temp_val += 1 date_val[smart_str(c.author.name)] = temp_val else: break for item in date_val: if graph == "commits": tmp_dict = {'date': item, 'value': date_val[item], 'sk': skip} list_values.append(tmp_dict) elif graph == "committers": tmp_dict = { 'commiter': item, 'value': date_val[item], 'first': str(first_date), 'last': str(last_date), 'sk': skip } list_values.append(tmp_dict) dajax.add_data(sorted(list_values), "set_graph_data") return dajax.json()
def team(request, userid, slug): template = "repocontrol/team.html" obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) team = func.getTeam(obj.get('user_obj').username, slug) repo = obj.get('repo') if request.method == "POST": if request.user != obj.get('repo').user: return HttpResponse("Only the owner can change user permissions") requser = request.POST.get('username', '') reqperm = request.POST.get('perm', '') permlist = {'r': 'R', 'rw': 'RW', 'rw+': 'RW+'} perm = permlist.get(reqperm, '') if requser != "@all": newuser = get_object_or_404(User, username=requser) else: newuser = "******" if perm and newuser: for u in team: if newuser != "@all" and newuser == u.get('user'): return redirect("/%s/%s/team/" % (userid, slug)) elif newuser == "@all" and newuser == u.get('user'): return redirect("/%s/%s/team/" % (userid, slug)) if newuser != "@all": repo.team.add(newuser) repo.save mess = "User user#%s added you as a team member in repo#%s You can now %s" % ( request.user, obj.get('reponame'), func.getPermissions(perm)) title = "Permission changes in %s" % (obj.get('reponame')) now = datetime.datetime.now() Message.objects.create(title=title, content=mess, datetime=now, user=newuser, read=False) func.addTeamMember(obj.get('reponame'), newuser.username, perm) file = "%s/conf/%s.conf" % (TEMP_REPODIR, obj.get('reponame')) func.gitAdd(file) func.commitChange( "Added %s permission for user %s in %s" % (perm, newuser.username, obj.get('reponame'))) func.pushUpstream() else: func.addTeamMember(obj.get('reponame'), "@all", perm) file = "%s/conf/%s.conf" % (TEMP_REPODIR, obj.get('reponame')) func.gitAdd(file) func.commitChange("Added %s permission for user %s in %s" % (perm, newuser, obj.get('reponame'))) func.pushUpstream() return redirect("/%s/%s/team/" % (userid, slug)) elif request.method == "GET": return render_to_response( template, { 'owner': obj.get('repo').user, 'team': team, 'reponame': obj.get('reponame'), 'activelink': 'team', 'href': "/%d/%s/" % (obj.get('user_obj').id, slug) }, context_instance=RequestContext(request))
def all(request, userid, slug): template = "issues/all.html" obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) return render_to_response(template, {'reponame':obj.get('reponame'), 'href':"/%d/%s/" % (obj.get('user_obj').id, slug), 'activelink':'browse'}, context_instance=RequestContext(request))
def addfiles(request, userid, slug): template = "repocontrol/addfiles.html" obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) user = User.objects.get(username=request.user) if not user.username == obj.get('user_obj').username: return redirect("/%s/%s" % (userid, slug)) if request.method == "POST": form = FileForm(request.POST, request.FILES) if form.is_valid(): files = request.FILES.getlist('files') updir = UPLOAD_DIR + obj.get('user_obj').username + "/" + slug if not os.path.exists(updir): os.makedirs(updir) func.cloneLocalRepository(obj.get('reponame'), updir) temp_repo = Repo(updir) file_names = [] for f in files: file_names.append(f.name) with open(updir + "/" + f.name, 'wb+') as dest: for chunk in f.chunks(): dest.write(chunk) for name in file_names: temp_repo.git.execute(["git", "add", name]) com_author = user.username + " <" + user.email + ">" com_message = "Files " + ', '.join(file_names) + " added" au = "--author=" + "\"" + com_author + "\"" try: temp_repo.git.execute(["git", "commit", "-m", com_message, au]) command = ["/usr/share/gitolite/gl-admin-push"] temp_repo.git.execute(command) except: return HttpResponse("Cannot commit, push failed!") form = FileForm() message = "Your files has been successfully added" else: message = "Error!" return render_to_response( template, { 'message': message, 'form': form, 'reponame': obj.get('reponame'), 'activelink': 'addfiles', 'href': "/%d/%s/" % (obj.get('user_obj').id, slug) }, context_instance=RequestContext(request)) elif request.method == "GET": form = FileForm() return render_to_response( template, { 'form': form, 'reponame': obj.get('reponame'), 'activelink': 'addfiles', 'href': "/%d/%s/" % (obj.get('user_obj').id, slug) }, context_instance=RequestContext(request))
def showcommits(request, userid, slug, branch="master"): template = "repocontrol/viewcommits.html" obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) branches = func.getBranches(obj.get('repoObj')) if branches and branch == "master": branch = branches[0] #teeeeest if not branch in branches: raise Http404 page = request.GET.get("page", 1) try: page = int(page) except: page = 1 show_per_page = 8 commits = list( func.getCommits(obj.get('repoObj'), branch, show_per_page, (page * show_per_page) - show_per_page)) nextlist = list( func.getCommits(obj.get('repoObj'), branch, show_per_page, (page * show_per_page))) haspreviouslist = list( func.getCommits(obj.get('repoObj'), branch, show_per_page, (page * show_per_page) - 2 * show_per_page)) hasnext = True hasprevious = True if len(nextlist) == 0: hasnext = False if page == 1 or len(haspreviouslist) == 0: hasprevious = False comlist = [] if commits: for commit in commits: newcom = { 'author': smart_str(commit.author), 'message': smart_str(commit.message), 'date': datetime.datetime.fromtimestamp(commit.committed_date), 'sha': commit.hexsha } comlist.append(newcom) return render_to_response( template, { 'previouspage': page - 1, 'nextpage': page + 1, 'hasprevious': hasprevious, 'hasnext': hasnext, 'page': page, 'reponame': obj.get('reponame'), 'requestedbranch': branch, 'commits': comlist, 'branches': branches, 'repodb': obj.get('repo'), 'href': "/%d/%s/" % (obj.get('user_obj').id, slug), 'activelink': 'viewcommits' }, context_instance=RequestContext(request))
def graphs(request, userid, slug): template = "repocontrol/graphs.html" obj = func.getRepoObjorNone(userid, slug) func.objOr404(obj) return render_to_response(template, {'reponame':obj.get('reponame') , 'activelink':'graphs', 'href':"/%d/%s/" % (obj.get('user_obj').id, slug)}, context_instance=RequestContext(request))