# Path: src/librement/profile/forms.py
# class ProfileForm(forms.ModelForm):
#     class Meta:
#         model = Profile
#         fields = (
#             'display_name',
#             'biography',
#             'rss_url',
#         )
# 
# class AccountForm(dict):
#     def __init__(self, user, *args, **kwargs):
#         self.user = user
# 
#         for key, klass, fn in (
#             ('user', AccountUserForm, lambda x: x),
#             ('profile', AccountProfileForm, lambda x: x.profile),
#         ):
#             self[key] = klass(instance=fn(user), *args, **kwargs)
# 
#     def save(self):
#         return [x.save() for x in self.values()]
# 
#     def is_valid(self):
#         return all(x.is_valid() for x in self.values())
# 
# class URLForm(forms.ModelForm):
#     username = forms.RegexField(regex=r'^[\w-]+$')
# 
#     class Meta:
#         model = User
#         fields = (
#             'username',
#         )
# 
# class PictureForm(forms.Form):
#     picture = forms.ImageField()
# 
#     def __init__(self, user, *args, **kwargs):
#         self.user = user
# 
#         super(PictureForm, self).__init__(*args, **kwargs)
# 
#     def save(self):
#         self.user.profile.picture.save(
#             self.cleaned_data['picture']
#         )
#         self.user.profile.save()
# 
# class PasswordForm(forms.Form):
#     password_old = forms.CharField()
#     password = forms.CharField()
#     password_confirm = forms.CharField()
# 
#     def __init__(self, user, *args, **kwargs):
#         self.user = user
# 
#         super(PasswordForm, self).__init__(*args, **kwargs)
# 
#     def save(self):
#         self.user.set_password(self.cleaned_data['password'])
#         self.user.save()
# 
#     def clean_password_old(self):
#         val = self.cleaned_data['password_old']
# 
#         if not self.user.check_password(val):
#             raise forms.ValidationError(
#                 "Password is not correct."
#             )
# 
#         return val
# 
#     def clean_password_confirm(self):
#         password = self.cleaned_data.get('password', '')
#         password_confirm = self.cleaned_data['password_confirm']
# 
#         if password != password_confirm:
#             raise forms.ValidationError("Passwords do not match.")
# 
#         if len(password) < 8:
#             raise forms.ValidationError(
#                 "Password must be at least 8 characters"
#             )
# 
#         return password
# 
# Path: src/librement/profile/utils.py
# def get_rss_feed(url):
#     if not url:
#         return None
# 
#     key = 'feed:%s' % hashlib.sha1(url).hexdigest()
#     feed = cache.get(key)
# 
#     if feed is None:
#         feed = feedparser.parse(url)
#         cache.set(key, feed, 3600)
# 
#     return feed


from django.contrib import messages
from django.shortcuts import render, get_object_or_404, redirect
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from librement.utils.ajax import ajax
from .forms import ProfileForm, AccountForm, URLForm, PictureForm, PasswordForm
from .utils import get_rss_feed
@ajax()
def xhr_rss(request, username):
    user = get_object_or_404(User, username=username)

    return render(request, 'profile/rss.html', {
        'feed': get_rss_feed(user.profile.rss_url),
    })

@login_required
def edit(request):
    if request.method == 'POST':
        form = ProfileForm(request.POST, instance=request.user.profile)

        if form.is_valid():
            form.save()

            messages.success(request, "Profile updated successfully")

            return redirect('profile:edit')

    else:
        form = ProfileForm(instance=request.user.profile)

    return render(request, 'profile/edit/view.html', {
        'form': form,
    })

@login_required
def edit_account(request):
    if request.method == 'POST':

# Path: src/librement/profile/forms.py
# class ProfileForm(forms.ModelForm):
#     class Meta:
#         model = Profile
#         fields = (
#             'display_name',
#             'biography',
#             'rss_url',
#         )
# 
# class AccountForm(dict):
#     def __init__(self, user, *args, **kwargs):
#         self.user = user
# 
#         for key, klass, fn in (
#             ('user', AccountUserForm, lambda x: x),
#             ('profile', AccountProfileForm, lambda x: x.profile),
#         ):
#             self[key] = klass(instance=fn(user), *args, **kwargs)
# 
#     def save(self):
#         return [x.save() for x in self.values()]
# 
#     def is_valid(self):
#         return all(x.is_valid() for x in self.values())
# 
# class URLForm(forms.ModelForm):
#     username = forms.RegexField(regex=r'^[\w-]+$')
# 
#     class Meta:
#         model = User
#         fields = (
#             'username',
#         )
# 
# class PictureForm(forms.Form):
#     picture = forms.ImageField()
# 
#     def __init__(self, user, *args, **kwargs):
#         self.user = user
# 
#         super(PictureForm, self).__init__(*args, **kwargs)
# 
#     def save(self):
#         self.user.profile.picture.save(
#             self.cleaned_data['picture']
#         )
#         self.user.profile.save()
# 
# class PasswordForm(forms.Form):
#     password_old = forms.CharField()
#     password = forms.CharField()
#     password_confirm = forms.CharField()
# 
#     def __init__(self, user, *args, **kwargs):
#         self.user = user
# 
#         super(PasswordForm, self).__init__(*args, **kwargs)
# 
#     def save(self):
#         self.user.set_password(self.cleaned_data['password'])
#         self.user.save()
# 
#     def clean_password_old(self):
#         val = self.cleaned_data['password_old']
# 
#         if not self.user.check_password(val):
#             raise forms.ValidationError(
#                 "Password is not correct."
#             )
# 
#         return val
# 
#     def clean_password_confirm(self):
#         password = self.cleaned_data.get('password', '')
#         password_confirm = self.cleaned_data['password_confirm']
# 
#         if password != password_confirm:
#             raise forms.ValidationError("Passwords do not match.")
# 
#         if len(password) < 8:
#             raise forms.ValidationError(
#                 "Password must be at least 8 characters"
#             )
# 
#         return password
# 
# Path: src/librement/profile/utils.py
# def get_rss_feed(url):
#     if not url:
#         return None
# 
#     key = 'feed:%s' % hashlib.sha1(url).hexdigest()
#     feed = cache.get(key)
# 
#     if feed is None:
#         feed = feedparser.parse(url)
#         cache.set(key, feed, 3600)
# 
#     return feed


# Path: src/librement/profile/views.py
from django.contrib import messages
from django.shortcuts import render, get_object_or_404, redirect
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from librement.utils.ajax import ajax
from .forms import ProfileForm, AccountForm, URLForm, PictureForm, PasswordForm
from .utils import get_rss_feed

@ajax()
def xhr_rss(request, username):
    user = get_object_or_404(User, username=username)

    return render(request, 'profile/rss.html', {
        'feed': get_rss_feed(user.profile.rss_url),
    })

@login_required
def edit(request):
    if request.method == 'POST':
        form = ProfileForm(request.POST, instance=request.user.profile)

        if form.is_valid():
            form.save()

            messages.success(request, "Profile updated successfully")

            return redirect('profile:edit')

    else:
        form = ProfileForm(instance=request.user.profile)

    return render(request, 'profile/edit/view.html', {
        'form': form,
    })

@login_required
def edit_account(request):
    if request.method == 'POST':

        form = AccountForm(request.user, request.POST)