Skip to content

Commit

Permalink
Merge pull request #463 from WikipediaLibrary/staging
Browse files Browse the repository at this point in the history
Deploy library bundle
  • Loading branch information
jsnshrmn authored Jun 8, 2020
2 parents 846eeb7 + 6281683 commit 8d6a72c
Show file tree
Hide file tree
Showing 123 changed files with 52,223 additions and 18,134 deletions.
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
repos:
- repo: https://github.com/ambv/black
rev: 19.3b0
hooks:
- id: black
language_version: python3.7
4 changes: 2 additions & 2 deletions TWLight/applications/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,14 @@ def _add_partner_data_subform(self, partner):
partner_id = int(partner[8:])
# We use the logic below to filter out the streams for which
# the user already has authorizations. Streams with authorizations
# can only be renewed (as opposed to applying) from the My Collection
# can only be renewed (as opposed to applying) from the My Library
# page.
queryset = Stream.objects.filter(partner_id=partner_id)
# We need a user if we are to determine which streams have authorizations.
# We set the user in the view code if a partner has streams.
if self.user:
all_authorizations = Authorization.objects.filter(
user=self.user, partner=partner_id, stream__isnull=False
user=self.user, partners=partner_id, stream__isnull=False
)
existing_streams = []
for each_authorization in all_authorizations:
Expand Down
42 changes: 4 additions & 38 deletions TWLight/applications/helpers.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import datetime

from django import forms
from django.db.models import Q
from django.utils.translation import ugettext as _

from TWLight.resources.models import Partner, Stream
from TWLight.users.models import Authorization

from .models import Application
from ..users.helpers.authorizations import get_valid_partner_authorizations

