#StackBounty: #python #django #django-rest-framework Django Rest Framework – passing Model data through a function, then posting output…

Bounty: 50

(Django 2.0, Python 3.6, Django Rest Framework 3.8)

I’m trying to fill the calendarydays field in the model below:

Model

class Bookings(models.Model):
    booked_trainer = models.ForeignKey(TrainerProfile, on_delete=models.CASCADE)
    booked_client = models.ForeignKey(ClientProfile, on_delete=models.CASCADE)
    trainer_availability_only = models.ForeignKey(Availability, on_delete=models.CASCADE)
    calendarydays = models.CharField(max_length=300, blank=True, null=True)

    PENDING = 'PENDING'
    CONFIRMED = 'CONFIRMED'
    CANCELED = 'CANCELED'

    STATUS_CHOICES = (
        (PENDING, 'Pending'),
        (CONFIRMED, 'Confirmed'),
        (CANCELED, 'Canceled')
    )


    booked_status = models.CharField(
        max_length = 9,
        choices = STATUS_CHOICES,
        default = 'Pending'
    )

    def __str__(self):
        return str(self.trainer_availability_only)

Now, I have a function that takes values from trainer_availability_only and converts those values to a list of datetime strings, the returned output would look like this:

{‘calendarydays’: [‘2018-07-23 01:00:00’, ‘2018-07-23 02:00:00’,
‘2018-07-23 03:00:00’, ‘2018-07-30 01:00:00’, ‘2018-07-30 02:00:00’,
‘2018-07-30 03:00:00’, ‘2018-08-06 01:00:00’, ‘2018-08-06 02:00:00’,
‘2018-08-06 03:00:00’, ‘2018-08-13 01:00:00’, ‘2018-08-13 02:00:00’,
‘2018-08-13 03:00:00’, ‘2018-08-20 01:00:00’, ‘2018-08-20 02:00:00’,
‘2018-08-20 03:00:00’]}

Problem

How can I fill the calendarydays field with the function output for a user to select from a dropdown, and where should I implement this logic (in my view or the serializer)? My main point of confusion is that, because my function depends on data from trainer_availability_only, I don’t want to create a separate model/table for this information (as that would seem too repetitive). I also don’t fully understand where in my serializers or views I can implement some sort of dropdown for a User to choose a single calendarydays value for (like I would be able to for a ForeignKey or OneToOneField for example).

Details for the other models aren’t really relevant to the question, except trainer_availability_only, which basically gives the user a dropdown selection that would look like this:

('Monday','12:00 am - 1:00 am')
('Wednesday','4:00 pm - 5:00 pm')
etc.

Any help is greatly appreciated.


Get this bounty!!!

#StackBounty: #python #django #amazon-web-services #deployment #elastic-beanstalk AWS Elastic Beanstalk health check issue

Bounty: 50

First I apologize for not being familiar with English.

My web application is Django and web server use Nginx, use Docker image and Elastic Beanstalk to deployment.

Normally there was no problem, but as the load balancer expands EC2, my web server becomes 502 Bad Gateway.

I’m checked Elastic Beanstalk application logs, about 16% of the requests returned 5xx errors, at which time the load balancer expands EC2, causing the web server to transition to the 502 Bad Gateway state and the Elastic Beanstalk application to the Degraded state.

Is this a common problem when the load balancer performs a health check? If not, can I tell you how to turn off the Health Check?

I will attach a capture image for your help.

enter image description here


Get this bounty!!!

#StackBounty: #python #django #django-models #django-1.8 #django-1.9 Using a model's sub-classes as choice options for that model r…

Bounty: 100

We are trying to upgrade a legacy code’s django version from 1.8 to 1.9. We have one model that is defined like this:

def _get_descendant_question_classes():
    stack = [Question]

    while stack:
        cls = stack.pop()
        stack.extend(cls.__subclasses__())
        yield cls

def _get_question_choices():
    question_classes = _get_descendant_question_classes()

    for cls in question_classes:
        yield (cls.slug, cls._meta.verbose_name)

class Question(models.Model):
    type = models.CharField(max_length=10, choices=_get_question_choices(), default=slug)

class TextQuestion(Question): pass
class SelectQuestion(Question): pass
...

Basically the model wants to use its sub-classes as choice options for one of its fields. It does this with traversing the model in a DFS manner and yielding all the sub-classes.

This code works in django 1.8 but in django 1.9 it gives this error:

