Esempio n. 1
0
def edit_profile(request, username):
    if request.user.username == username or  request.user.is_superuser:
        current_user = User.objects.get(username=username)
        user_p =  get_object_or_404(UserProfileInfo, user=current_user)
        if request.method == 'POST':
            user_form_edit = EditProfileForm(request.POST, instance=current_user)
            profile_form = UserProfileInfoForm(request.POST, instance=user_p)
            if user_form_edit.is_valid() and profile_form.is_valid():
                user = user_form_edit.save()
                user.save()
                profile = profile_form.save(commit=False)
                profile.user = user
                if 'profile_pic' in request.FILES:
                    profile.profile_pic = request.FILES['profile_pic']
                profile_form.save()
                messages.success(request, 'Your profile was successfully updated!')
                return HttpResponseRedirect(reverse("accounts:profile", kwargs={"username": current_user.username}))
            else:
                user_form_edit = EditProfileForm(instance=current_user)
                profile_form = UserProfileInfoForm(instance=user_p)
                return render(request, 'accounts/edit_profile.html',
                              {'user_form_edit': user_form_edit, 'profile_form': profile_form, 'user':current_user})
        else:
            user_form_edit = EditProfileForm(instance=current_user)
            profile_form = UserProfileInfoForm(instance=UserProfileInfo(user=current_user))
            return render(request, 'accounts/edit_profile.html',
                          {'user_form_edit': user_form_edit, 'profile_form': profile_form, 'user':current_user})
    else:
        return HttpResponse("No access to this page")
Esempio n. 2
0
def edit_profile(request):
    if request.user.is_authenticated:
        if request.method == 'POST':
            user_form_edit = EditProfileForm(request.POST, instance=request.user)
            profile_form = UserProfileInfoForm(request.POST, instance=request.user.userprofileinfo)
            if user_form_edit.is_valid() and profile_form.is_valid():
                user = user_form_edit.save()
                user.save()
                profile = profile_form.save(commit=False)
                profile.user = user
                if 'profile_pic' in request.FILES:
                    profile.profile_pic = request.FILES['profile_pic']
                profile_form.save()
                messages.success(request, 'Your profile was successfully updated!')
                return HttpResponseRedirect(reverse("accounts:profile", kwargs={"username": request.user.username}))
            else:
                user_form_edit = EditProfileForm(instance=request.user)
                profile_form = UserProfileInfoForm(instance=request.user.userprofileinfo)
                return render(request, 'accounts/edit_profile.html',
                              {'user_form_edit': user_form_edit, 'profile_form': profile_form})
        else:
            user_form_edit = EditProfileForm(instance=request.user)
            profile_form = UserProfileInfoForm(instance=request.user.userprofileinfo)
            return render(request, 'accounts/edit_profile.html',
                          {'user_form_edit': user_form_edit, 'profile_form': profile_form})
    else:
        return HttpResponse("No access to this page")