"""
Lists and characterizes the types of information that partners can require as
Expand Down Expand Up @@ -172,46 +169,15 @@ def get_output_for_application(app):
return output


def get_valid_authorizations(partner_pk, stream_pk=None):
"""
Retrieves the valid authorizations available for a particular
partner (or collections if stream_pk is not None). Valid authorizations are
authorizations with which we can operate, and is decided by certain conditions as
spelled out in the is_valid property of the Authorization model object (users/models.py).
"""
today = datetime.date.today()
try:
# The filter below is equivalent to retrieving all authorizations for a partner
# and (or) stream and checking every authorization against the is_valid property
# of the authorization model, and hence *must* be kept in sync with the logic in
# TWLight.users.model.Authorization.is_valid property. We don't need to check for
# partner_id__isnull since it is functionally covered by partner=partner_pk.
valid_authorizations = Authorization.objects.filter(
Q(date_expires__isnull=False, date_expires__gte=today)
| Q(date_expires__isnull=True),
authorizer__isnull=False,
user__isnull=False,
date_authorized__isnull=False,
date_authorized__lte=today,
partner=partner_pk,
)
if stream_pk:
valid_authorizations = valid_authorizations.filter(stream=stream_pk)

return valid_authorizations
except Authorization.DoesNotExist:
return Authorization.objects.none()


def count_valid_authorizations(partner_pk, stream_pk=None):
"""
Retrieves the numbers of valid authorizations using the
get_valid_authorizations() method above.
get_valid_partner_authorizations() method above.
"""
if stream_pk:
return get_valid_authorizations(partner_pk, stream_pk).count()
return get_valid_partner_authorizations(partner_pk, stream_pk).count()
else:
return get_valid_authorizations(partner_pk).count()
return get_valid_partner_authorizations(partner_pk).count()


def get_accounts_available(app):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@

logger = logging.getLogger(__name__)

twl_team, created = User.objects.get_or_create(
username="TWL Team", email="[email protected]"
)
twl_team = User.objects.get(username="TWL Team")


class Command(BaseCommand):
Expand Down
154 changes: 84 additions & 70 deletions TWLight/applications/management/commands/applications_example_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
from TWLight.applications.models import Application
from TWLight.applications.views import SendReadyApplicationsView
from TWLight.resources.models import Partner, Stream, AccessCode
from TWLight.users.models import Editor

twl_team = User.objects.get(username="TWL Team")


def logged_in_example_coordinator(client, coordinator):
Expand Down Expand Up @@ -47,7 +50,7 @@ def handle(self, *args, **options):

available_partners = Partner.objects.all()
# Don't fire any applications from the superuser.
all_users = User.objects.exclude(is_superuser=True)
all_editors = Editor.objects.exclude(user__is_superuser=True)

import_date = datetime.datetime(2017, 7, 17, 0, 0, 0)

Expand All @@ -62,82 +65,90 @@ def handle(self, *args, **options):
]

for _ in range(num_applications):
random_user = random.choice(all_users)
random_editor = random.choice(all_editors)
# Limit to partners this user hasn't already applied to.
not_applied_partners = available_partners.exclude(
applications__editor=random_user.editor
)
random_partner = random.choice(not_applied_partners)

app = ApplicationFactory(
editor=random_user.editor,
partner=random_partner,
hidden=self.chance(True, False, 10),
applications__editor=random_editor
)

# Make sure partner-specific information is filled.
if random_partner.specific_stream:
app.specific_stream = random.choice(
Stream.objects.filter(partner=random_partner)
if not_applied_partners:
random_partner = random.choice(not_applied_partners)
app = ApplicationFactory(
editor=random_editor,
partner=random_partner,
hidden=self.chance(True, False, 10),
)

if random_partner.specific_title:
app.specific_title = Faker(
random.choice(settings.FAKER_LOCALES)
).sentence(nb_words=3)

if random_partner.agreement_with_terms_of_use:
app.agreement_with_terms_of_use = True

if random_partner.account_email:
app.account_email = Faker(random.choice(settings.FAKER_LOCALES)).email()

# Imported applications have very specific information, and were
# all imported on the same date.
imported = self.chance(True, False, 50)

if imported:
app.status = Application.SENT
app.date_created = import_date
app.date_closed = import_date
app.rationale = "Imported on 2017-07-17"
app.comments = "Imported on 2017-07-17"
app.imported = True
else:
app.status = random.choice(valid_choices)

# Figure out earliest valid date for this app
if random_user.editor.wp_registered < import_date.date():
start_date = import_date
else:
start_date = random_user.editor.wp_registered
# Make sure partner-specific information is filled.
if random_partner.specific_stream:
app.specific_stream = random.choice(
Stream.objects.filter(partner=random_partner)
)

app.date_created = Faker(
random.choice(settings.FAKER_LOCALES)
).date_time_between(start_date=start_date, end_date="now", tzinfo=None)
app.rationale = Faker(random.choice(settings.FAKER_LOCALES)).paragraph(
nb_sentences=3
)
app.comments = Faker(random.choice(settings.FAKER_LOCALES)).paragraph(
nb_sentences=2
)
if random_partner.specific_title:
app.specific_title = Faker(
random.choice(settings.FAKER_LOCALES)
).sentence(nb_words=3)

if random_partner.agreement_with_terms_of_use:
app.agreement_with_terms_of_use = True

if random_partner.account_email:
app.account_email = Faker(
random.choice(settings.FAKER_LOCALES)
).email()

# Imported applications have very specific information, and were
# all imported on the same date.
imported = self.chance(True, False, 50)

if imported:
app.status = Application.SENT
app.date_created = import_date
app.date_closed = import_date
app.rationale = "Imported on 2017-07-17"
app.comments = "Imported on 2017-07-17"
app.imported = True
else:
app.status = random.choice(valid_choices)

# For closed applications, assign date_closed and date_open
if app.status in Application.FINAL_STATUS_LIST:
if not imported:
potential_end_date = app.date_created + relativedelta(years=1)
if potential_end_date > datetime.datetime.now():
end_date = "now"
# Figure out earliest valid date for this app
if random_editor.wp_registered < import_date.date():
start_date = import_date
else:
end_date = potential_end_date
app.date_closed = Faker(
start_date = random_editor.wp_registered

app.date_created = Faker(
random.choice(settings.FAKER_LOCALES)
).date_time_between(
start_date=app.date_created, end_date=end_date, tzinfo=None
start_date=start_date, end_date="now", tzinfo=None
)
app.days_open = (app.date_closed - app.date_created).days

app.save()
app.rationale = Faker(
random.choice(settings.FAKER_LOCALES)
).paragraph(nb_sentences=3)
app.comments = Faker(
random.choice(settings.FAKER_LOCALES)
).paragraph(nb_sentences=2)

# For closed applications, assign date_closed and date_open
if app.status in Application.FINAL_STATUS_LIST:
if not imported:
potential_end_date = app.date_created + relativedelta(years=1)
if potential_end_date > datetime.datetime.now():
end_date = "now"
else:
end_date = potential_end_date
app.date_closed = Faker(
random.choice(settings.FAKER_LOCALES)
).date_time_between(
start_date=app.date_created, end_date=end_date, tzinfo=None
)
app.days_open = (app.date_closed - app.date_created).days
# Make sure we always set sent_by
if app.status == Application.SENT and not app.sent_by:
app.sent_by = twl_team

app.save()

# Let's mark all Approved applications that were approved more
# than 3 weeks ago as Sent.
Expand All @@ -154,22 +165,25 @@ def handle(self, *args, **options):
coordinator = logged_in_example_coordinator(
client, approved_app.partner.coordinator
)
this_partner_access_codes = AccessCode.objects.filter(
partner=approved_app.partner, authorization__isnull=True
)

url = reverse(
"applications:send_partner", kwargs={"pk": approved_app.partner.pk}
)

this_partner_access_codes = AccessCode.objects.filter(
partner=approved_app.partner, authorization__isnull=True
)

if approved_app.partner.authorization_method == Partner.EMAIL:
request = RequestFactory().post(
url, data={"applications": [approved_app.pk]}
)

# If this partner has access codes, assign a code to
# this sent application.
elif approved_app.partner.authorization_method == Partner.CODES:
elif (
this_partner_access_codes
and approved_app.partner.authorization_method == Partner.CODES
):
access_code = random.choice(this_partner_access_codes)
request = RequestFactory().post(
url,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@

from datetime import timedelta

from django.conf import settings
from django.contrib.auth.models import User
from django.core.management.base import BaseCommand, CommandError
from django.db.models import Q
from django.utils import timezone
from django.utils.translation import ugettext as _
from TWLight.helpers import site_id
from TWLight.applications.models import Application
from TWLight.resources.models import Partner
from django_comments.models import Comment

logger = logging.getLogger(__name__)

twl_team, created = User.objects.get_or_create(
username="TWL Team", email="[email protected]"
)
twl_team = User.objects.get(username="TWL Team")


class Command(BaseCommand):
Expand All @@ -39,7 +37,7 @@ def handle(self, *args, **options):
for app in pending_apps:
if (
Comment.objects.filter(
Q(object_pk=str(app.pk), site_id=settings.SITE_ID),
Q(object_pk=str(app.pk), site_id=site_id()),
(
Q(user=twl_team)
| Q(submit_date__gte=(timezone.now() - timedelta(days=8)))
Expand All @@ -49,7 +47,7 @@ def handle(self, *args, **options):
):
comment = Comment(
content_object=app,
site_id=settings.SITE_ID,
site_id=site_id(),
user=twl_team,
# Translators: This comment is added to pending applications when our terms of use change.
comment=_(
Expand Down
4 changes: 2 additions & 2 deletions TWLight/applications/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,13 +343,13 @@ def get_authorization(self):
try:
if self.specific_stream:
authorization = Authorization.objects.get(
partner=self.partner,
partners=self.partner,
user=self.editor.user,
stream=self.specific_stream,
)
else:
authorization = Authorization.objects.get(
partner=self.partner, user=self.editor.user
partners=self.partner, user=self.editor.user
)
except Authorization.DoesNotExist:
return None
Expand Down
Loading

0 comments on commit 8d6a72c

Please sign in to comment.