aboutsummaryrefslogtreecommitdiff
path: root/app/views/admin.py
blob: 0e448970595059f63bdf61e6f15970497fecf5b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# Content DB
# Copyright (C) 2018  rubenwardy
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.


from flask import *
from flask_user import *
from flask.ext import menu
from app import app
from app.models import *
from celery import uuid
from app.tasks.importtasks import importRepoScreenshot, importAllDependencies, makeVCSRelease
from app.tasks.forumtasks  import importTopicList
from flask_wtf import FlaskForm
from wtforms import *
from app.utils import loginUser, rank_required, triggerNotif
import datetime
from flask_admin import Admin
from flask_admin.contrib.sqla import ModelView

class MyModelView(ModelView):
	def is_accessible(self):
		return current_user.is_authenticated and current_user.rank.atLeast(UserRank.ADMIN)

	def inaccessible_callback(self, name, **kwargs):
		# redirect to login page if user doesn't have access
		return redirect(url_for('user.login', next=request.url))

admin = Admin(app, name='ContentDB', template_mode='bootstrap3', url="/admin/db")
admin.add_view(MyModelView(User, db.session))
admin.add_view(MyModelView(Package, db.session))
admin.add_view(MyModelView(Dependency, db.session))
admin.add_view(MyModelView(EditRequest, db.session))
admin.add_view(MyModelView(EditRequestChange, db.session))
admin.add_view(MyModelView(ForumTopic, db.session))
admin.add_view(MyModelView(License, db.session))
admin.add_view(MyModelView(MetaPackage, db.session))
admin.add_view(MyModelView(Notification, db.session))
admin.add_view(MyModelView(PackageRelease, db.session))
admin.add_view(MyModelView(PackageScreenshot, db.session))
admin.add_view(MyModelView(Tag, db.session))
admin.add_view(MyModelView(Thread, db.session))
admin.add_view(MyModelView(ThreadReply, db.session))
admin.add_view(MyModelView(UserEmailVerification, db.session))


@app.route("/admin/", methods=["GET", "POST"])
@rank_required(UserRank.ADMIN)
def admin_page():
	if request.method == "POST":
		action = request.form["action"]
		if action == "importmodlist":
			task = importTopicList.delay()
			return redirect(url_for("check_task", id=task.id, r=url_for("todo_topics_page")))
		elif action == "importscreenshots":
			packages = Package.query \
				.filter_by(soft_deleted=False) \
				.outerjoin(PackageScreenshot, Package.id==PackageScreenshot.package_id) \
				.filter(PackageScreenshot.id==None) \
				.all()
			for package in packages:
				importRepoScreenshot.delay(package.id)

			return redirect(url_for("admin_page"))
		elif action == "restore":
			package = Package.query.get(request.form["package"])
			if package is None:
				flash("Unknown package", "error")
			else:
				package.soft_deleted = False
				db.session.commit()
				return redirect(url_for("admin_page"))
		elif action == "importdepends":
			task = importAllDependencies.delay()
			return redirect(url_for("check_task", id=task.id, r=url_for("admin_page")))
		elif action == "modprovides":
			packages = Package.query.filter_by(type=PackageType.MOD).all()
			mpackage_cache = {}
			for p in packages:
				if len(p.provides) == 0:
					p.provides.append(MetaPackage.GetOrCreate(p.name, mpackage_cache))

			db.session.commit()
			return redirect(url_for("admin_page"))
		elif action == "recalcscores":
			for p in Package.query.all():
				p.recalcScore()

			db.session.commit()
			return redirect(url_for("admin_page"))
		elif action == "vcsrelease":
			for package in Package.query.filter(Package.repo.isnot(None)).all():
				if package.releases.count() != 0:
					continue

				rel = PackageRelease()
				rel.package  = package
				rel.title	= datetime.date.today().isoformat()
				rel.url	  = ""
				rel.task_id  = uuid()
				rel.approved = True
				db.session.add(rel)
				db.session.commit()

				makeVCSRelease.apply_async((rel.id, "master"), task_id=rel.task_id)

				msg = "{}: Release {} created".format(package.title, rel.title)
				triggerNotif(package.author, current_user, msg, rel.getEditURL())
				db.session.commit()

		else:
			flash("Unknown action: " + action, "error")

	deleted_packages = Package.query.filter_by(soft_deleted=True).all()
	return render_template("admin/list.html", deleted_packages=deleted_packages)


class SwitchUserForm(FlaskForm):
	username = StringField("Username")
	submit = SubmitField("Switch")


@app.route("/admin/switchuser/", methods=["GET", "POST"])
@rank_required(UserRank.ADMIN)
def switch_user_page():
	form = SwitchUserForm(formdata=request.form)
	if request.method == "POST" and form.validate():
		user = User.query.filter_by(username=form["username"].data).first()
		if user is None:
			flash("Unable to find user", "error")
		elif loginUser(user):
			return redirect(url_for("user_profile_page", username=current_user.username))
		else:
			flash("Unable to login as user", "error")


	# Process GET or invalid POST
	return render_template("admin/switch_user_page.html", form=form)