Esempio n. 3
0
def newuser(request):
    registered = False

    if request.method == "POST":
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileInfoForm(data=request.POST)

        if user_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()

            profile = profile_form.save(commit=False)
            profile.user = user

            if 'profile_pic' in request.FILES:
                profile.profile_pic = request.FILES['profile_pic']

            profile.save()

            registered = True
        else:
            print(user_form.errors)
    else:
        user_form = UserForm()
        profile_form = UserProfileInfoForm()

    return render(request, 'accounts/newuser.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered})
Esempio n. 4
0
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        user = self.get_object()

        context['is_owner'] = False
        context['profile_info_form'] = UserProfileInfoForm(
            data=model_to_dict(user.profile))
        return context
Esempio n. 5
0
 def get_context_data(self, **kwargs):
     context = super().get_context_data(**kwargs)
     context['user'] = self.request.user
     context['is_owner'] = True
     context['avatar_form'] = AvatarForm()
     context['profile_info_form'] = UserProfileInfoForm(
         data=model_to_dict(self.request.user.profile))
     return context
Esempio n. 6
0
    def get(self, request):
        registered = self.registered
        user_form = UserForm()
        profile_form = UserProfileInfoForm()

        return render(
            request, self.template_name, {
                'user_form': user_form,
                'profile_form': profile_form,
                'registered': registered
            })
Esempio n. 7
0
def register(request):
    registered = False
    if request.method == 'POST':
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileInfoForm(data=request.POST)

        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()

            profile = profile_form.save(commit=False)
            profile.user = user
            if 'profile_pic' in request.FILES:
                profile.profile_pic = request.FILES['profile_pic']
            profile.save()

            registered = True

    else:
        user_form = UserForm()
        profile_form = UserProfileInfoForm()

    return render(
        request, 'registration/registration.html', {
            'user_form': user_form,
            'profile_form': profile_form,
            'registered': registered
        })
Esempio n. 8
0
def register(request):
    registered = False
    if request.method == 'POST':
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileInfoForm(data=request.POST)
        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()
            profile = profile_form.save(commit=False)
            profile.user = user
            if 'profile_pic' in request.FILES:
                print('found it')
                profile.profile_pic = request.FILES['profile_pic']
            profile.save()
            registered = True
            return redirect('index')

        else:
            messages.error(request, user_form.errors)
            messages.error(request, profile_form.errors)
    else:
        user_form = UserForm()
        profile_form = UserProfileInfoForm()
    return render(
        request, 'accounts/registration.html', {
            'user_form': user_form,
            'profile_form': profile_form,
            'registered': registered
        })
Esempio n. 9
0
def register(request):
    registered = False
    if request.method == 'POST':
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileInfoForm(data=request.POST)

        #image = QRForm(data=request.POST)
        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()
            user = authenticate(username=user_form.cleaned_data['username'],
                                password=user_form.cleaned_data['password'])
            profile = profile_form.save(commit=False)
            profile.user = user
            profile.save()

            registered = True
            login(request, user)
        else:
            print(user_form.errors, profile_form.errors)
    else:
        user_form = UserForm()
        profile_form = UserProfileInfoForm()
    return render(
        request, 'accounts/templates/registration/register.html', {
            'user_form': user_form,
            'profile_form': profile_form,
            'registered': registered
        })
Esempio n. 10
0
def register(request):
    registered = False
    if request.method == 'POST':
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileInfoForm(data=request.POST)
        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)
            user.save()
            profile = profile_form.save(commit=False)
            profile.user = user
            profile.save()
            registered = True
            # return render(request,'accounts/registration_welcome.html')
        else:
            return render(
                request, 'accounts/registration.html', {
                    'user_form': user_form,
                    'profile_form': profile_form,
                    'registered': registered,
                    'user_error': user_form.errors,
                    'profile_error': profile_form.errors
                })
    else:
        user_form = UserForm()
        profile_form = UserProfileInfoForm()
    return render(
        request, 'accounts/registration.html', {
            'user_form': user_form,
            'profile_form': profile_form,
            'registered': registered
        })
Esempio n. 11
0
def upload_pic(request):

    if request.method == 'POST':
        form = UserProfileInfoForm(request.POST, request.FILES)
        if form.is_valid():
            profile = form.save(commit=False)
            if 'profile_pic' in request.FILES:
                form.profile_pic = request.FILES['profile_pic']

            m = get_object_or_404(UserProfileInfo, user=request.user)
            m.profile_pic = form.cleaned_data['profile_pic']
            m.save()
            return HttpResponseRedirect(reverse("accounts:profile", kwargs={"username": request.user.username}))
        else:
            form = UserProfileInfoForm(request.POST, request.FILES)
            return render(request, 'accounts/profile.html',{'form': form})
