Skip to content

Add Release.uploader as a real FK #5015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion warehouse/admin/templates/admin/projects/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ <h4>Releases:</h4>
<tr>
<td><a href="{{ request.route_path('admin.project.release', project_name=release.project.normalized_name, version=release.version) }}">{{ release.name }}-{{ release.version }}</a></td>
<td>{{ release.created }}</td>
<td><a href="{{ request.route_path('admin.user.detail', user_id=release.uploader.id) }}">{{ release.uploader.username }}</a></td>
<td>
{% if release.uploader %}
<a href="{{ request.route_path('admin.user.detail', user_id=release.uploader.id) }}">{{ release.uploader.username }}</a>
{% endif %}
</td>
<td>{{ release.author_email }}</td>
</tr>
{% endfor %}
Expand Down
6 changes: 5 additions & 1 deletion warehouse/admin/templates/admin/projects/release_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ <h3>Details</h3>
</tr>
<tr>
<td>Uploader</td>
<td><a href="{{ request.route_path('admin.user.detail', user_id=release.uploader.id) }}">{{ release.uploader.username }}</a></td>
<td>
{% if release.uploader %}
<a href="{{ request.route_path('admin.user.detail', user_id=release.uploader.id) }}">{{ release.uploader.username }}</a>
{% endif %}
</td>
</tr>
<tr>
<td>Created via</td>
Expand Down
6 changes: 5 additions & 1 deletion warehouse/admin/templates/admin/projects/releases_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@
<tr>
<td><a href="{{ request.route_path('admin.project.release', project_name=release.project.normalized_name, version=release.version) }}">{{ release.name }}-{{ release.version }}</a></td>
<td>{{ release.created }}</td>
<td><a href="{{ request.route_path('admin.user.detail', user_id=release.uploader.id) }}">{{ release.uploader.username }}</a></td>
<td>
{% if release.uploader %}
<a href="{{ request.route_path('admin.user.detail', user_id=release.uploader.id) }}">{{ release.uploader.username }}</a>
{% endif %}
</td>
<td>{{ release.author_email }}</td>
</tr>
{% endfor %}
Expand Down
1 change: 1 addition & 0 deletions warehouse/forklift/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,7 @@ def file_upload(request):
"requires_python",
}
},
uploader=request.user,
uploaded_via=request.user_agent,
)
request.db.add(release)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Add uploader field to Release

Revision ID: e612a92c1017
Revises: 5538f2d929dd
Create Date: 2018-11-06 16:22:01.484362
"""

from alembic import op
import sqlalchemy as sa

from sqlalchemy.dialects import postgresql


revision = "e612a92c1017"
down_revision = "5538f2d929dd"


def upgrade():
op.add_column(
"releases",
sa.Column("uploader_id", postgresql.UUID(as_uuid=True), nullable=True),
)
op.execute(
"""
UPDATE releases
SET uploader_id = s.user_id
FROM (
SELECT accounts_user.id as user_id,
packages.id as project_id,
releases.version as release_version,
ROW_NUMBER() OVER (
PARTITION BY journals.name, journals.version
ORDER BY journals.id DESC
) as rn
FROM accounts_user, packages, journals, releases
WHERE journals.name = packages.name
AND journals.version = releases.version
AND journals.action = 'new release'
AND accounts_user.username = journals.submitted_by
) s
WHERE releases.project_id = s.project_id
AND releases.version = s.release_version
AND s.rn = 1
"""
)
op.create_foreign_key(
None,
"releases",
"accounts_user",
["uploader_id"],
["id"],
onupdate="CASCADE",
ondelete="SET NULL",
)
op.create_index("ix_releases_uploader_id", "releases", ["uploader_id"])


def downgrade():
raise RuntimeError("Order No. 227 - Ни шагу назад!")
26 changes: 5 additions & 21 deletions warehouse/packaging/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,28 +348,12 @@ def __table_args__(cls): # noqa
_project_urls = _dependency_relation(DependencyKind.project_url)
project_urls = association_proxy("_project_urls", "specifier")

uploader = orm.relationship(
"User",
secondary=lambda: JournalEntry.__table__,
primaryjoin=lambda: (
(JournalEntry.name == orm.foreign(Project.name))
& (Project.id == Release.project_id)
& (JournalEntry.version == orm.foreign(Release.version))
& (JournalEntry.action == "new release")
),
secondaryjoin=lambda: (
(User.username == orm.foreign(JournalEntry._submitted_by))
),
order_by=lambda: JournalEntry.id.desc(),
# TODO: We have uselist=False here which raises a warning because
# multiple items were returned. This should only be temporary because
# we should add a nullable FK to JournalEntry so we don't need to rely
# on ordering and implicitly selecting the first object to make this
# happen,
uselist=False,
viewonly=True,
uploader_id = Column(
ForeignKey("accounts_user.id", onupdate="CASCADE", ondelete="SET NULL"),
nullable=True,
index=True,
)

uploader = orm.relationship(User)
uploaded_via = Column(Text)

@property
Expand Down