Traceback (most recent call last):
  File "./manage.py", line 16, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 324, in execute
    django.setup()
  File "/usr/local/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/usr/local/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
    app_config.import_models(all_models)
  File "/usr/local/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/local/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/home/saeed/saeed/survey/models.py", line 85, in <module>
    class Question(models.Model):
  File "/home/saeed/saeed/survey/models.py", line 99, in Question
    type = models.CharField(max_length=10, choices=_get_question_choices(), default=slug)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 1072, in __init__
    super(CharField, self).__init__(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 161, in __init__
    choices = list(choices)
  File "/home/saeed/saeed/survey/models.py", line 65, in _get_question_choices
    for cls in question_classes:
  File "/home/saeed/saeed/survey/models.py", line 54, in _get_descendant_question_classes
    stack = [Question]
NameError: global name 'Question' is not defined

I understand the problem what I don’t understand is how this works in django 1.8? What has changed in django 1.9 that causes this? What is the best way to fix this?


Get this bounty!!!

#StackBounty: #python #django #django-i18n "django-admin.py makemessages -l en" adds Plural-Forms to the output file

Bounty: 50

Every time I run django-admin.py makemessages -l en, it adds the following line to the djangojs.po file:

"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;n"

after that running python manage.py runserver break out whith this error:

ValueError: plural forms expression could be dangerous

Of course removing that line fix the error and make it go away.
How can I prevent this line from being added?


Get this bounty!!!

#StackBounty: #python #django #python-3.x #django-models Django 2.0 Access Models (CREATE/REMOVE/FILTER) Standalone [without manage.py …

Bounty: 50

I have a Django project and I wanted to generate some objects (from the models)

What I’m trying to get at : Standalone Python Script to create bunch of objects and/or filter,delete.

after importing the model with from apps.base.models import MyModel
and setting up the configuration as the previous StackOverflow Questions suggested I was not able to run the script.

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myProject.settings")
import django

django.setup()
from apps.base.models import MyModel

Please note that this is on Django version 2.0.6 [Django 2.0+].

Correct settings have been used, (i.e. myProject.settings)

  • After properly configuring everything else I get the following error:
    • RuntimeError: Model class apps.base.models.MyModel doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.


Get this bounty!!!

#StackBounty: #apache-2.4 #django Apache: LocationMatch or ProxyMatch doesn't work with RegEx

Bounty: 50

I want to redirect requests for a exact specific location to proxy server but both solutions doesn’t work:

<VirtualHost *:80>
  ServerName exmaple.com

<LocationMatch "^/test01$">
  ProxyPreserveHost On
  ProxyPass http://localhost:8000/
  ProxyPassReverse http://localhost:8000/
</LocationMatch>

<ProxyMatch "^/test02$">
  ProxyPreserveHost On
  ProxyPass http://localhost:8000/
  ProxyPassReverse http://localhost:8000/
</ProxyMatch>

</VirtualHost>

Test:

$ curl -I exmaple.com/test01
HTTP/1.1 404 Not Found
Date: Sun, 17 Jun 2018 15:37:10 GMT
Server: Apache
Content-Type: text/html; charset=iso-8859-1

$ curl -I exmaple.com/test02
HTTP/1.1 404 Not Found
Date: Sun, 17 Jun 2018 15:37:13 GMT
Server: Apache
Content-Type: text/html; charset=iso-8859-1

When I remove the RegEx part then it works but I want Apache to display the 404, not the Django development server:

<LocationMatch "/test01">
  ProxyPreserveHost On
  ProxyPass http://localhost:8000/
  ProxyPassReverse http://localhost:8000/
</LocationMatch>

$ curl -I exmaple.com/test01
HTTP/1.1 200 OK
Date: Sun, 17 Jun 2018 15:42:26 GMT
Server: WSGIServer/0.2 CPython/3.6.5
Content-Type: text/html; charset=utf-8
X-Frame-Options: SAMEORIGIN
Content-Length: 12767

$ curl -I exmaple.com/test01/none
HTTP/1.1 404 Not Found
Date: Sun, 17 Jun 2018 15:42:33 GMT
Server: WSGIServer/0.2 CPython/3.6.5
Content-Type: text/html; charset=UTF-8
X-Frame-Options: SAMEORIGIN
Content-Length: 2073

Update: Mon 18 Jun 14:40:59 UTC 2018

So I’ve done some further research and according to Apache docs:

When used inside a <Location> section, the first argument is omitted
and the local directory is obtained from the <Location>. The same will
occur inside a <LocationMatch> section; however, ProxyPass does not
interpret the regexp as such, so it is necessary to use ProxyPassMatch
in this situation instead.

So I tried ProxyPassMatch and it works however the RegEx matched is passed to Proxy http://localhost:8000/test01 and this is not what I want.

I can achieve this easily with Nginx:

server {
    listen 80;
    server_name exmaple.com;

    location = /test01/ {
        proxy_pass http://localhost:8000/;
        proxy_set_header Host $host;
    }
}

How I can do something similar with Apache?

Update: Sun 24 Jun 10:46:12 UTC 2018

<LocationMatch "^/test01$">
  Redirect / http://test.com/
</LocationMatch>

$ curl -I exmaple.com/test01
HTTP/1.1 302 Found
Date: Sun, 24 Jun 2018 10:47:04 GMT
Server: Apache
Location: http://test.com/test01
Content-Type: text/html; charset=iso-8859-1


Get this bounty!!!

#StackBounty: #apache-2.4 #django Apache: LocationMatch or ProxyMatch doesn't work with RegEx

Bounty: 50

I want to redirect requests for a exact specific location to proxy server but both solutions doesn’t work:

<VirtualHost *:80>
  ServerName exmaple.com

<LocationMatch "^/test01$">
  ProxyPreserveHost On
  ProxyPass http://localhost:8000/
  ProxyPassReverse http://localhost:8000/
</LocationMatch>

<ProxyMatch "^/test02$">
  ProxyPreserveHost On
  ProxyPass http://localhost:8000/
  ProxyPassReverse http://localhost:8000/
</ProxyMatch>

</VirtualHost>

Test:

$ curl -I exmaple.com/test01
HTTP/1.1 404 Not Found
Date: Sun, 17 Jun 2018 15:37:10 GMT
Server: Apache
Content-Type: text/html; charset=iso-8859-1

$ curl -I exmaple.com/test02
HTTP/1.1 404 Not Found
Date: Sun, 17 Jun 2018 15:37:13 GMT
Server: Apache
Content-Type: text/html; charset=iso-8859-1

When I remove the RegEx part then it works but I want Apache to display the 404, not the Django development server:

<LocationMatch "/test01">
  ProxyPreserveHost On
  ProxyPass http://localhost:8000/
  ProxyPassReverse http://localhost:8000/
</LocationMatch>

$ curl -I exmaple.com/test01
HTTP/1.1 200 OK
Date: Sun, 17 Jun 2018 15:42:26 GMT
Server: WSGIServer/0.2 CPython/3.6.5
Content-Type: text/html; charset=utf-8
X-Frame-Options: SAMEORIGIN
Content-Length: 12767

$ curl -I exmaple.com/test01/none
HTTP/1.1 404 Not Found
Date: Sun, 17 Jun 2018 15:42:33 GMT
Server: WSGIServer/0.2 CPython/3.6.5
Content-Type: text/html; charset=UTF-8
X-Frame-Options: SAMEORIGIN
Content-Length: 2073

Update: Mon 18 Jun 14:40:59 UTC 2018

So I’ve done some further research and according to Apache docs:

When used inside a <Location> section, the first argument is omitted
and the local directory is obtained from the <Location>. The same will
occur inside a <LocationMatch> section; however, ProxyPass does not
interpret the regexp as such, so it is necessary to use ProxyPassMatch
in this situation instead.

So I tried ProxyPassMatch and it works however the RegEx matched is passed to Proxy http://localhost:8000/test01 and this is not what I want.

I can achieve this easily with Nginx:

server {
    listen 80;
    server_name exmaple.com;

    location = /test01/ {
        proxy_pass http://localhost:8000/;
        proxy_set_header Host $host;
    }
}

How I can do something similar with Apache?

Update: Sun 24 Jun 10:46:12 UTC 2018

<LocationMatch "^/test01$">
  Redirect / http://test.com/
</LocationMatch>

$ curl -I exmaple.com/test01
HTTP/1.1 302 Found
Date: Sun, 24 Jun 2018 10:47:04 GMT
Server: Apache
Location: http://test.com/test01
Content-Type: text/html; charset=iso-8859-1


Get this bounty!!!

#StackBounty: #apache-2.4 #django Apache: LocationMatch or ProxyMatch doesn't work with RegEx

Bounty: 50

I want to redirect requests for a exact specific location to proxy server but both solutions doesn’t work:

<VirtualHost *:80>
  ServerName exmaple.com

<LocationMatch "^/test01$">
  ProxyPreserveHost On
  ProxyPass http://localhost:8000/
  ProxyPassReverse http://localhost:8000/
</LocationMatch>

<ProxyMatch "^/test02$">
  ProxyPreserveHost On
  ProxyPass http://localhost:8000/
  ProxyPassReverse http://localhost:8000/
</ProxyMatch>

</VirtualHost>

Test:

$ curl -I exmaple.com/test01
HTTP/1.1 404 Not Found
Date: Sun, 17 Jun 2018 15:37:10 GMT
Server: Apache
Content-Type: text/html; charset=iso-8859-1

$ curl -I exmaple.com/test02
HTTP/1.1 404 Not Found
Date: Sun, 17 Jun 2018 15:37:13 GMT
Server: Apache
Content-Type: text/html; charset=iso-8859-1

When I remove the RegEx part then it works but I want Apache to display the 404, not the Django development server:

<LocationMatch "/test01">
  ProxyPreserveHost On
  ProxyPass http://localhost:8000/
  ProxyPassReverse http://localhost:8000/
</LocationMatch>

$ curl -I exmaple.com/test01
HTTP/1.1 200 OK
Date: Sun, 17 Jun 2018 15:42:26 GMT
Server: WSGIServer/0.2 CPython/3.6.5
Content-Type: text/html; charset=utf-8
X-Frame-Options: SAMEORIGIN
Content-Length: 12767

$ curl -I exmaple.com/test01/none
HTTP/1.1 404 Not Found
Date: Sun, 17 Jun 2018 15:42:33 GMT
Server: WSGIServer/0.2 CPython/3.6.5
Content-Type: text/html; charset=UTF-8
X-Frame-Options: SAMEORIGIN
Content-Length: 2073

Update: Mon 18 Jun 14:40:59 UTC 2018

So I’ve done some further research and according to Apache docs:

When used inside a <Location> section, the first argument is omitted
and the local directory is obtained from the <Location>. The same will
occur inside a <LocationMatch> section; however, ProxyPass does not
interpret the regexp as such, so it is necessary to use ProxyPassMatch
in this situation instead.

So I tried ProxyPassMatch and it works however the RegEx matched is passed to Proxy http://localhost:8000/test01 and this is not what I want.

I can achieve this easily with Nginx:

server {
    listen 80;
    server_name exmaple.com;

    location = /test01/ {
        proxy_pass http://localhost:8000/;
        proxy_set_header Host $host;
    }
}

How I can do something similar with Apache?

Update: Sun 24 Jun 10:46:12 UTC 2018

<LocationMatch "^/test01$">
  Redirect / http://test.com/
</LocationMatch>

$ curl -I exmaple.com/test01
HTTP/1.1 302 Found
Date: Sun, 24 Jun 2018 10:47:04 GMT
Server: Apache
Location: http://test.com/test01
Content-Type: text/html; charset=iso-8859-1


Get this bounty!!!

#StackBounty: #apache-2.4 #django Apache: LocationMatch or ProxyMatch doesn't work with RegEx

Bounty: 50

I want to redirect requests for a exact specific location to proxy server but both solutions doesn’t work:

<VirtualHost *:80>
  ServerName exmaple.com

<LocationMatch "^/test01$">
  ProxyPreserveHost On
  ProxyPass http://localhost:8000/
  ProxyPassReverse http://localhost:8000/
</LocationMatch>

<ProxyMatch "^/test02$">
  ProxyPreserveHost On
  ProxyPass http://localhost:8000/
  ProxyPassReverse http://localhost:8000/
</ProxyMatch>

</VirtualHost>

Test:

$ curl -I exmaple.com/test01
HTTP/1.1 404 Not Found
Date: Sun, 17 Jun 2018 15:37:10 GMT
Server: Apache
Content-Type: text/html; charset=iso-8859-1

$ curl -I exmaple.com/test02
HTTP/1.1 404 Not Found
Date: Sun, 17 Jun 2018 15:37:13 GMT
Server: Apache
Content-Type: text/html; charset=iso-8859-1

When I remove the RegEx part then it works but I want Apache to display the 404, not the Django development server:

<LocationMatch "/test01">
  ProxyPreserveHost On
  ProxyPass http://localhost:8000/
  ProxyPassReverse http://localhost:8000/
</LocationMatch>

$ curl -I exmaple.com/test01
HTTP/1.1 200 OK
Date: Sun, 17 Jun 2018 15:42:26 GMT
Server: WSGIServer/0.2 CPython/3.6.5
Content-Type: text/html; charset=utf-8
X-Frame-Options: SAMEORIGIN
Content-Length: 12767

$ curl -I exmaple.com/test01/none
HTTP/1.1 404 Not Found
Date: Sun, 17 Jun 2018 15:42:33 GMT
Server: WSGIServer/0.2 CPython/3.6.5
Content-Type: text/html; charset=UTF-8
X-Frame-Options: SAMEORIGIN
Content-Length: 2073

Update: Mon 18 Jun 14:40:59 UTC 2018

So I’ve done some further research and according to Apache docs:

When used inside a <Location> section, the first argument is omitted
and the local directory is obtained from the <Location>. The same will
occur inside a <LocationMatch> section; however, ProxyPass does not
interpret the regexp as such, so it is necessary to use ProxyPassMatch
in this situation instead.

So I tried ProxyPassMatch and it works however the RegEx matched is passed to Proxy http://localhost:8000/test01 and this is not what I want.

I can achieve this easily with Nginx:

server {
    listen 80;
    server_name exmaple.com;

    location = /test01/ {
        proxy_pass http://localhost:8000/;
        proxy_set_header Host $host;
    }
}

How I can do something similar with Apache?

Update: Sun 24 Jun 10:46:12 UTC 2018

<LocationMatch "^/test01$">
  Redirect / http://test.com/
</LocationMatch>

$ curl -I exmaple.com/test01
HTTP/1.1 302 Found
Date: Sun, 24 Jun 2018 10:47:04 GMT
Server: Apache
Location: http://test.com/test01
Content-Type: text/html; charset=iso-8859-1


Get this bounty!!!

#StackBounty: #apache-2.4 #django Apache: LocationMatch or ProxyMatch doesn't work with RegEx

Bounty: 50

I want to redirect requests for a exact specific location to proxy server but both solutions doesn’t work:

<VirtualHost *:80>
  ServerName exmaple.com

<LocationMatch "^/test01$">
  ProxyPreserveHost On
  ProxyPass http://localhost:8000/
  ProxyPassReverse http://localhost:8000/
</LocationMatch>

<ProxyMatch "^/test02$">
  ProxyPreserveHost On
  ProxyPass http://localhost:8000/
  ProxyPassReverse http://localhost:8000/
</ProxyMatch>

</VirtualHost>

Test:

$ curl -I exmaple.com/test01
HTTP/1.1 404 Not Found
Date: Sun, 17 Jun 2018 15:37:10 GMT
Server: Apache
Content-Type: text/html; charset=iso-8859-1

$ curl -I exmaple.com/test02
HTTP/1.1 404 Not Found
Date: Sun, 17 Jun 2018 15:37:13 GMT
Server: Apache
Content-Type: text/html; charset=iso-8859-1

When I remove the RegEx part then it works but I want Apache to display the 404, not the Django development server:

<LocationMatch "/test01">
  ProxyPreserveHost On
  ProxyPass http://localhost:8000/
  ProxyPassReverse http://localhost:8000/
</LocationMatch>

$ curl -I exmaple.com/test01
HTTP/1.1 200 OK
Date: Sun, 17 Jun 2018 15:42:26 GMT
Server: WSGIServer/0.2 CPython/3.6.5
Content-Type: text/html; charset=utf-8
X-Frame-Options: SAMEORIGIN
Content-Length: 12767

$ curl -I exmaple.com/test01/none
HTTP/1.1 404 Not Found
Date: Sun, 17 Jun 2018 15:42:33 GMT
Server: WSGIServer/0.2 CPython/3.6.5
Content-Type: text/html; charset=UTF-8
X-Frame-Options: SAMEORIGIN
Content-Length: 2073

Update: Mon 18 Jun 14:40:59 UTC 2018

So I’ve done some further research and according to Apache docs:

When used inside a <Location> section, the first argument is omitted
and the local directory is obtained from the <Location>. The same will
occur inside a <LocationMatch> section; however, ProxyPass does not
interpret the regexp as such, so it is necessary to use ProxyPassMatch
in this situation instead.

So I tried ProxyPassMatch and it works however the RegEx matched is passed to Proxy http://localhost:8000/test01 and this is not what I want.

I can achieve this easily with Nginx:

server {
    listen 80;
    server_name exmaple.com;

    location = /test01/ {
        proxy_pass http://localhost:8000/;
        proxy_set_header Host $host;
    }
}

How I can do something similar with Apache?


Get this bounty!!!