Esempio n. 12
0
def register(request):
    """
    This view function is used to Register new users to
    website, using UserForm and UserProfileInfoForm from
    forms.py . verify all data and save it in User and UserProfileInfo
    Tables
    :param request:
    :return:
    """
    # to send template to check user authorization
    registered = False

    if request.method == "POST":
        # get all data post in form through request and save it in user_form , profile_form
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileInfoForm(data=request.POST)
        # check if post data is valid
        if user_form.is_valid() and profile_form.is_valid():

            # if valid save data in User and UserProfileInfo tables in Database
            user = user_form.save()
            user.set_password(user.password)
            user.save()

            profile = profile_form.save(commit=False)
            profile.user = user
            profile.bio = request.POST.get('bio')
            profile.birth_date = request.POST.get('birth_date')

            if 'profile_image' in request.FILES:
                profile.profile_image = request.FILES['profile_image']

            profile.save()

            registered = True
        else:
            print(user_form.errors, profile_form.errors)

    else:
        user_form = UserForm()
        profile_form = UserProfileInfoForm()

    return render(
        request, 'accounts/registration.html', {
            'user_form': user_form,
            'profile_form': profile_form,
            'registered': registered
        })
Esempio n. 13
0
def register(request):
    registered = False
    if request.method == "POST":
        user_form = UserForm(data=request.POST)
        if user_form.is_valid():
            username = user_form.cleaned_data.get('username')
            email = user_form.cleaned_data.get('email')
            password = user_form.cleaned_data.get('password')
            User.objects.create_user(username=username, password=password,email=email)
            user = authenticate(username=username, password=password)
            registered = True
            login(request, user)
            return render(request, 'accounts/signup.html', {'user_form': user_form,'registered': registered})
        else:
            return render(request, 'accounts/signup.html')
    else:
        user_form = UserForm()
        profile_form = UserProfileInfoForm()
        return render(request, 'accounts/signup.html',
                      {'user_form': user_form,
                       'profile_form': profile_form,
                       'registered': registered})
def signup(request):
    """
    performs registration of the user through forms
    :param request: includes user attributes from user form i.e first_name, last_name, email, password
    :return: returns the success if the user is registered successfully and forms is valid else returns to the user form
    """
    try:
        registered = False
        if request.method == 'POST':
            user_form = UserForm(data=request.POST)
            user_form.username = request.POST['email']
            profile_form = UserProfileInfoForm(data=request.POST)
            if user_form.is_valid() and profile_form.is_valid():
                user = User()
                user.first_name = request.POST.get('first_name')
                user.last_name = request.POST.get('last_name')
                user.email = request.POST.get('email')
                user.username = request.POST.get('email')
                user.set_password(request.POST.get('password'))
                user.save()
                profile = profile_form.save(commit=False)
                profile.user = user
                profile.save()
                registered = True
            else:
                print(user_form.errors, profile_form.errors)
        else:
            user_form = UserForm()
            profile_form = UserProfileInfoForm()
        return render(
            request, 'accounts/registration.html', {
                'user_form': user_form,
                'profile_form': profile_form,
                'registered': registered
            })
    except Exception as e:
        return HttpResponse(e, status=500)
