~widelands-dev/widelands-website/remove_djangoratings

423.1.3 by franku
run pyformat on wl_utils.py
1
def get_real_ip(request):
423.1.1 by franku
fix getting ip adress
2
    """Returns the real user IP, even if behind a proxy."""
423.1.5 by franku
reworked wl_utils.py
3
    for key in ('HTTP_X_FORWARDED_FOR', 'REMOTE_ADDR'):
4
        if key in request.META:
5
            return request.META[key]
6
    # No match -> Return a fictional IP to have the model fields not empty
423.1.6 by franku
added comment to wl_utils.py, fixed a missing import
7
    return '192.168.255.255'
423.1.5 by franku
reworked wl_utils.py
8
429.2.1 by franku
reactivating AutoOneToOneField which makes additional saving of models superfluous
9
10
# AutoOneToOneField
11
# =================
12
# Initial implemenation details about AutoOneToOneField:
13
#   http://softwaremaniacs.org/blog/2007/03/07/auto-one-to-one-field/
14
#
15
489.1.6 by franku
run through 1.9 release notes
16
17
from django.db.models.fields.related_descriptors import ReverseOneToOneDescriptor
18
429.2.1 by franku
reactivating AutoOneToOneField which makes additional saving of models superfluous
19
from django.db.models import OneToOneField
20
from django.db import models
21
22
438.1.6 by franku
run the script
23
class AutoReverseOneToOneDescriptor(ReverseOneToOneDescriptor):
489.1.6 by franku
run through 1.9 release notes
24
    """The descriptor that handles the object creation for an
438.1.6 by franku
run the script
25
    AutoOneToOneField."""
26
27
    def __get__(self, instance, instance_type=None):
429.2.1 by franku
reactivating AutoOneToOneField which makes additional saving of models superfluous
28
        model = getattr(self.related, 'related_model', self.related.model)
29
30
        try:
31
            return (super(AutoReverseOneToOneDescriptor, self)
489.1.6 by franku
run through 1.9 release notes
32
                    .__get__(instance, instance_type))
429.2.1 by franku
reactivating AutoOneToOneField which makes additional saving of models superfluous
33
        except model.DoesNotExist:
34
            obj = model(**{self.related.field.name: instance})
35
36
            obj.save()
37
38
            # Don't return obj directly, otherwise it won't be added
39
            # to Django's cache, and the first 2 calls to obj.relobj
40
            # will return 2 different in-memory objects
41
            return (super(AutoReverseOneToOneDescriptor, self)
489.1.6 by franku
run through 1.9 release notes
42
                    .__get__(instance, instance_type))
429.2.1 by franku
reactivating AutoOneToOneField which makes additional saving of models superfluous
43
44
438.1.6 by franku
run the script
45
class AutoOneToOneField(OneToOneField):
429.2.1 by franku
reactivating AutoOneToOneField which makes additional saving of models superfluous
46
    """OneToOneField creates dependent object on first request from parent
438.1.6 by franku
run the script
47
    object if dependent oject has not created yet."""
48
429.2.1 by franku
reactivating AutoOneToOneField which makes additional saving of models superfluous
49
    def contribute_to_related_class(self, cls, related):
50
        setattr(cls, related.get_accessor_name(),
438.1.6 by franku
run the script
51
                AutoReverseOneToOneDescriptor(related))
489.1.6 by franku
run through 1.9 release notes
52