def start_new_team(request, *args, **kwargs): if request.method == 'GET': form = NewTeamForm() g = location.get_geoip(request) if g.latlng is not None and g.latlng[0] is not None and g.latlng[1] is not None: city = location.get_nearest_city(g.latlng) if city: form.initial={'city': city, 'tz': city.tz} context = { 'team_form': form, } return render(request, 'get_together/new_team/start_new_team.html', context) elif request.method == 'POST': form = NewTeamForm(request.POST) if form.is_valid(): new_team = form.save() new_team.owner_profile = request.user.profile new_team.save() Member.objects.create(team=new_team, user=request.user.profile, role=Member.ADMIN) ga.add_event(request, action='new_team', category='growth', label=new_team.name) return redirect('define-team', team_id=new_team.pk) else: context = { 'team_form': form, } return render(request, 'get_together/new_team/start_new_team.html', context) else: return redirect('home')
def new_event_start(request): team = request.user.profile.personal_team new_event = Event(team=team, created_by=request.user.profile) if request.method == 'GET': form = NewEventForm(instance=new_event) context = { 'event': new_event, 'team': team, 'event_form': form, } return render(request, 'get_together/new_event/create_event.html', context) elif request.method == 'POST': form = NewEventForm(request.POST, instance=new_event) if form.is_valid: new_event = form.save() Attendee.objects.create(event=new_event, user=request.user.profile, role=Attendee.HOST, status=Attendee.YES) messages.add_message(request, messages.SUCCESS, message=_('Your event has been scheduled! Next, find a place for your event.')) ga.add_event(request, action='new_event', category='activity', label=new_event.get_full_url()) return redirect('new-event-add-place', new_event.id) else: context = { 'event': new_event, 'team': team, 'event_form': form, } return render(request, 'get_together/new_event/create_event.html', context) else: return redirect('home')
def user_confirm_email(request, confirmation_key): if request.user.account.confirm_email(confirmation_key): messages.add_message(request, messages.SUCCESS, message=_('Your email address has been confirmed.')) ga.add_event(request, action='email_confirmed', category='activity', label=str(request.user.profile)) return redirect('confirm-notifications') else: return render(request, 'get_together/new_user/bad_email_confirmation.html')
def create_event(request, team_id): team = get_object_or_404(Team, id=team_id) if not request.user.profile.can_create_event(team): messages.add_message(request, messages.WARNING, message=_('You can not create events for this team.')) return redirect('show-team-by-slug', team_slug=team.slug) new_event = Event(team=team, created_by=request.user.profile) if request.method == 'GET': initial = {} if 'common' in request.GET and request.GET['common'] != '': new_event.parent = CommonEvent.objects.get(id=request.GET['common']) initial['name'] = new_event.parent.name initial['summary'] = new_event.parent.summary initial['start_time'] = new_event.parent.start_time initial['end_time'] = new_event.parent.end_time form = NewTeamEventForm(instance=new_event, initial=initial) context = { 'event': new_event, 'team': team, 'event_form': form, } return render(request, 'get_together/events/create_event.html', context) elif request.method == 'POST': if 'common' in request.POST and request.POST['common'] != '': new_event.parent = CommonEvent.objects.get(id=request.POST['common']) form = NewTeamEventForm(request.POST, instance=new_event) if form.is_valid: new_event = form.save() Attendee.objects.create(event=new_event, user=request.user.profile, role=Attendee.HOST, status=Attendee.YES) if form.cleaned_data.get('recurrences', None): new_series = EventSeries.from_event(new_event, recurrences=form.cleaned_data['recurrences']) new_series.save() new_event.series = new_series new_event.save() messages.add_message(request, messages.SUCCESS, message=_('Your event has been scheduled! Next, find a place for your event.')) ga.add_event(request, action='new_event', category='activity', label=new_event.get_full_url()) return redirect('add-place', new_event.id) else: context = { 'event': new_event, 'team': team, 'event_form': form, } return render(request, 'get_together/events/create_event.html', context) else: return redirect('home')
def start_new_team(request): new_team = Team(owner_profile=request.user.profile) new_team.owner_profile = request.user.profile if request.method == 'GET': if 'organization' in request.GET and request.GET['organization'] != '': org = Organization.objects.get(id=request.GET['organization']) if request.user.profile.can_edit_org(org): new_team.organization = org form = NewTeamForm(instance=new_team) g = location.get_geoip(request) if g.latlng is not None and g.latlng[0] is not None and g.latlng[1] is not None: city = location.get_nearest_city(g.latlng) if city: form.initial={'city': city, 'tz': city.tz} context = { 'team': new_team, 'team_form': form, } if 'event' in request.GET and request.GET['event'] != '': context['event'] = request.GET['event'] return render(request, 'get_together/new_team/start_new_team.html', context) elif request.method == 'POST': if 'organization' in request.POST and request.POST['organization'] != '': org = Organization.objects.get(id=request.POST['organization']) if request.user.profile.can_edit_org(org): new_team.organization = org form = NewTeamForm(request.POST, request.FILES, instance=new_team) if form.is_valid(): new_team = form.save() new_team.save() if 'event' in request.POST and request.POST['event'] != '': try: event = Event.objects.get(id=request.POST['event']) event.team = new_team event.save() except: pass Member.objects.create(team=new_team, user=request.user.profile, role=Member.ADMIN) ga.add_event(request, action='new_team', category='growth', label=new_team.name) return redirect('define-team', team_id=new_team.pk) else: context = { 'team': new_team, 'team_form': form, } if 'event' in request.POST and request.POST['event'] != '': context['event'] = request.POST['event'] return render(request, 'get_together/new_team/start_new_team.html', context) else: return redirect('home')
def start_new_team(request): new_team = Team(owner_profile=request.user.profile) new_team.owner_profile = request.user.profile if request.method == "GET": if "organization" in request.GET and request.GET["organization"] != "": org = Organization.objects.get(id=request.GET["organization"]) if request.user.profile.can_edit_org(org): new_team.organization = org form = NewTeamForm(instance=new_team) g = location.get_geoip(request) if g.latlng is not None and g.latlng[0] is not None and g.latlng[1] is not None: city = location.get_nearest_city(g.latlng) if city: form.initial = {"city": city, "tz": city.tz} context = {"team": new_team, "team_form": form} if "event" in request.GET and request.GET["event"] != "": context["event"] = request.GET["event"] return render(request, "get_together/new_team/start_new_team.html", context) elif request.method == "POST": if "organization" in request.POST and request.POST["organization"] != "": org = Organization.objects.get(id=request.POST["organization"]) if request.user.profile.can_edit_org(org): new_team.organization = org form = NewTeamForm(request.POST, request.FILES, instance=new_team) if form.is_valid(): new_team = form.save() new_team.save() if "event" in request.POST and request.POST["event"] != "": try: event = Event.objects.get(id=request.POST["event"]) event.team = new_team event.save() except: pass Member.objects.create( team=new_team, user=request.user.profile, role=Member.ADMIN ) ga.add_event( request, action="new_team", category="growth", label=new_team.name ) return redirect("define-team", team_id=new_team.pk) else: context = {"team": new_team, "team_form": form} if "event" in request.POST and request.POST["event"] != "": context["event"] = request.POST["event"] return render(request, "get_together/new_team/start_new_team.html", context) else: return redirect("home")
def user_confirm_email(request, confirmation_key): if request.user.account.confirm_email(confirmation_key): messages.add_message( request, messages.SUCCESS, message=_("Your email address has been confirmed."), ) ga.add_event( request, action="email_confirmed", category="activity", label=str(request.user.profile), ) return redirect("confirm-notifications") else: return render(request, "get_together/new_user/bad_email_confirmation.html")
def new_event_start(request): team = request.user.profile.personal_team new_event = Event(team=team, created_by=request.user.profile) if request.method == "GET": form = NewEventForm(instance=new_event) context = {"event": new_event, "team": team, "event_form": form} return render(request, "get_together/new_event/create_event.html", context) elif request.method == "POST": form = NewEventForm(request.POST, instance=new_event) if form.is_valid: new_event = form.save() Attendee.objects.create( event=new_event, user=request.user.profile, role=Attendee.HOST, status=Attendee.YES, ) messages.add_message( request, messages.SUCCESS, message=_( "Your event has been scheduled! Next, find a place for your event." ), ) ga.add_event( request, action="new_event", category="activity", label=new_event.get_full_url(), ) return redirect("new-event-add-place", new_event.id) else: context = {"event": new_event, "team": team, "event_form": form} return render(request, "get_together/new_event/create_event.html", context) else: return redirect("home")
def create_event(request, team_id): team = get_object_or_404(Team, id=team_id) if not request.user.profile.can_create_event(team): messages.add_message( request, messages.WARNING, message=_("You can not create events for this team."), ) return redirect("show-team-by-slug", team_slug=team.slug) new_event = Event(team=team, created_by=request.user.profile) if request.method == "GET": initial = {} if "common" in request.GET and request.GET["common"] != "": new_event.parent = CommonEvent.objects.get( id=request.GET["common"]) initial["name"] = new_event.parent.name initial["summary"] = new_event.parent.summary initial["start_time"] = new_event.parent.start_time initial["end_time"] = new_event.parent.end_time form = NewTeamEventForm(instance=new_event, initial=initial) context = {"event": new_event, "team": team, "event_form": form} return render(request, "get_together/events/create_event.html", context) elif request.method == "POST": if "common" in request.POST and request.POST["common"] != "": new_event.parent = CommonEvent.objects.get( id=request.POST["common"]) form = NewTeamEventForm(request.POST, instance=new_event) if form.is_valid: new_event = form.save() Attendee.objects.create( event=new_event, user=request.user.profile, role=Attendee.HOST, status=Attendee.YES, ) if form.cleaned_data.get("recurrences", None): new_series = EventSeries.from_event( new_event, recurrences=form.cleaned_data["recurrences"]) new_series.save() new_event.series = new_series new_event.save() messages.add_message( request, messages.SUCCESS, message= _("Your event has been scheduled! Next, find a place for your event." ), ) ga.add_event( request, action="new_event", category="activity", label=new_event.get_full_url(), ) return redirect("add-place", new_event.id) else: context = {"event": new_event, "team": team, "event_form": form} return render(request, "get_together/events/create_event.html", context) else: return redirect("home")
def home(request, *args, **kwards): context = {} near_distance = int(request.GET.get("distance", DEFAULT_NEAR_DISTANCE)) context["distance"] = near_distance context["name"] = request.GET.get("name", "") context["geoip_lookup"] = False context["city_search"] = False city = None ll = None if "city" in request.GET and request.GET.get("city"): try: city_id = int(request.GET.get("city")) city = City.objects.get(id=city_id) context["city"] = city ll = [city.latitude, city.longitude] ga.add_event(request, "homepage_search", category="search", label=city.short_name) context["city_search"] = True except: messages.add_message( request, messages.ERROR, message=_("Could not locate the City you requested."), ) context["city_search"] = False if context["city_search"] == False: try: g = location.get_geoip(request) if (g.latlng is not None and len(g.latlng) >= 2 and g.latlng[0] is not None and g.latlng[1] is not None): ll = g.latlng context["geoip_lookup"] = True try: city_distance = 1 # km while city is None and city_distance < 100: minlat = ll[0] - (city_distance / KM_PER_DEGREE_LAT) maxlat = ll[0] + (city_distance / KM_PER_DEGREE_LAT) minlng = ll[1] - (city_distance / (KM_PER_DEGREE_LNG * math.cos(math.radians(ll[0])))) maxlng = ll[1] + (city_distance / (KM_PER_DEGREE_LNG * math.cos(math.radians(ll[0])))) nearby_cities = City.objects.filter( latitude__gte=minlat, latitude__lte=maxlat, longitude__gte=minlng, longitude__lte=maxlng, ) if len(nearby_cities) == 0: city_distance += 1 else: city = sorted( nearby_cities, key=lambda city: location.city_distance_from( ll, city), )[0] if (request.user.is_authenticated and request.user.profile.city is None): profile = request.user.profile profile.city = city profile.save() except Exception as err: print("City lookup failed", err) raise Exception("City lookup filed") else: raise Exception("Geocoder result has no latlng") except Exception as err: context["geoip_lookup"] = False print( "Geocoder lookup failed for %s" % request.META.get("REMOTE_ADDR"), err) if ll is not None: context["latitude"] = ll[0] context["longitude"] = ll[1] try: minlat = ll[0] - (near_distance / KM_PER_DEGREE_LAT) maxlat = ll[0] + (near_distance / KM_PER_DEGREE_LAT) minlng = ll[1] - ( near_distance / (KM_PER_DEGREE_LNG * math.cos(math.radians(ll[0])))) maxlng = ll[1] + ( near_distance / (KM_PER_DEGREE_LNG * math.cos(math.radians(ll[0])))) context["minlat"] = minlat context["maxlat"] = maxlat context["minlng"] = minlng context["maxlng"] = maxlng near_events = Searchable.objects.filter( latitude__gte=minlat, latitude__lte=maxlat, longitude__gte=minlng, longitude__lte=maxlng, end_time__gte=datetime.datetime.now(), ) if context["name"]: near_events = near_events.filter( Q(event_title__icontains=context["name"]) | Q(group_name__icontains=context["name"])) context["near_events"] = sorted( near_events, key=lambda searchable: location.searchable_distance_from( ll, searchable), ) # # If there aren't any teams in the user's geoip area, show them the closest ones if context["geoip_lookup"] and len(near_events) < 1: context["closest_events"] = sorted( Searchable.objects.filter( end_time__gte=datetime.datetime.now()), key=lambda searchable: location.searchable_distance_from( ll, searchable), )[:3] near_teams = Team.objects.filter( city__latitude__gte=minlat, city__latitude__lte=maxlat, city__longitude__gte=minlng, city__longitude__lte=maxlng, ).filter( Q(access=Team.PUBLIC) | Q(access=Team.PRIVATE, members=request.user.profile)) if context["name"]: near_teams = near_teams.filter(name__icontains=context["name"]) near_teams = near_teams.distinct() context["near_teams"] = sorted( near_teams, key=lambda team: location.team_distance_from(ll, team)) # # If there aren't any teams in the user's geoip area, show them the closest ones if context["geoip_lookup"] and len(near_teams) < 1: context["closest_teams"] = sorted( Team.public_objects.all(), key=lambda team: location.team_distance_from(ll, team), )[:3] except Exception as err: print("Error looking up nearby teams and events", err) traceback.print_exc() initial_search = {"distance": near_distance} if city is not None and city.id > 0: initial_search["city"] = city.id context["search_by_city"] = city if context["name"]: initial_search["name"] = context["name"] search_form = SearchTeamsByName(initial=initial_search) context["search_form"] = search_form return render(request, "get_together/index.html", context)
def home(request, *args, **kwards): context = {} if request.user.is_authenticated: user_teams = Team.objects.filter(owner_profile=request.user.profile) if len(user_teams) > 0: context['user_teams'] = user_teams near_distance = int(request.GET.get("distance", DEFAULT_NEAR_DISTANCE)) context['distance'] = near_distance context['geoip_lookup'] = False city = None ll = None if "city" in request.GET and request.GET.get("city"): context['city_search'] = True city = City.objects.get(id=request.GET.get("city")) context['city'] = city ll = [city.latitude, city.longitude] ga.add_event(request, 'homepage_search', category='search', label=city.short_name) else: context['city_search'] = False try: g = location.get_geoip(request) if g.latlng is not None and g.latlng[0] is not None and g.latlng[ 1] is not None: ll = g.latlng context['geoip_lookup'] = True try: city_distance = 1 #km while city is None and city_distance < 100: minlat = ll[0] - (city_distance / KM_PER_DEGREE_LAT) maxlat = ll[0] + (city_distance / KM_PER_DEGREE_LAT) minlng = ll[1] - (city_distance / (KM_PER_DEGREE_LNG * math.cos(math.radians(ll[0])))) maxlng = ll[1] + (city_distance / (KM_PER_DEGREE_LNG * math.cos(math.radians(ll[0])))) nearby_cities = City.objects.filter( latitude__gte=minlat, latitude__lte=maxlat, longitude__gte=minlng, longitude__lte=maxlng) if len(nearby_cities) == 0: city_distance += 1 else: city = sorted(nearby_cities, key=lambda city: location. city_distance_from(ll, city))[0] if request.user.profile.city is None: profile = request.user.profile profile.city = city profile.save() except: raise Exception('City lookup filed') else: raise Exception('Geocoder result has no latlng') except Exception as err: context['geoip_lookup'] = False print( "Geocoder lookup failed for %s" % request.META.get('REMOTE_ADDR'), err) if ll is not None: context['latitude'] = ll[0] context['longitude'] = ll[1] try: minlat = ll[0] - (near_distance / KM_PER_DEGREE_LAT) maxlat = ll[0] + (near_distance / KM_PER_DEGREE_LAT) minlng = ll[1] - ( near_distance / (KM_PER_DEGREE_LNG * math.cos(math.radians(ll[0])))) maxlng = ll[1] + ( near_distance / (KM_PER_DEGREE_LNG * math.cos(math.radians(ll[0])))) context['minlat'] = minlat context['maxlat'] = maxlat context['minlng'] = minlng context['maxlng'] = maxlng near_events = Searchable.objects.filter( latitude__gte=minlat, latitude__lte=maxlat, longitude__gte=minlng, longitude__lte=maxlng, end_time__gte=datetime.datetime.now()) context['near_events'] = sorted( near_events, key=lambda searchable: location.searchable_distance_from( ll, searchable)) # # If there aren't any teams in the user's geoip area, show them the closest ones if context['geoip_lookup'] and len(near_events) < 1: context['closest_events'] = sorted( Searchable.objects.filter( end_time__gte=datetime.datetime.now()), key=lambda searchable: location.searchable_distance_from( ll, searchable))[:3] near_teams = Team.objects.filter(city__latitude__gte=minlat, city__latitude__lte=maxlat, city__longitude__gte=minlng, city__longitude__lte=maxlng) context['near_teams'] = sorted( near_teams, key=lambda team: location.team_distance_from(ll, team)) # # If there aren't any teams in the user's geoip area, show them the closest ones if context['geoip_lookup'] and len(near_teams) < 1: context['closest_teams'] = sorted( Team.objects.all(), key=lambda team: location.team_distance_from(ll, team))[:3] except Exception as err: print("Error looking up nearby teams and events", err) traceback.print_exc() initial_search = {'distance': near_distance} if city is not None and city.id > 0: initial_search['city'] = city.id search_form = SearchForm(initial=initial_search) context['search_form'] = search_form return render(request, 'get_together/index.html', context)