Esempio n. 15
0
    def post(self, request):
        user_form = UserForm(data=request.POST)
        profile_form = UserProfileInfoForm(data=request.POST)

        if user_form.is_valid() and profile_form.is_valid():
            user = user_form.save()
            user.set_password(user.password)

            profile = profile_form.save(commit=False)
            profile.user = user
            profile.id_cluster = None

            # make query from form fields
            city = profile_form.cleaned_data['city']
            street = profile_form.cleaned_data['street']
            house_number = profile_form.cleaned_data['house_number']
            search_text = city + ' ' + street + ' ' + ' ' + str(house_number)

            message = requests.get(
                'https://geocoder.api.here.com/6.2/geocode.json', {
                    'app_id': 'Z7uukAiQbHvHZ43KIBKW',
                    'app_code': 'nadFSh5EHBHkTdUQ3YnTEg',
                    'searchtext': search_text
                })

            data = message.json()
            latitude = data['Response']['View'][0]['Result'][0]['Location'][
                'NavigationPosition'][0]['Latitude']
            longitude = data['Response']['View'][0]['Result'][0]['Location'][
                'NavigationPosition'][0]['Longitude']

            for t in Truck.objects.all():
                status1 = checkDiv(t.start_latitude, t.start_longitude)
                status2 = checkDiv(t.end_latitude, t.end_longitude)
                if (status1 == 1):
                    if ((status2 == 1 and t.start_longitude > t.end_longitude)
                            or status2 == 4):
                        if (isBigger(t.start_latitude, t.start_longitude,
                                     latitude, longitude) or
                                not isBigger(t.end_latitude, t.end_longitude,
                                             latitude, longitude)):
                            profile.id_cluster = t
                    if (status2 == 2 or status2 == 3
                            or (status2 == 1
                                and t.start_longitude < t.end_longitude)):
                        if (isBigger(t.start_latitude, t.start_longitude,
                                     latitude, longitude)
                                and isBigger(t.end_latitude, t.end_longitude,
                                             latitude, longitude)):
                            profile.id_cluster = t
                if (status1 == 2):
                    if ((status2 == 2 and t.start_longitude < t.end_longitude)
                            or status2 == 3):
                        if (not isBigger(t.start_latitude, t.start_longitude,
                                         latitude, longitude)
                                and isBigger(t.end_latitude, t.end_longitude,
                                             latitude, longitude)):
                            profile.id_cluster = t
                    if ((status2 == 2
                         and t.start_longitude > t.end_longitude)):
                        if (not isBigger(t.start_latitude, t.start_longitude,
                                         latitude, longitude)
                                or isBigger(t.end_latitude, t.end_longitude,
                                            latitude, longitude)):
                            profile.id_cluster = t
                    if (status2 == 4 or status2 == 1
                            or (status2 == 2
                                and t.start_longitude < t.end_longitude)):
                        if (not isBigger(t.start_latitude, t.start_longitude,
                                         latitude, longitude) or
                                not isBigger(t.end_latitude, t.end_longitude,
                                             latitude, longitude)):
                            profile.id_cluster = t

                if (status1 == 3):
                    if (status2 == 4 or status2 == 1):
                        if (not isBigger(t.start_latitude, t.start_longitude,
                                         latitude, longitude) and
                                not isBigger(t.end_latitude, t.end_longitude,
                                             latitude, longitude)):
                            profile.id_cluster = t
                    if (status2 == 2 or status2 == 3):
                        if (not isBigger(t.start_latitude, t.start_longitude,
                                         latitude, longitude)
                                or isBigger(t.end_latitude, t.end_longitude,
                                            latitude, longitude)):
                            profile.id_cluster = t

                if (status1 == 4):
                    if ((status2 == 4 and t.start_longitude < t.end_longitude)
                            or status2 == 3 or status2 == 2):
                        if (isBigger(t.start_latitude, t.start_longitude,
                                     latitude, longitude) and
                                not isBigger(t.end_latitude, t.end_longitude,
                                             latitude, longitude)):
                            profile.id_cluster = t
                    if (status2 == 1
                            or (status2 == 4
                                and t.start_longitude > t.end_longitude)):
                        if (isBigger(t.start_latitude, t.start_longitude,
                                     latitude, longitude)
                                or isBigger(t.end_latitude, t.end_longitude,
                                            latitude, longitude)):
                            profile.id_cluster = t

            profile.latitude = float(latitude)
            profile.longitude = float(longitude)

            if 'profile_pic' in request.FILES:
                profile.profile_pic = request.FILES['profile_pic']

            user.save()
            profile.save()

            self.registered = True
        else:
            print(user_form.errors, profile_form.errors)
        return render(
            request, self.template_name, {
                'user_form': user_form,
                'profile_form': profile_form,
                'registered': self.registered
            })