-
Notifications
You must be signed in to change notification settings - Fork 31
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
Espace réutilisateur : formulaire pour ajouter des données améliorées #4437
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,3 +54,7 @@ form.search-followed-datasets { | |
.align-right { | ||
text-align: right; | ||
} | ||
|
||
form.full-width { | ||
max-width: 100%; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
defmodule DB.ReuserImprovedData do | ||
@moduledoc """ | ||
Represents improved static data shared by reusers. | ||
""" | ||
use Ecto.Schema | ||
use TypedEctoSchema | ||
import Ecto.Changeset | ||
|
||
typed_schema "reuser_improved_data" do | ||
belongs_to(:dataset, DB.Dataset) | ||
belongs_to(:resource, DB.Resource) | ||
belongs_to(:contact, DB.Contact) | ||
belongs_to(:organization, DB.Organization, type: :string) | ||
field(:download_url, :string) | ||
timestamps(type: :utc_datetime_usec) | ||
end | ||
|
||
def changeset(%__MODULE__{} = struct, attrs \\ %{}) do | ||
fields = [:dataset_id, :resource_id, :contact_id, :organization_id, :download_url] | ||
|
||
struct | ||
|> cast(attrs, fields) | ||
|> validate_required(fields) | ||
|> assoc_constraint(:dataset) | ||
|> assoc_constraint(:resource) | ||
|> assoc_constraint(:contact) | ||
|> assoc_constraint(:organization) | ||
|> unique_constraint([:dataset_id, :organization_id]) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ defmodule TransportWeb.ReuserSpaceController do | |
use TransportWeb, :controller | ||
import Ecto.Query | ||
|
||
plug(:find_dataset_or_redirect when action in [:datasets_edit, :unfavorite]) | ||
plug(:find_dataset_or_redirect when action in [:datasets_edit, :unfavorite, :add_improved_data]) | ||
|
||
def espace_reutilisateur(%Plug.Conn{assigns: %{current_user: %{"id" => datagouv_user_id}}} = conn, _) do | ||
contact = DB.Repo.get_by!(DB.Contact, datagouv_user_id: datagouv_user_id) | ||
|
@@ -14,7 +14,54 @@ defmodule TransportWeb.ReuserSpaceController do | |
|> render("index.html") | ||
end | ||
|
||
def datasets_edit(%Plug.Conn{} = conn, _), do: render(conn, "datasets_edit.html") | ||
def datasets_edit( | ||
%Plug.Conn{assigns: %{dataset: %DB.Dataset{} = dataset, contact: %DB.Contact{} = contact}} = conn, | ||
_ | ||
) do | ||
contact = DB.Repo.preload(contact, :organizations) | ||
eligible_organizations = data_sharing_eligible_orgs(contact) | ||
|
||
conn | ||
|> assign(:contact, contact) | ||
|> assign(:dataset, DB.Repo.preload(dataset, :resources)) | ||
|> assign(:eligible_to_data_sharing_pilot, data_sharing_pilot?(dataset, contact)) | ||
|> assign(:eligible_organizations, eligible_organizations) | ||
|> assign(:existing_improved_data, existing_improved_data(dataset, eligible_organizations)) | ||
|> render("datasets_edit.html") | ||
end | ||
|
||
defp existing_improved_data(%DB.Dataset{id: dataset_id}, [%DB.Organization{id: organization_id}]) do | ||
DB.ReuserImprovedData | ||
|> where([r], r.dataset_id == ^dataset_id and r.organization_id == ^organization_id) | ||
|> DB.Repo.one() | ||
end | ||
Comment on lines
+33
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Une ressource améliorée est déposée au nom de l'organisation éligible au pilote, ce n'est pas scopé par le contact qui a fait l'action (même si le contact est bien enregistré) |
||
|
||
defp existing_improved_data(%DB.Dataset{}, _orgs), do: nil | ||
|
||
def add_improved_data( | ||
%Plug.Conn{ | ||
assigns: %{dataset: %DB.Dataset{} = dataset, contact: %DB.Contact{} = contact}, | ||
params: %{ | ||
"resource_id" => resource_id, | ||
"organization_id" => organization_id, | ||
"download_url" => download_url | ||
} | ||
} = conn, | ||
_ | ||
) do | ||
DB.ReuserImprovedData.changeset(%DB.ReuserImprovedData{}, %{ | ||
dataset_id: dataset.id, | ||
resource_id: resource_id, | ||
contact_id: contact.id, | ||
organization_id: organization_id, | ||
download_url: download_url | ||
}) | ||
|> DB.Repo.insert!() | ||
|
||
conn | ||
|> put_flash(:info, dgettext("reuser-space", "Your improved data has been saved.")) | ||
|> redirect(to: reuser_space_path(conn, :datasets_edit, dataset.id)) | ||
end | ||
|
||
def unfavorite(%Plug.Conn{assigns: %{dataset: %DB.Dataset{} = dataset, contact: %DB.Contact{} = contact}} = conn, _) do | ||
DB.DatasetFollower.unfollow!(contact, dataset) | ||
|
@@ -59,4 +106,29 @@ defmodule TransportWeb.ReuserSpaceController do | |
|> halt() | ||
end | ||
end | ||
|
||
@doc """ | ||
Is the following dataset eligible for the data sharing pilot for this contact, member | ||
of various organizations? | ||
""" | ||
@spec data_sharing_pilot?(DB.Dataset.t(), DB.Contact.t()) :: boolean() | ||
def data_sharing_pilot?(%DB.Dataset{} = dataset, %DB.Contact{} = contact) do | ||
eligible_dataset_type = dataset.type == "public-transit" | ||
has_dataset_tag = DB.Dataset.has_custom_tag?(dataset, config_value(:dataset_custom_tag)) | ||
member_eligible_org = data_sharing_eligible_orgs(contact) |> Enum.count() == 1 | ||
|
||
Enum.all?([eligible_dataset_type, has_dataset_tag, member_eligible_org]) | ||
end | ||
|
||
def data_sharing_eligible_orgs(%DB.Contact{organizations: organizations}) do | ||
data_sharing_eligible_orgs(organizations) | ||
end | ||
|
||
def data_sharing_eligible_orgs(organizations) when is_list(organizations) do | ||
Enum.filter(organizations, &(&1.id in config_value(:eligible_datagouv_organization_ids))) | ||
end | ||
|
||
defp config_value(key) do | ||
Application.fetch_env!(:transport, :"data_sharing_pilot_#{key}") | ||
end | ||
end |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. C'est hors scope mais je corrige la Content Security Policy. En local les images des organisations/utilisateurs n'étaient pas affichés car le domaine |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
defmodule DB.Repo.Migrations.ReuserImprovedData do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:reuser_improved_data) do | ||
add(:dataset_id, references(:dataset, on_delete: :delete_all), null: false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On pourrait retrouver le |
||
add(:resource_id, references(:resource, on_delete: :delete_all), null: false) | ||
add(:contact_id, references(:contact, on_delete: :delete_all), null: false) | ||
add(:organization_id, references(:organization, type: :string, on_delete: :delete_all), null: false) | ||
add(:download_url, :string, null: false) | ||
timestamps(type: :utc_datetime_usec) | ||
end | ||
|
||
create(index(:reuser_improved_data, [:dataset_id])) | ||
create(index(:reuser_improved_data, [:organization_id])) | ||
create(unique_index(:reuser_improved_data, [:dataset_id, :organization_id])) | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Je m'attendais à voir le resource_id membre de la clef d'unicité. Un réutilisateur ne fournit-il pas une version améliorée d'une ressource ?