Beispiel #1
0
    def mutate(self, info, **kwargs):

        phoneNumber = kwargs.get('phoneNumber')
        countryPhoneNumber = kwargs.get('countryPhoneNumber')
        countryPhoneCode = kwargs.get('countryPhoneCode')
        key = kwargs.get('key')
        cityId = kwargs.get('cityId')

        if phoneNumber.startswith('0'):
            phoneNumber = phoneNumber.replace('0', '')
            return phoneNumber

        payload = countryPhoneNumber + phoneNumber

        try:
            verification = models.Verification.objects.get(key=key,
                                                           target="phone",
                                                           payload=payload,
                                                           is_verified=False,
                                                           is_edit=False)

            try:
                exstingUser = users_models.User.objects.get(
                    phone_number=phoneNumber)
                exstingUser.is_verified_phone_number = True
                exstingUser.save()
                verification.is_verified = True
                verification.user = exstingUser
                verification.save()
                token = get_token(exstingUser)
                return types.CompletePhoneVerificationResponse(ok=True,
                                                               token=token)

            except users_models.User.DoesNotExist:

                def get_locations_nearby_coords(latitude,
                                                longitude,
                                                max_distance=3000):
                    gcd_formula = "6371 * acos(cos(radians(%s)) * \
                    cos(radians(latitude)) \
                    * cos(radians(longitude) - radians(%s)) + \
                    sin(radians(%s)) * sin(radians(latitude)))"

                    distance_raw_sql = RawSQL(gcd_formula,
                                              (latitude, longitude, latitude))
                    qs = location_models.City.objects.all().annotate(
                        distance=distance_raw_sql).order_by('distance')
                    if max_distance is not None:
                        qs = qs.filter(distance__lt=max_distance)
                        for i in qs:
                            pass
                        return qs

                try:
                    city = location_models.City.objects.get(city_id=cityId)
                except location_models.City.DoesNotExist:
                    cityLatitude, cityLongitude, cityName, countryCode = reversePlace.reverse_place(
                        cityId)
                    nearCities = get_locations_nearby_coords(
                        cityLatitude, cityLongitude, 3000)[:20]

                    try:
                        country = location_models.Country.objects.get(
                            country_code=countryCode)
                    except location_models.Country.DoesNotExist:

                        with open('pinner/locations/countryData.json',
                                  mode='rt',
                                  encoding='utf-8') as file:
                            countryData = json.load(file)
                            currentCountry = countryData[countryCode]
                            countryName = currentCountry['name']
                            countryNameNative = currentCountry['native']
                            countryCapital = currentCountry['capital']
                            countryCurrency = currentCountry['currency']
                            countryPhone = currentCountry['phone']
                            countryEmoji = currentCountry['emoji']
                            continentCode = currentCountry['continent']
                            latitude = currentCountry['latitude']
                            longitude = currentCountry['longitude']

                            try:
                                continent = location_models.Continent.objects.get(
                                    continent_code=continentCode)
                            except:
                                with open(
                                        'pinner/locations/continentData.json',
                                        mode='rt',
                                        encoding='utf-8') as file:
                                    continentData = json.load(file)
                                    continentName = continentData[
                                        continentCode]

                                    try:
                                        gp = locationThumbnail.get_photos(
                                            term=continentName).get_urls()
                                        continentPhotoURL = gp + "?ixlib=rb-0.3.5&q=100&fm=jpg&crop=entropy&cs=faces&h=450&w=450&fit=crop"
                                        continentThumbnailURL = gp + "?ixlib=rb-0.3.5&q=100&fm=jpg&crop=entropy&cs=faces&h=80&w=80&fit=crop"
                                    except:
                                        continentPhotoURL = None
                                        continentThumbnailURL = None

                                    continent = location_models.Continent.objects.create(
                                        continent_name=continentName,
                                        continent_photo=continentPhotoURL,
                                        continent_thumbnail=
                                        continentThumbnailURL,
                                        continent_code=continentCode)

                        try:
                            gp = locationThumbnail.get_photos(
                                term=countryName).get_urls()
                            countryPhotoURL = gp + "?ixlib=rb-0.3.5&q=100&fm=jpg&crop=entropy&cs=faces&h=450&w=450&fit=crop"
                            countryThumbnailURL = gp + "?ixlib=rb-0.3.5&q=100&fm=jpg&crop=entropy&cs=faces&h=80&w=80&fit=crop"
                        except:
                            countryPhotoURL = None
                            countryThumbnailURL = None
                        country = location_models.Country.objects.create(
                            country_code=countryCode,
                            country_name=countryName,
                            country_name_native=countryNameNative,
                            country_capital=countryCapital,
                            country_currency=countryCurrency,
                            country_phone=countryPhone,
                            country_emoji=countryEmoji,
                            country_photo=countryPhotoURL,
                            country_thumbnail=countryThumbnailURL,
                            continent=continent,
                            latitude=latitude,
                            longitude=longitude)

                    try:
                        gp = locationThumbnail.get_photos(
                            term=cityName).get_urls()
                        cityPhotoURL = gp + "?ixlib=rb-0.3.5&q=100&fm=jpg&crop=entropy&cs=faces&h=450&w=450&fit=crop"
                        cityThumbnailURL = gp + "?ixlib=rb-0.3.5&q=100&fm=jpg&crop=entropy&cs=faces&h=80&w=80&fit=crop"
                    except:
                        cityPhotoURL = None
                        cityThumbnailURL = None
                    city = location_models.City.objects.create(
                        city_id=cityId,
                        city_name=cityName,
                        country=country,
                        city_photo=cityPhotoURL,
                        city_thumbnail=cityThumbnailURL,
                        latitude=cityLatitude,
                        longitude=cityLongitude)
                    for i in nearCities:
                        city.near_city.add(i)
                        city.save()

                with open('pinner/users/adjectives.json',
                          mode='rt',
                          encoding='utf-8') as adjectives:
                    with open('pinner/users/nouns.json',
                              mode='rt',
                              encoding='utf-8') as nouns:
                        adjectives = json.load(adjectives)
                        nouns = json.load(nouns)
                        username = random.choice(adjectives) + random.choice(
                            nouns).capitalize()
                        newUser = users_models.User.objects.create_user(
                            username=username)
                        token = get_token(newUser)
                        city = location_models.City.objects.get(city_id=cityId)
                        newUser.country_phone_number = countryPhoneNumber
                        newUser.country_phone_code = countryPhoneCode
                        newUser.phone_number = phoneNumber
                        newUser.current_city = city
                        newUser.current_country = city.country
                        newUser.current_continent = city.country.continent
                        moveNotification = notification_models.MoveNotification.objects.create(
                            actor=newUser,
                            city=city,
                            country=city.country,
                            continent=city.country.continent,
                        )
                        newUser.is_verified_phone_number = True
                        newUser.save()
                        verification.is_verified = True
                        verification.user = newUser
                        verification.save()
                        return types.CompletePhoneVerificationResponse(
                            ok=True, token=token)

        except models.Verification.DoesNotExist:
            return types.CompletePhoneVerificationResponse(ok=False,
                                                           token=None)
            raise Exception('Verification key not valid')
Beispiel #2
0
def createCity(cityId):
    def get_locations_nearby_coords(latitude, longitude, max_distance=3000):
        gcd_formula = "6371 * acos(cos(radians(%s)) * \
        cos(radians(latitude)) \
        * cos(radians(longitude) - radians(%s)) + \
        sin(radians(%s)) * sin(radians(latitude)))"

        distance_raw_sql = RawSQL(gcd_formula, (latitude, longitude, latitude))
        qs = location_models.City.objects.all().annotate(
            distance=distance_raw_sql).order_by('distance')
        if max_distance is not None:
            qs = qs.filter(distance__lt=max_distance)
            for i in qs:
                pass
            return qs

    cityLatitude, cityLongitude, cityName, countryCode = reversePlace.reverse_place(
        cityId)
    nearCities = get_locations_nearby_coords(cityLatitude, cityLongitude,
                                             3000)[:20]
    if cityLatitude and cityLongitude and cityName and countryCode:
        try:
            country = location_models.Country.objects.get(
                country_code=countryCode)
        except location_models.Country.DoesNotExist:

            with open('pinner/locations/countryData.json',
                      mode='rt',
                      encoding='utf-8') as file:
                countryData = json.load(file)
                currentCountry = countryData[countryCode]
                countryName = currentCountry['name']
                countryNameNative = currentCountry['native']
                countryCapital = currentCountry['capital']
                countryCurrency = currentCountry['currency']
                countryPhone = currentCountry['phone']
                countryEmoji = currentCountry['emoji']
                continentCode = currentCountry['continent']
                latitude = currentCountry['latitude']
                longitude = currentCountry['longitude']

                try:
                    continent = location_models.Continent.objects.get(
                        continent_code=continentCode)
                except:
                    with open('pinner/locations/continentData.json',
                              mode='rt',
                              encoding='utf-8') as file:
                        continentData = json.load(file)
                        continentName = continentData[continentCode]

                        try:
                            gp = locationThumbnail.get_photos(
                                term=continentName).get_urls()
                            continentPhotoURL = gp + "?ixlib=rb-0.3.5&q=100&fm=jpg&crop=entropy&cs=faces&h=450&w=450&fit=crop"
                            continentThumbnailURL = gp + "?ixlib=rb-0.3.5&q=100&fm=jpg&crop=entropy&cs=faces&h=80&w=80&fit=crop"
                        except:
                            continentPhotoURL = None
                            continentThumbnailURL = None

                        continent = location_models.Continent.objects.create(
                            continent_name=continentName,
                            continent_photo=continentPhotoURL,
                            continent_thumbnail=continentThumbnailURL,
                            continent_code=continentCode)
            try:
                gp = locationThumbnail.get_photos(term=countryName).get_urls()
                countryPhotoURL = gp + "?ixlib=rb-0.3.5&q=100&fm=jpg&crop=entropy&cs=faces&h=450&w=450&fit=crop"
                countryThumbnailURL = gp + "?ixlib=rb-0.3.5&q=100&fm=jpg&crop=entropy&cs=faces&h=80&w=80&fit=crop"
            except:
                countryPhotoURL = None
                countryThumbnailURL = None
            country = location_models.Country.objects.create(
                country_code=countryCode,
                country_name=countryName,
                country_name_native=countryNameNative,
                country_capital=countryCapital,
                country_currency=countryCurrency,
                country_phone=countryPhone,
                country_emoji=countryEmoji,
                country_photo=countryPhotoURL,
                country_thumbnail=countryThumbnailURL,
                continent=continent,
                latitude=latitude,
                longitude=longitude)

        try:
            gp = locationThumbnail.get_photos(term=cityName).get_urls()
            cityPhotoURL = gp + "?ixlib=rb-0.3.5&q=100&fm=jpg&crop=entropy&cs=faces&h=450&w=450&fit=crop"
            cityThumbnailURL = gp + "?ixlib=rb-0.3.5&q=100&fm=jpg&crop=entropy&cs=faces&h=80&w=80&fit=crop"
        except:
            cityPhotoURL = None
            cityThumbnailURL = None
        city = location_models.City.objects.create(
            city_id=cityId,
            city_name=cityName,
            country=country,
            city_photo=cityPhotoURL,
            city_thumbnail=cityThumbnailURL,
            latitude=cityLatitude,
            longitude=cityLongitude)
        print(city.city_name)
        for i in nearCities:
            city.near_city.add(i)
            city.save()
    def mutate(self, info, **kwargs):

        first_name = kwargs.get('first_name')
        last_name = kwargs.get('last_name')
        email = kwargs.get('email')
        gender = kwargs.get('gender')
        cityId = kwargs.get('cityId')
        countryCode = kwargs.get('countryCode')
        appleId = kwargs.get('appleId')

        try:
            user = models.User.objects.get(
                appleId=appleId
            )
            token = get_token(user)
            return types.AppleConnectResponse(ok=True, token=token)

        except models.User.DoesNotExist:

            def get_locations_nearby_coords(latitude, longitude, max_distance=3000):
                gcd_formula = "6371 * acos(cos(radians(%s)) * \
                cos(radians(latitude)) \
                * cos(radians(longitude) - radians(%s)) + \
                sin(radians(%s)) * sin(radians(latitude)))"
                distance_raw_sql = RawSQL(
                    gcd_formula,
                    (latitude, longitude, latitude)
                )
                qs = location_models.City.objects.all().annotate(distance=distance_raw_sql).order_by('distance')
                if max_distance is not None:
                    qs = qs.filter(Q(distance__lt=max_distance))
                    for i in qs:
                        pass
                return qs

            try:
                city = location_models.City.objects.get(city_id=cityId)
            except location_models.City.DoesNotExist:
                cityLatitude, cityLongitude, cityName, countryCode = reversePlace.reverse_place(cityId)
                nearCities = get_locations_nearby_coords(cityLatitude, cityLongitude, 3000)[:20]

                try:
                    country = location_models.Country.objects.get(country_code=countryCode)
                except location_models.Country.DoesNotExist:

                    with open('pinner/locations/countryData.json', mode='rt', encoding='utf-8') as file:
                        countryData = json.load(file)
                        currentCountry = countryData[countryCode]
                        countryName = currentCountry['name']
                        countryNameNative = currentCountry['native']
                        countryCapital = currentCountry['capital']
                        countryCurrency = currentCountry['currency']
                        countryPhone = currentCountry['phone']
                        countryEmoji = currentCountry['emoji']
                        continentCode = currentCountry['continent']
                        latitude = currentCountry['latitude']
                        longitude = currentCountry['longitude']

                        try:
                            continent = location_models.Continent.objects.get(continent_code=continentCode)
                        except:
                            with open('pinner/locations/continentData.json', mode='rt', encoding='utf-8') as file:
                                continentData = json.load(file)
                                continentName = continentData[continentCode]

                                try:
                                    gp = locationThumbnail.get_photos(term=continentName).get_urls()
                                    continentPhotoURL = gp+"?ixlib=rb-0.3.5&q=100&fm=jpg&crop=entropy&cs=faces&h=450&w=450&fit=crop"
                                    continentThumbnailURL = gp+"?ixlib=rb-0.3.5&q=100&fm=jpg&crop=entropy&cs=faces&h=80&w=80&fit=crop"
                                except:
                                    continentPhotoURL = None
                                    continentThumbnailURL = None

                                continent = location_models.Continent.objects.create(
                                    continent_name=continentName,
                                    continent_photo=continentPhotoURL,
                                    continent_thumbnail=continentThumbnailURL,
                                    continent_code=continentCode
                                )
                    try:
                        gp = locationThumbnail.get_photos(term=countryName).get_urls()
                        countryPhotoURL = gp+"?ixlib=rb-0.3.5&q=100&fm=jpg&crop=entropy&cs=faces&h=450&w=450&fit=crop"
                        countryThumbnailURL = gp+"?ixlib=rb-0.3.5&q=100&fm=jpg&crop=entropy&cs=faces&h=80&w=80&fit=crop"
                    except:
                        countryPhotoURL = None
                        countryThumbnailURL = None

                    country = location_models.Country.objects.create(
                        country_code=countryCode,
                        country_name=countryName,
                        country_name_native=countryNameNative,
                        country_capital=countryCapital,
                        country_currency=countryCurrency,
                        country_phone=countryPhone,
                        country_emoji=countryEmoji,
                        country_photo=countryPhotoURL,
                        country_thumbnail=countryThumbnailURL,
                        continent=continent,
                        latitude=latitude,
                        longitude=longitude
                    )

                try:
                    gp = locationThumbnail.get_photos(term=cityName).get_urls()
                    cityPhotoURL = gp+"?ixlib=rb-0.3.5&q=100&fm=jpg&crop=entropy&cs=faces&h=450&w=450&fit=crop"
                    cityThumbnailURL = gp+"?ixlib=rb-0.3.5&q=100&fm=jpg&crop=entropy&cs=faces&h=80&w=80&fit=crop"
                except:
                    cityPhotoURL = None
                    cityThumbnailURL = None

                city = location_models.City.objects.create(
                    city_id=cityId,
                    city_name=cityName,
                    country=country,
                    city_photo=cityPhotoURL,
                    city_thumbnail=cityThumbnailURL,
                    latitude=cityLatitude,
                    longitude=cityLongitude
                )
                for i in nearCities:
                    city.near_city.add(i)
                    city.save()

            with open('pinner/users/adjectives.json', mode='rt', encoding='utf-8') as adjectives:
                with open('pinner/users/nouns.json', mode='rt', encoding='utf-8') as nouns:
                    adjectives = json.load(adjectives)
                    nouns = json.load(nouns)

                    if email:
                        local, at, domain = email.rpartition('@')
                        username = random.choice(adjectives) + local.capitalize()
                        is_verified_email_address = True
                    else:
                        username = random.choice(adjectives) + random.choice(nouns).capitalize()
                        is_verified_email_address = False

                    newUser = models.User.objects.create_user(username)
                    if first_name:
                        newUser.first_name = first_name
                    if last_name:
                        newUser.last_name = last_name
                    newUser.save()
                    newUser.appleId = appleId
                    newUser.email_address = email
                    newUser.is_verified_email_address = is_verified_email_address
                    newUser.gender = gender
                    newUser.current_city = city
                    newUser.current_country = city.country
                    newUser.current_continent = city.country.continent
                    newUser.save()

                    moveNotification = notification_models.MoveNotification.objects.create(
                        actor=newUser,
                        city=city,
                        country=city.country,
                        continent=city.country.continent,
                    )

                    token = get_token(newUser)
                    return types.AppleConnectResponse(ok=True, token=token)